compote-ui 0.84.0 → 0.85.0
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/dist/components/data-table-v9/toolbar/data-table-export.svelte +146 -146
- package/dist/components/data-table-v9/xlsx/index.d.ts +5 -0
- package/dist/components/data-table-v9/xlsx/index.js +29 -5
- package/dist/icons/PhDownloadSimple.svelte +18 -18
- package/package.json +129 -129
- package/skills/data-table/SKILL.md +400 -400
- package/skills/data-table/references/column-options.md +181 -181
- package/skills/data-table/references/export.md +160 -158
|
@@ -1,158 +1,160 @@
|
|
|
1
|
-
# Export
|
|
2
|
-
|
|
3
|
-
`DataTable.Export` downloads the table as a file. It sits in the toolbar like `ColumnVisibility`
|
|
4
|
-
and takes the `table`.
|
|
5
|
-
|
|
6
|
-
```svelte
|
|
7
|
-
<script lang="ts">
|
|
8
|
-
import * as DataTable from 'compote-ui/data-table';
|
|
9
|
-
</script>
|
|
10
|
-
|
|
11
|
-
<DataTable.Toolbar>
|
|
12
|
-
{#snippet right()}
|
|
13
|
-
<DataTable.Export {table} filename="invoices" />
|
|
14
|
-
<DataTable.ColumnVisibility {table} />
|
|
15
|
-
{/snippet}
|
|
16
|
-
</DataTable.Toolbar>
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
Props:
|
|
20
|
-
|
|
21
|
-
- `table: DataTableInstance<T>`
|
|
22
|
-
- `exporters?: DataTableExporter[]` — defaults to `[csvExporter]`.
|
|
23
|
-
- `filename?: string` — without extension, defaults to `'export'`.
|
|
24
|
-
- `triggerLabel?: string` — icon-only with a tooltip when omitted.
|
|
25
|
-
|
|
26
|
-
One exporter and no selected rows renders a plain button. More exporters, or any selected row,
|
|
27
|
-
renders a menu. When rows are selected the menu offers **All rows (n)** / **Selected rows (k)**.
|
|
28
|
-
`k` counts the selected rows the current filter keeps, exactly what gets exported.
|
|
29
|
-
|
|
30
|
-
## What gets exported
|
|
31
|
-
|
|
32
|
-
- **Columns:** the visible ones, in the order the table shows them (pinned left, center, pinned
|
|
33
|
-
right). Columns with `enableExport: false` are skipped, and so are `type: 'action'` columns by
|
|
34
|
-
default. A grouped column's header is `Group / Column`.
|
|
35
|
-
- **Rows:** the filtered and sorted rows (`table.getRowModel()`). The table has no pagination,
|
|
36
|
-
so this is every matching row, not one page.
|
|
37
|
-
- **Cells:** the text the table shows (`cell`, else type formatting; `boolean`, `url` and
|
|
38
|
-
`phone` as the table draws them, which ignores `cell`). Empty cells are blank, not `-`. A
|
|
39
|
-
column with `exportValue` and no `cell` formats `exportValue` output by its `type`.
|
|
40
|
-
|
|
41
|
-
## CSV
|
|
42
|
-
|
|
43
|
-
`csvExporter` writes a UTF-8 BOM (so Excel reads č/ć/š/đ/ž correctly), `;` as the separator
|
|
44
|
-
(what Excel expects under most European locales), and CRLF line endings. Fields starting with
|
|
45
|
-
`= + - @`, tab or CR get a `'` prefix so spreadsheets don't run them as formulas. Exempt, so
|
|
46
|
-
negative numbers and phone numbers stay as they are: text made only of digits, spaces and number
|
|
47
|
-
punctuation (`-5`, `+381 60 123`), and number, currency and percent columns without their own
|
|
48
|
-
`cell`.
|
|
49
|
-
|
|
50
|
-
```ts
|
|
51
|
-
import { createCsvExporter } from 'compote-ui/data-table';
|
|
52
|
-
|
|
53
|
-
const commaCsv = createCsvExporter({ id: 'csv-comma', label: 'CSV (comma)', separator: ',' });
|
|
54
|
-
// Also: bom?: boolean (default true)
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
CSV holds the formatted text (`1,234.56 RSD`), so a spreadsheet can't sum it.
|
|
58
|
-
|
|
59
|
-
## Excel (.xlsx)
|
|
60
|
-
|
|
61
|
-
`xlsxExporter` lives in its own subpath and needs the optional peer `write-excel-file`
|
|
62
|
-
(about 19 KB gzip, loaded only when an export runs, so importing the subpath is SSR-safe):
|
|
63
|
-
|
|
64
|
-
```sh
|
|
65
|
-
bun add write-excel-file
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
```svelte
|
|
69
|
-
<script lang="ts">
|
|
70
|
-
import * as DataTable from 'compote-ui/data-table';
|
|
71
|
-
import { xlsxExporter } from 'compote-ui/data-table/xlsx';
|
|
72
|
-
</script>
|
|
73
|
-
|
|
74
|
-
<DataTable.Export {table} filename="invoices" exporters={[DataTable.csvExporter, xlsxExporter]} />
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
Cells stay computable in Excel:
|
|
78
|
-
|
|
79
|
-
- `number`, `currency`, `percent` → numbers, also from numeric strings (`"1234.56"`), with an
|
|
80
|
-
Excel format built from the column's Intl options: grouping, the decimals Intl would show,
|
|
81
|
-
`%`, and the currency code (`#,##0.00 "USD"`). Options Excel can't express (`notation`,
|
|
82
|
-
significant digits, `signDisplay`, `currencyDisplay: 'name'`, …) fall back to the text.
|
|
83
|
-
- `date`, `time`, `date-time` → Excel dates, on the day and time the table shows in the column's
|
|
84
|
-
zone (`formatOptions.timeZone`, else local). A `date` column drops the time; a `time` column
|
|
85
|
-
keeps only the time of day, so it sorts by time. Formats default to `dd.mm.yyyy`, `hh:mm` and
|
|
86
|
-
`dd.mm.yyyy hh:mm`.
|
|
87
|
-
- `boolean` → TRUE/FALSE for real `true`/`false`; other values are blank, as the table shows
|
|
88
|
-
`-` for them.
|
|
89
|
-
- Everything else, and any column with its own `cell` but no `exportValue`, is the text the
|
|
90
|
-
table shows. Typed cells can't hold formulas, so there is no `'` prefix.
|
|
91
|
-
|
|
92
|
-
The header row is bold and frozen
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
1
|
+
# Export
|
|
2
|
+
|
|
3
|
+
`DataTable.Export` downloads the table as a file. It sits in the toolbar like `ColumnVisibility`
|
|
4
|
+
and takes the `table`.
|
|
5
|
+
|
|
6
|
+
```svelte
|
|
7
|
+
<script lang="ts">
|
|
8
|
+
import * as DataTable from 'compote-ui/data-table';
|
|
9
|
+
</script>
|
|
10
|
+
|
|
11
|
+
<DataTable.Toolbar>
|
|
12
|
+
{#snippet right()}
|
|
13
|
+
<DataTable.Export {table} filename="invoices" />
|
|
14
|
+
<DataTable.ColumnVisibility {table} />
|
|
15
|
+
{/snippet}
|
|
16
|
+
</DataTable.Toolbar>
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Props:
|
|
20
|
+
|
|
21
|
+
- `table: DataTableInstance<T>`
|
|
22
|
+
- `exporters?: DataTableExporter[]` — defaults to `[csvExporter]`.
|
|
23
|
+
- `filename?: string` — without extension, defaults to `'export'`.
|
|
24
|
+
- `triggerLabel?: string` — icon-only with a tooltip when omitted.
|
|
25
|
+
|
|
26
|
+
One exporter and no selected rows renders a plain button. More exporters, or any selected row,
|
|
27
|
+
renders a menu. When rows are selected the menu offers **All rows (n)** / **Selected rows (k)**.
|
|
28
|
+
`k` counts the selected rows the current filter keeps, exactly what gets exported.
|
|
29
|
+
|
|
30
|
+
## What gets exported
|
|
31
|
+
|
|
32
|
+
- **Columns:** the visible ones, in the order the table shows them (pinned left, center, pinned
|
|
33
|
+
right). Columns with `enableExport: false` are skipped, and so are `type: 'action'` columns by
|
|
34
|
+
default. A grouped column's header is `Group / Column`.
|
|
35
|
+
- **Rows:** the filtered and sorted rows (`table.getRowModel()`). The table has no pagination,
|
|
36
|
+
so this is every matching row, not one page.
|
|
37
|
+
- **Cells:** the text the table shows (`cell`, else type formatting; `boolean`, `url` and
|
|
38
|
+
`phone` as the table draws them, which ignores `cell`). Empty cells are blank, not `-`. A
|
|
39
|
+
column with `exportValue` and no `cell` formats `exportValue` output by its `type`.
|
|
40
|
+
|
|
41
|
+
## CSV
|
|
42
|
+
|
|
43
|
+
`csvExporter` writes a UTF-8 BOM (so Excel reads č/ć/š/đ/ž correctly), `;` as the separator
|
|
44
|
+
(what Excel expects under most European locales), and CRLF line endings. Fields starting with
|
|
45
|
+
`= + - @`, tab or CR get a `'` prefix so spreadsheets don't run them as formulas. Exempt, so
|
|
46
|
+
negative numbers and phone numbers stay as they are: text made only of digits, spaces and number
|
|
47
|
+
punctuation (`-5`, `+381 60 123`), and number, currency and percent columns without their own
|
|
48
|
+
`cell`.
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
import { createCsvExporter } from 'compote-ui/data-table';
|
|
52
|
+
|
|
53
|
+
const commaCsv = createCsvExporter({ id: 'csv-comma', label: 'CSV (comma)', separator: ',' });
|
|
54
|
+
// Also: bom?: boolean (default true)
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
CSV holds the formatted text (`1,234.56 RSD`), so a spreadsheet can't sum it.
|
|
58
|
+
|
|
59
|
+
## Excel (.xlsx)
|
|
60
|
+
|
|
61
|
+
`xlsxExporter` lives in its own subpath and needs the optional peer `write-excel-file`
|
|
62
|
+
(about 19 KB gzip, loaded only when an export runs, so importing the subpath is SSR-safe):
|
|
63
|
+
|
|
64
|
+
```sh
|
|
65
|
+
bun add write-excel-file
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
```svelte
|
|
69
|
+
<script lang="ts">
|
|
70
|
+
import * as DataTable from 'compote-ui/data-table';
|
|
71
|
+
import { xlsxExporter } from 'compote-ui/data-table/xlsx';
|
|
72
|
+
</script>
|
|
73
|
+
|
|
74
|
+
<DataTable.Export {table} filename="invoices" exporters={[DataTable.csvExporter, xlsxExporter]} />
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Cells stay computable in Excel:
|
|
78
|
+
|
|
79
|
+
- `number`, `currency`, `percent` → numbers, also from numeric strings (`"1234.56"`), with an
|
|
80
|
+
Excel format built from the column's Intl options: grouping, the decimals Intl would show,
|
|
81
|
+
`%`, and the currency code (`#,##0.00 "USD"`). Options Excel can't express (`notation`,
|
|
82
|
+
significant digits, `signDisplay`, `currencyDisplay: 'name'`, …) fall back to the text.
|
|
83
|
+
- `date`, `time`, `date-time` → Excel dates, on the day and time the table shows in the column's
|
|
84
|
+
zone (`formatOptions.timeZone`, else local). A `date` column drops the time; a `time` column
|
|
85
|
+
keeps only the time of day, so it sorts by time. Formats default to `dd.mm.yyyy`, `hh:mm` and
|
|
86
|
+
`dd.mm.yyyy hh:mm`.
|
|
87
|
+
- `boolean` → TRUE/FALSE for real `true`/`false`; other values are blank, as the table shows
|
|
88
|
+
`-` for them.
|
|
89
|
+
- Everything else, and any column with its own `cell` but no `exportValue`, is the text the
|
|
90
|
+
table shows. Typed cells can't hold formulas, so there is no `'` prefix.
|
|
91
|
+
|
|
92
|
+
The header row is bold and frozen. Each column is as wide as its header or its longest cell
|
|
93
|
+
text (longest line for multi-line text) plus 1 character, between 8 and 60
|
|
94
|
+
(`xlsxColumnWidths(data)` computes them).
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
import { createXlsxExporter } from 'compote-ui/data-table/xlsx';
|
|
98
|
+
|
|
99
|
+
const excel = createXlsxExporter({
|
|
100
|
+
sheet: 'Invoices',
|
|
101
|
+
date: 'yyyy-mm-dd',
|
|
102
|
+
dateTime: 'yyyy-mm-dd hh:mm'
|
|
103
|
+
// also: time, id, label
|
|
104
|
+
});
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
`exportDataToXlsxBlob(data, options)` returns the file as a `Blob` (e.g. to upload instead of
|
|
108
|
+
download), and `toXlsxCell` maps one cell.
|
|
109
|
+
|
|
110
|
+
## Custom exporter
|
|
111
|
+
|
|
112
|
+
An exporter is `{ id, label, icon?, export(context) }`. `context` holds `table`, `scope`
|
|
113
|
+
(`'filtered' | 'selected'`), `filename` and `getData()`, which returns `{ columns, rows }`. Each
|
|
114
|
+
cell is `{ value, text }`. If `export` returns a promise, the trigger stays busy until it
|
|
115
|
+
settles. Errors are not caught.
|
|
116
|
+
|
|
117
|
+
Export on the server, from the table state:
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
import type { DataTableExporter } from 'compote-ui/data-table';
|
|
121
|
+
|
|
122
|
+
const serverExcel: DataTableExporter = {
|
|
123
|
+
id: 'server-xlsx',
|
|
124
|
+
label: 'Excel',
|
|
125
|
+
export: async ({ table }) => {
|
|
126
|
+
const url = await requestExport({
|
|
127
|
+
sorting: table.atoms.sorting.get(),
|
|
128
|
+
columnFilters: table.atoms.columnFilters.get(),
|
|
129
|
+
globalFilter: table.atoms.globalFilter.get(),
|
|
130
|
+
columns: table.getVisibleLeafColumns().map((column) => column.id)
|
|
131
|
+
});
|
|
132
|
+
window.location.assign(url);
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Use a library the app already has instead of `write-excel-file` (SheetJS here):
|
|
138
|
+
|
|
139
|
+
```ts
|
|
140
|
+
import { utils, writeFileXLSX } from 'xlsx';
|
|
141
|
+
import type { DataTableExporter } from 'compote-ui/data-table';
|
|
142
|
+
|
|
143
|
+
const sheetJs: DataTableExporter = {
|
|
144
|
+
id: 'xlsx',
|
|
145
|
+
label: 'Excel',
|
|
146
|
+
export: ({ filename, getData }) => {
|
|
147
|
+
const { columns, rows } = getData();
|
|
148
|
+
const sheet = utils.aoa_to_sheet([
|
|
149
|
+
columns.map((column) => column.header),
|
|
150
|
+
...rows.map((cells) => cells.map((cell) => cell.text))
|
|
151
|
+
]);
|
|
152
|
+
const book = utils.book_new();
|
|
153
|
+
utils.book_append_sheet(book, sheet, 'Sheet1');
|
|
154
|
+
writeFileXLSX(book, `${filename}.xlsx`);
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
`getExportData(table, scope)`, `getExportRows(table, scope)` and `getExportColumns(table)` are
|
|
160
|
+
exported for building export UI outside `DataTable.Export`.
|