Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(n-datatable): where data containing , is truncated when exporting using the downloadCsv method #6410

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.en-US.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# CHANGELOG

## NEXT_VERSION

`NEXT_VERSION`

### Fixes

- Fix the issue where Chinese column names are encoded when exporting using the `downloadCsv` method of `n-data-table`
- Fix the issue where data containing `,` is truncated when exporting using the `downloadCsv` method of `n-data-table`

## 2.40.1

`2024-09-26`
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.zh-CN.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# CHANGELOG

## NEXT_VERSION

`NEXT_VERSION`

### Fixes

- 修复 `n-data-table` 的 `downloadCsv` 方法在导出中文列名的时候,中文列名会被编码的问题
- 修复 `n-data-table` 的 `downloadCsv` 方法在导出包含 `,` 的数据时,数据会被截断的问题

## 2.40.1

`2024-09-26`
Expand Down
2 changes: 1 addition & 1 deletion src/data-table/src/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export default defineComponent({
const downloadCsv = (options?: CsvOptionsType): void => {
const { fileName = 'data.csv', keepOriginalData = false } = options || {}
const data = keepOriginalData ? props.data : rawPaginatedDataRef.value
const csvData = generateCsv(props.columns, data)
const csvData = `\uFEFF${generateCsv(props.columns, data)}`
const blob = new Blob([csvData], { type: 'text/csv;charset=utf-8' })
const downloadUrl = URL.createObjectURL(blob)
download(
Expand Down
16 changes: 10 additions & 6 deletions src/data-table/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,19 @@ export function isColumnSorting(
}

function formatCsvCell(value: unknown): string {
if (typeof value === 'string') {
return value.replace(/,/g, '\\,')
}
else if (value === null || value === undefined) {
if (value === null || value === undefined) {
return ''
}
else {
return `${value}`.replace(/,/g, '\\,')
const stringValue = String(value)
if (
stringValue.includes('"')
|| stringValue.includes(',')
|| stringValue.includes('\n')
) {
const escapedValue = stringValue.replace(/"/g, '""')
return `"${escapedValue}"`
}
return stringValue
}

export function generateCsv(columns: TableColumn[], data: RowData[]): string {
Expand Down
Loading