@zhangt58/svelte-vtable 0.5.1 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +53 -0
- package/dist/components/DataTable.svelte +17 -24
- package/dist/components/DataTable.svelte.d.ts +5 -1
- package/dist/exportUtils.d.ts +84 -0
- package/dist/exportUtils.js +155 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +17 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -200,6 +200,9 @@ The `rowSnippet` receives an object with:
|
|
|
200
200
|
| `filterType` | `'value' \| 'list' \| 'daterange' \| 'datetimerange' \| 'none'` | `'value'` | Filter UI for this column. `'list'` is an alias for value-list filters; `'none'` excludes it from `buildColumnFilters`. |
|
|
201
201
|
| `headerSnippet` | `Snippet<[{key, label, sortKey, sortDir}]>` | `undefined` | Custom Svelte 5 snippet rendered inside `<th>`. |
|
|
202
202
|
| `cellSnippet` | `Snippet<[{item, value, index}]>` | `undefined` | Custom Svelte 5 snippet rendered inside each `<td>`. Used for default row rendering when `rowSnippet` is not provided. |
|
|
203
|
+
| `exportable` | `boolean` | `true` | Set `false` to leave the column out of CSV exports (e.g. an actions column). |
|
|
204
|
+
| `exportLabel` | `string` | `label` | CSV header for the column. |
|
|
205
|
+
| `exportValue` | `(item, value) => unknown` | raw value | Plain value written to CSV (a `cellSnippet` cannot be exported). |
|
|
203
206
|
|
|
204
207
|
#### Minimal example using `columns`
|
|
205
208
|
|
|
@@ -361,6 +364,56 @@ This also covers truncation inside a `cellSnippet` (e.g. a `truncate` or `line-c
|
|
|
361
364
|
element) and truncated header labels. A `title` you set on a cell's content takes
|
|
362
365
|
precedence.
|
|
363
366
|
|
|
367
|
+
#### Exporting the current view to CSV
|
|
368
|
+
|
|
369
|
+
Export what the user sees — the visible columns, in their order, and the rows left after
|
|
370
|
+
search, filters and sort — with the helpers from the package:
|
|
371
|
+
|
|
372
|
+
```svelte
|
|
373
|
+
<script>
|
|
374
|
+
import { DataTable, applyFilters, sortItems, exportCSV } from '@zhangt58/svelte-vtable';
|
|
375
|
+
|
|
376
|
+
let sortKey = $state(null);
|
|
377
|
+
let sortDir = $state('asc');
|
|
378
|
+
// visibleColumns: the ColumnDef[] passed to the table (after a column chooser)
|
|
379
|
+
const viewRows = $derived(sortItems(applyFilters(searchedRows, activeFilters), sortKey, sortDir));
|
|
380
|
+
</script>
|
|
381
|
+
|
|
382
|
+
<button onclick={() => exportCSV(viewRows, visibleColumns, { filename: 'view.csv' })}>
|
|
383
|
+
Export CSV
|
|
384
|
+
</button>
|
|
385
|
+
<DataTable items={pagedRows} columns={visibleColumns} bind:sortKey bind:sortDir />
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
Export the full view rather than the table's `items` when the table is paged: `items` is
|
|
389
|
+
only the current page. When the table holds all rows itself (local filters and sort),
|
|
390
|
+
its `exportCSV()` method exports exactly what it shows:
|
|
391
|
+
|
|
392
|
+
```svelte
|
|
393
|
+
<script>
|
|
394
|
+
let table = $state();
|
|
395
|
+
</script>
|
|
396
|
+
|
|
397
|
+
<button onclick={() => table.exportCSV({ filename: 'orders.csv' })}>Export CSV</button>
|
|
398
|
+
<DataTable bind:this={table} {items} {columns} inlineFilters />
|
|
399
|
+
```
|
|
400
|
+
|
|
401
|
+
- Cells hold the raw row values; set `ColumnDef.exportValue` for a formatted or derived
|
|
402
|
+
value (a `cellSnippet` is not exported), `exportLabel` for another header and
|
|
403
|
+
`exportable: false` to leave a column out.
|
|
404
|
+
- Arrays are joined with `; `, dates written as ISO strings, objects as JSON.
|
|
405
|
+
- Text starting with `=`, `+`, `-`, `@` is prefixed with `'` so spreadsheets do not run it
|
|
406
|
+
as a formula (`escapeFormulas: false` turns that off; numbers are never changed).
|
|
407
|
+
- The file is UTF-8 with a byte order mark so Excel reads accents correctly (`bom: false`
|
|
408
|
+
to leave it out).
|
|
409
|
+
|
|
410
|
+
| Function | Description |
|
|
411
|
+
| --------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
|
|
412
|
+
| `exportCSV(rows, columns, options?)` | Build the CSV and save it as a file. Options: `filename` (`'export.csv'`), `bom`, and the `toCSV` options. Returns the CSV text. |
|
|
413
|
+
| `toCSV(rows, columns, options?)` | CSV text only. Options: `delimiter` (`','`), `header` (`true`), `lineEnding` (`'\r\n'`), `escapeFormulas` (`true`). |
|
|
414
|
+
| `downloadCSV(csv, filename?, { bom }?)` | Save CSV text as a file in the browser. |
|
|
415
|
+
| `sortItems(items, key, dir?)` | Sort rows the way `DataTable` sorts locally, e.g. to export in the table's order. |
|
|
416
|
+
|
|
364
417
|
#### Row Snippet Parameters
|
|
365
418
|
|
|
366
419
|
The `rowSnippet` receives an object with:
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
hasActiveFilterValue,
|
|
10
10
|
resolveColumnFilters,
|
|
11
11
|
} from '../filterUtils.js';
|
|
12
|
+
import { exportCSV as exportRowsToCSV, sortItems } from '../exportUtils.js';
|
|
12
13
|
|
|
13
14
|
// Runic props: accept props via $props() so the component matches the
|
|
14
15
|
// project's runes-style components. Include the `select` callback prop.
|
|
@@ -409,32 +410,24 @@
|
|
|
409
410
|
// pass items through unchanged. Otherwise we sort locally.
|
|
410
411
|
const sortedItems = $derived.by(() => {
|
|
411
412
|
if (onsort !== undefined || !sortKey) return filteredItems;
|
|
412
|
-
|
|
413
|
-
const key = sortKey;
|
|
414
|
-
const dir = sortDir;
|
|
415
|
-
return [...filteredItems].sort((a, b) => {
|
|
416
|
-
const aVal = a[key];
|
|
417
|
-
const bVal = b[key];
|
|
418
|
-
|
|
419
|
-
// Handle null/undefined values
|
|
420
|
-
if (aVal == null && bVal == null) return 0;
|
|
421
|
-
if (aVal == null) return 1;
|
|
422
|
-
if (bVal == null) return -1;
|
|
423
|
-
|
|
424
|
-
// Convert to strings for comparison
|
|
425
|
-
const aStr = String(aVal);
|
|
426
|
-
const bStr = String(bVal);
|
|
427
|
-
|
|
428
|
-
// Alphabetical comparison
|
|
429
|
-
const comparison = aStr.localeCompare(bStr, undefined, {
|
|
430
|
-
numeric: true,
|
|
431
|
-
sensitivity: 'base',
|
|
432
|
-
});
|
|
433
|
-
|
|
434
|
-
return dir === 'asc' ? comparison : -comparison;
|
|
435
|
-
});
|
|
413
|
+
return sortItems(filteredItems, sortKey, sortDir);
|
|
436
414
|
});
|
|
437
415
|
|
|
416
|
+
/**
|
|
417
|
+
* Save the rows this table shows — after its local filters and sort — as a
|
|
418
|
+
* CSV file, with the current columns (`exportable: false` ones left out).
|
|
419
|
+
* When the parent pages, filters or sorts the rows itself, `items` is only
|
|
420
|
+
* what the parent passed: export the full view with `exportCSV` from the
|
|
421
|
+
* package instead. Call through `bind:this`.
|
|
422
|
+
* @param {Object} [options] - `filename` (default `'export.csv'`), `rows` and
|
|
423
|
+
* `columns` overrides, and the `toCSV` / `downloadCSV` options.
|
|
424
|
+
* @returns {string} The CSV text.
|
|
425
|
+
*/
|
|
426
|
+
export function exportCSV(options = {}) {
|
|
427
|
+
const { rows = sortedItems, columns: exportColumns = columns, ...rest } = options;
|
|
428
|
+
return exportRowsToCSV(rows, exportColumns, rest);
|
|
429
|
+
}
|
|
430
|
+
|
|
438
431
|
// expose selection via the callback prop (if provided)
|
|
439
432
|
function selectItem(item, index) {
|
|
440
433
|
selected = item;
|
|
@@ -2,6 +2,8 @@ export default DataTable;
|
|
|
2
2
|
type DataTable = {
|
|
3
3
|
$on?(type: string, callback: (e: any) => void): () => void;
|
|
4
4
|
$set?(props: Partial<$$ComponentProps>): void;
|
|
5
|
+
} & {
|
|
6
|
+
exportCSV: (options?: any) => string;
|
|
5
7
|
};
|
|
6
8
|
declare const DataTable: import("svelte").Component<{
|
|
7
9
|
items?: any[];
|
|
@@ -34,7 +36,9 @@ declare const DataTable: import("svelte").Component<{
|
|
|
34
36
|
oncolumnresize?: any;
|
|
35
37
|
storageKey?: any;
|
|
36
38
|
wrap?: boolean;
|
|
37
|
-
}, {
|
|
39
|
+
}, {
|
|
40
|
+
exportCSV: (options?: any) => string;
|
|
41
|
+
}, "sortKey" | "sortDir" | "columnWidths">;
|
|
38
42
|
type $$ComponentProps = {
|
|
39
43
|
items?: any[];
|
|
40
44
|
columns?: any[];
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions for exporting table rows (e.g. the current view) to CSV
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Sort rows the way DataTable sorts locally: natural, case-insensitive string
|
|
6
|
+
* comparison, empty values (null/undefined) last in either direction.
|
|
7
|
+
* Returns a new array; the input is not modified.
|
|
8
|
+
* @param {Array<Object>} items
|
|
9
|
+
* @param {string|null|undefined} key - Column key to sort by; falsy keeps the order.
|
|
10
|
+
* @param {string} [dir='asc'] - `'asc'` or `'desc'`.
|
|
11
|
+
* @returns {Array<Object>}
|
|
12
|
+
*/
|
|
13
|
+
export function sortItems(items: Array<any>, key: string | null | undefined, dir?: string): Array<any>;
|
|
14
|
+
/**
|
|
15
|
+
* Plain value of one cell for export: ColumnDef.exportValue when given, else
|
|
16
|
+
* the raw row value.
|
|
17
|
+
* @param {Object} item
|
|
18
|
+
* @param {{key: string, exportValue?: Function}} column
|
|
19
|
+
* @returns {unknown}
|
|
20
|
+
*/
|
|
21
|
+
export function getExportValue(item: any, column: {
|
|
22
|
+
key: string;
|
|
23
|
+
exportValue?: Function;
|
|
24
|
+
}): unknown;
|
|
25
|
+
/**
|
|
26
|
+
* Columns that take part in an export: all but those with `exportable: false`.
|
|
27
|
+
* @param {Array<{key: string, exportable?: boolean}>} columns
|
|
28
|
+
* @returns {Array<Object>}
|
|
29
|
+
*/
|
|
30
|
+
export function getExportColumns(columns: Array<{
|
|
31
|
+
key: string;
|
|
32
|
+
exportable?: boolean;
|
|
33
|
+
}>): Array<any>;
|
|
34
|
+
/**
|
|
35
|
+
* Escape one cell for CSV (RFC 4180): quote it when it holds the delimiter,
|
|
36
|
+
* a quote, a line break or edge whitespace, doubling inner quotes.
|
|
37
|
+
* @param {unknown} value
|
|
38
|
+
* @param {{delimiter?: string, escapeFormulas?: boolean}} [options]
|
|
39
|
+
* @returns {string}
|
|
40
|
+
*/
|
|
41
|
+
export function escapeCSVValue(value: unknown, { delimiter, escapeFormulas }?: {
|
|
42
|
+
delimiter?: string;
|
|
43
|
+
escapeFormulas?: boolean;
|
|
44
|
+
}): string;
|
|
45
|
+
/**
|
|
46
|
+
* Build CSV text from rows and column definitions. The header row uses
|
|
47
|
+
* `exportLabel ?? label ?? key`; columns with `exportable: false` are left out.
|
|
48
|
+
* @param {Array<Object>} rows
|
|
49
|
+
* @param {Array<Object>} columns - ColumnDef[] (only `key`, `label`, `exportLabel`,
|
|
50
|
+
* `exportValue` and `exportable` are read).
|
|
51
|
+
* @param {Object} [options]
|
|
52
|
+
* @param {string} [options.delimiter=','] - Field separator (e.g. `';'` or `'\t'`).
|
|
53
|
+
* @param {boolean} [options.header=true] - Include the header row.
|
|
54
|
+
* @param {string} [options.lineEnding='\r\n']
|
|
55
|
+
* @param {boolean} [options.escapeFormulas=true] - Prefix text starting with
|
|
56
|
+
* `= + - @` (or a tab / carriage return) with `'` so spreadsheets do not run it.
|
|
57
|
+
* @returns {string}
|
|
58
|
+
*/
|
|
59
|
+
export function toCSV(rows: Array<any>, columns: Array<any>, options?: {
|
|
60
|
+
delimiter?: string;
|
|
61
|
+
header?: boolean;
|
|
62
|
+
lineEnding?: string;
|
|
63
|
+
escapeFormulas?: boolean;
|
|
64
|
+
}): string;
|
|
65
|
+
/**
|
|
66
|
+
* Let the browser save CSV text as a file.
|
|
67
|
+
* @param {string} csv
|
|
68
|
+
* @param {string} [filename='export.csv']
|
|
69
|
+
* @param {Object} [options]
|
|
70
|
+
* @param {boolean} [options.bom=true] - Prepend a UTF-8 byte order mark so
|
|
71
|
+
* Excel reads non-ASCII text correctly.
|
|
72
|
+
*/
|
|
73
|
+
export function downloadCSV(csv: string, filename?: string, { bom }?: {
|
|
74
|
+
bom?: boolean;
|
|
75
|
+
}): void;
|
|
76
|
+
/**
|
|
77
|
+
* Build CSV from rows and columns and save it as a file.
|
|
78
|
+
* @param {Array<Object>} rows
|
|
79
|
+
* @param {Array<Object>} columns
|
|
80
|
+
* @param {Object} [options] - `filename` (default `'export.csv'`), `bom`, and the
|
|
81
|
+
* `toCSV` options.
|
|
82
|
+
* @returns {string} The CSV text.
|
|
83
|
+
*/
|
|
84
|
+
export function exportCSV(rows: Array<any>, columns: Array<any>, options?: any): string;
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions for exporting table rows (e.g. the current view) to CSV
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Sort rows the way DataTable sorts locally: natural, case-insensitive string
|
|
7
|
+
* comparison, empty values (null/undefined) last in either direction.
|
|
8
|
+
* Returns a new array; the input is not modified.
|
|
9
|
+
* @param {Array<Object>} items
|
|
10
|
+
* @param {string|null|undefined} key - Column key to sort by; falsy keeps the order.
|
|
11
|
+
* @param {string} [dir='asc'] - `'asc'` or `'desc'`.
|
|
12
|
+
* @returns {Array<Object>}
|
|
13
|
+
*/
|
|
14
|
+
export function sortItems(items, key, dir = 'asc') {
|
|
15
|
+
const rows = Array.isArray(items) ? [...items] : [];
|
|
16
|
+
if (!key) return rows;
|
|
17
|
+
return rows.sort((a, b) => {
|
|
18
|
+
const aVal = a?.[key];
|
|
19
|
+
const bVal = b?.[key];
|
|
20
|
+
if (aVal == null && bVal == null) return 0;
|
|
21
|
+
if (aVal == null) return 1;
|
|
22
|
+
if (bVal == null) return -1;
|
|
23
|
+
const comparison = String(aVal).localeCompare(String(bVal), undefined, {
|
|
24
|
+
numeric: true,
|
|
25
|
+
sensitivity: 'base',
|
|
26
|
+
});
|
|
27
|
+
return dir === 'desc' ? -comparison : comparison;
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Plain value of one cell for export: ColumnDef.exportValue when given, else
|
|
33
|
+
* the raw row value.
|
|
34
|
+
* @param {Object} item
|
|
35
|
+
* @param {{key: string, exportValue?: Function}} column
|
|
36
|
+
* @returns {unknown}
|
|
37
|
+
*/
|
|
38
|
+
export function getExportValue(item, column) {
|
|
39
|
+
const value = item?.[column.key];
|
|
40
|
+
return typeof column.exportValue === 'function' ? column.exportValue(item, value) : value;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Columns that take part in an export: all but those with `exportable: false`.
|
|
45
|
+
* @param {Array<{key: string, exportable?: boolean}>} columns
|
|
46
|
+
* @returns {Array<Object>}
|
|
47
|
+
*/
|
|
48
|
+
export function getExportColumns(columns) {
|
|
49
|
+
return (Array.isArray(columns) ? columns : []).filter((c) => c && c.exportable !== false);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Turn a cell value into text: empty for null/undefined, ISO for dates,
|
|
54
|
+
* '; '-joined for arrays, JSON for other objects.
|
|
55
|
+
* @param {unknown} value
|
|
56
|
+
* @returns {string}
|
|
57
|
+
*/
|
|
58
|
+
function stringifyValue(value) {
|
|
59
|
+
if (value == null) return '';
|
|
60
|
+
if (value instanceof Date) return Number.isNaN(value.getTime()) ? '' : value.toISOString();
|
|
61
|
+
if (Array.isArray(value)) return value.map(stringifyValue).join('; ');
|
|
62
|
+
if (typeof value === 'object') return JSON.stringify(value);
|
|
63
|
+
return String(value);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Byte order mark that tells Excel the file is UTF-8 (written as a code point:
|
|
67
|
+
// formatters turn a \uFEFF escape into an invisible literal).
|
|
68
|
+
const UTF8_BOM = String.fromCharCode(0xfeff);
|
|
69
|
+
|
|
70
|
+
// Text a spreadsheet would run as a formula (CSV injection).
|
|
71
|
+
const FORMULA_START = /^[=+\-@\t\r]/;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Escape one cell for CSV (RFC 4180): quote it when it holds the delimiter,
|
|
75
|
+
* a quote, a line break or edge whitespace, doubling inner quotes.
|
|
76
|
+
* @param {unknown} value
|
|
77
|
+
* @param {{delimiter?: string, escapeFormulas?: boolean}} [options]
|
|
78
|
+
* @returns {string}
|
|
79
|
+
*/
|
|
80
|
+
export function escapeCSVValue(value, { delimiter = ',', escapeFormulas = true } = {}) {
|
|
81
|
+
let text = stringifyValue(value);
|
|
82
|
+
// Only text values: a negative number is data, not a formula.
|
|
83
|
+
if (escapeFormulas && typeof value === 'string' && FORMULA_START.test(text)) {
|
|
84
|
+
text = `'${text}`;
|
|
85
|
+
}
|
|
86
|
+
const needsQuotes =
|
|
87
|
+
text.includes(delimiter) || text.includes('"') || /[\r\n]/.test(text) || text !== text.trim();
|
|
88
|
+
return needsQuotes ? `"${text.replace(/"/g, '""')}"` : text;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Build CSV text from rows and column definitions. The header row uses
|
|
93
|
+
* `exportLabel ?? label ?? key`; columns with `exportable: false` are left out.
|
|
94
|
+
* @param {Array<Object>} rows
|
|
95
|
+
* @param {Array<Object>} columns - ColumnDef[] (only `key`, `label`, `exportLabel`,
|
|
96
|
+
* `exportValue` and `exportable` are read).
|
|
97
|
+
* @param {Object} [options]
|
|
98
|
+
* @param {string} [options.delimiter=','] - Field separator (e.g. `';'` or `'\t'`).
|
|
99
|
+
* @param {boolean} [options.header=true] - Include the header row.
|
|
100
|
+
* @param {string} [options.lineEnding='\r\n']
|
|
101
|
+
* @param {boolean} [options.escapeFormulas=true] - Prefix text starting with
|
|
102
|
+
* `= + - @` (or a tab / carriage return) with `'` so spreadsheets do not run it.
|
|
103
|
+
* @returns {string}
|
|
104
|
+
*/
|
|
105
|
+
export function toCSV(rows, columns, options = {}) {
|
|
106
|
+
const { delimiter = ',', header = true, lineEnding = '\r\n', escapeFormulas = true } = options;
|
|
107
|
+
const cols = getExportColumns(columns);
|
|
108
|
+
const cell = (value) => escapeCSVValue(value, { delimiter, escapeFormulas });
|
|
109
|
+
const lines = [];
|
|
110
|
+
if (header) {
|
|
111
|
+
lines.push(cols.map((c) => cell(String(c.exportLabel ?? c.label ?? c.key))).join(delimiter));
|
|
112
|
+
}
|
|
113
|
+
for (const item of Array.isArray(rows) ? rows : []) {
|
|
114
|
+
lines.push(cols.map((c) => cell(getExportValue(item, c))).join(delimiter));
|
|
115
|
+
}
|
|
116
|
+
return lines.join(lineEnding);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Let the browser save CSV text as a file.
|
|
121
|
+
* @param {string} csv
|
|
122
|
+
* @param {string} [filename='export.csv']
|
|
123
|
+
* @param {Object} [options]
|
|
124
|
+
* @param {boolean} [options.bom=true] - Prepend a UTF-8 byte order mark so
|
|
125
|
+
* Excel reads non-ASCII text correctly.
|
|
126
|
+
*/
|
|
127
|
+
export function downloadCSV(csv, filename = 'export.csv', { bom = true } = {}) {
|
|
128
|
+
if (typeof document === 'undefined') throw new Error('downloadCSV needs a browser document');
|
|
129
|
+
const blob = new Blob([bom ? UTF8_BOM : '', csv], { type: 'text/csv;charset=utf-8' });
|
|
130
|
+
const url = URL.createObjectURL(blob);
|
|
131
|
+
const link = document.createElement('a');
|
|
132
|
+
link.href = url;
|
|
133
|
+
link.download = filename;
|
|
134
|
+
link.style.display = 'none';
|
|
135
|
+
document.body.appendChild(link);
|
|
136
|
+
link.click();
|
|
137
|
+
link.remove();
|
|
138
|
+
// Revoke after the click has been handled, or some browsers cancel the download.
|
|
139
|
+
setTimeout(() => URL.revokeObjectURL(url), 0);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Build CSV from rows and columns and save it as a file.
|
|
144
|
+
* @param {Array<Object>} rows
|
|
145
|
+
* @param {Array<Object>} columns
|
|
146
|
+
* @param {Object} [options] - `filename` (default `'export.csv'`), `bom`, and the
|
|
147
|
+
* `toCSV` options.
|
|
148
|
+
* @returns {string} The CSV text.
|
|
149
|
+
*/
|
|
150
|
+
export function exportCSV(rows, columns, options = {}) {
|
|
151
|
+
const { filename = 'export.csv', bom = true, ...csvOptions } = options;
|
|
152
|
+
const csv = toCSV(rows, columns, csvOptions);
|
|
153
|
+
downloadCSV(csv, filename, { bom });
|
|
154
|
+
return csv;
|
|
155
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -77,6 +77,21 @@ export type ColumnDef<T extends Record<string, unknown> = Record<string, unknown
|
|
|
77
77
|
sortKey: string | null;
|
|
78
78
|
sortDir: string;
|
|
79
79
|
}]>;
|
|
80
|
+
/**
|
|
81
|
+
* - Set `false` to leave the column out of CSV
|
|
82
|
+
* exports (e.g. an actions column). Defaults to `true`.
|
|
83
|
+
*/
|
|
84
|
+
exportable?: boolean;
|
|
85
|
+
/**
|
|
86
|
+
* - CSV header for the column. Defaults to `label`.
|
|
87
|
+
*/
|
|
88
|
+
exportLabel?: string;
|
|
89
|
+
/**
|
|
90
|
+
* - Plain value
|
|
91
|
+
* written to CSV for this column (a `cellSnippet` cannot be exported).
|
|
92
|
+
* Defaults to the raw row value.
|
|
93
|
+
*/
|
|
94
|
+
exportValue?: (item: T, value: unknown) => unknown;
|
|
80
95
|
/**
|
|
81
96
|
* Custom Svelte 5 snippet rendered inside each `<td>` for this column.
|
|
82
97
|
* Receives `{ item, value, index }`. When omitted the raw value is rendered.
|
|
@@ -88,3 +103,4 @@ export type ColumnDef<T extends Record<string, unknown> = Record<string, unknown
|
|
|
88
103
|
}]>;
|
|
89
104
|
};
|
|
90
105
|
export { DEFAULT_RELATIVE_RANGE_PRESETS, getFilterType, filtersWithoutKey, resolveColumnFilters, getUniqueValuesWithCounts, buildColumnFilters, applyFilters, matchesDateRange, isDateRangeColumn, hasActiveFilterValue, countActiveFilterValue, hasActiveFilters, countActiveFilters, clearAllFilters, filtersToSearchParams, searchParamsToFilters } from "./filterUtils.js";
|
|
106
|
+
export { sortItems, getExportColumns, getExportValue, escapeCSVValue, toCSV, downloadCSV, exportCSV } from "./exportUtils.js";
|
package/dist/index.js
CHANGED
|
@@ -29,6 +29,17 @@ export {
|
|
|
29
29
|
searchParamsToFilters,
|
|
30
30
|
} from './filterUtils.js';
|
|
31
31
|
|
|
32
|
+
// Export utility functions (CSV)
|
|
33
|
+
export {
|
|
34
|
+
sortItems,
|
|
35
|
+
getExportColumns,
|
|
36
|
+
getExportValue,
|
|
37
|
+
escapeCSVValue,
|
|
38
|
+
toCSV,
|
|
39
|
+
downloadCSV,
|
|
40
|
+
exportCSV,
|
|
41
|
+
} from './exportUtils.js';
|
|
42
|
+
|
|
32
43
|
/**
|
|
33
44
|
* Unified column definition for DataTable.
|
|
34
45
|
* Pass an array of `ColumnDef` objects as the `columns` prop to configure
|
|
@@ -65,6 +76,12 @@ export {
|
|
|
65
76
|
* @property {import('svelte').Snippet<[{key: string, label: string, sortKey: string|null, sortDir: string}]>} [headerSnippet]
|
|
66
77
|
* Custom Svelte 5 snippet rendered inside the `<th>`. Receives
|
|
67
78
|
* `{ key, label, sortKey, sortDir }`.
|
|
79
|
+
* @property {boolean} [exportable] - Set `false` to leave the column out of CSV
|
|
80
|
+
* exports (e.g. an actions column). Defaults to `true`.
|
|
81
|
+
* @property {string} [exportLabel] - CSV header for the column. Defaults to `label`.
|
|
82
|
+
* @property {(item: T, value: unknown) => unknown} [exportValue] - Plain value
|
|
83
|
+
* written to CSV for this column (a `cellSnippet` cannot be exported).
|
|
84
|
+
* Defaults to the raw row value.
|
|
68
85
|
* @property {import('svelte').Snippet<[{item: T, value: unknown, index: number}]>} [cellSnippet]
|
|
69
86
|
* Custom Svelte 5 snippet rendered inside each `<td>` for this column.
|
|
70
87
|
* Receives `{ item, value, index }`. When omitted the raw value is rendered.
|