-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from bryanmylee/plugin/addDataExport
addDataExport
- Loading branch information
Showing
6 changed files
with
234 additions
and
10 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
docs/src/routes/docs/[...3]plugins/[...14]add-data-export.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
--- | ||
title: addDataExport | ||
description: Export the transformed table as a new dataset. | ||
sidebar_title: addDataExport | ||
--- | ||
|
||
<script> | ||
import { useHljs } from '$lib/utils/useHljs'; | ||
useHljs('ts'); | ||
</script> | ||
|
||
# {$frontmatter.title} | ||
|
||
`addDataExport` allows for reading the data source as it is currently transformed by the table. | ||
|
||
This is useful if you need to export data from the table with all plugin transformations applied. | ||
|
||
## Options | ||
|
||
:::callout | ||
Options passed into `addDataExport`. | ||
::: | ||
|
||
```ts {3} | ||
const table = createTable(data, { | ||
export: addDataExport({ ... }), | ||
}); | ||
``` | ||
|
||
### `format?: 'object' | 'json' | 'csv'` | ||
|
||
Sets the exported data format. | ||
|
||
_Defaults to `'object'`_. | ||
|
||
### `childrenKey?: string` | ||
|
||
The property key to store sub-rows under. | ||
|
||
This only applies if `format` is `'object'` or `'json'`. | ||
|
||
_Defaults to `'children'`_. | ||
|
||
## Column Options | ||
|
||
:::callout | ||
Options passed into column definitions. | ||
::: | ||
|
||
```ts {7} | ||
const columns = table.createColumns([ | ||
table.column({ | ||
header: 'Name', | ||
accessor: 'name', | ||
plugins: { | ||
export: { ... }, | ||
}, | ||
}), | ||
]); | ||
``` | ||
|
||
### `exclude?: boolean` | ||
|
||
Excludes the column from the data export. | ||
|
||
_Defaults to `false`_. | ||
|
||
## Prop Set | ||
|
||
:::callout | ||
Extensions to the view model. | ||
|
||
Subscribe to `.props()` on the respective table components. | ||
::: | ||
|
||
```svelte | ||
{#each $headerRows as headerRow (headerRow.id)} | ||
<Subscribe rowProps={headerRow.props()} let:rowProps> | ||
{rowProps.export} <!-- HeaderRow props --> | ||
{#each headerRow.cells as cell (cell.id)} | ||
<Subscribe props={cell.props()} let:props> | ||
{props.export} <!-- HeaderCell props --> | ||
</Subscribe> | ||
{/each} | ||
</Subscribe> | ||
{/each} | ||
``` | ||
|
||
_Nothing here so far_. | ||
|
||
## Plugin State | ||
|
||
:::callout | ||
State provided by `addDataExport`. | ||
::: | ||
|
||
```ts {3} | ||
const { headerRows, rows, pluginStates } = table.createViewModel(columns); | ||
const { ... } = pluginStates.export; | ||
``` | ||
|
||
### `exportedData: Readable<DataExport>` | ||
|
||
The exported data. `DataExport` is: | ||
|
||
- `Record<string, unknown>[]` if `format` is `'object'`, | ||
- `string` if `format` is `'json'`, | ||
- `string` if `format` is `'csv'`, | ||
|
||
Either subscribe to `exportedData` or use [`get`](https://svelte.dev/docs#run-time-svelte-store-get) to compute the exported data once. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { DataBodyCell } from '$lib/bodyCells'; | ||
import type { BodyRow } from '$lib/bodyRows'; | ||
import type { TablePlugin } from '$lib/types/TablePlugin'; | ||
import { derived, type Readable } from 'svelte/store'; | ||
|
||
export type DataExportFormat = 'object' | 'json' | 'csv'; | ||
type ExportForFormat = { | ||
object: Record<string, unknown>[]; | ||
json: string; | ||
csv: string; | ||
}; | ||
export type DataExport<F extends DataExportFormat> = ExportForFormat[F]; | ||
|
||
export interface DataExportConfig<F extends DataExportFormat> { | ||
childrenKey?: string; | ||
format?: F; | ||
} | ||
|
||
export interface DataExportState<F extends DataExportFormat> { | ||
exportedData: Readable<DataExport<F>>; | ||
} | ||
|
||
export interface DataExportColumnOptions { | ||
exclude?: boolean; | ||
} | ||
|
||
const getObjectsFromRows = <Item>( | ||
rows: BodyRow<Item>[], | ||
ids: string[], | ||
childrenKey: string | ||
): Record<string, unknown>[] => { | ||
return rows.map((row) => { | ||
const dataObject = Object.fromEntries( | ||
ids.map((id) => { | ||
const cell = row.cellForId[id]; | ||
if (cell instanceof DataBodyCell) { | ||
return [id, cell.value]; | ||
} | ||
return [id, null]; | ||
}) | ||
); | ||
if (row.subRows !== undefined) { | ||
dataObject[childrenKey] = getObjectsFromRows(row.subRows, ids, childrenKey); | ||
} | ||
return dataObject; | ||
}); | ||
}; | ||
|
||
const getCsvFromRows = <Item>(rows: BodyRow<Item>[], ids: string[]): string => { | ||
const dataLines = rows.map((row) => { | ||
const line = ids.map((id) => { | ||
const cell = row.cellForId[id]; | ||
if (cell instanceof DataBodyCell) { | ||
return cell.value; | ||
} | ||
return null; | ||
}); | ||
return line.join(','); | ||
}); | ||
const headerLine = ids.join(','); | ||
return headerLine + '\n' + dataLines.join('\n'); | ||
}; | ||
|
||
export const addDataExport = | ||
<Item, F extends DataExportFormat = 'object'>({ | ||
format = 'object' as F, | ||
childrenKey = 'children', | ||
}: DataExportConfig<F> = {}): TablePlugin<Item, DataExportState<F>, DataExportColumnOptions> => | ||
({ tableState, columnOptions }) => { | ||
const excludedIds = Object.entries(columnOptions) | ||
.filter(([, option]) => option.exclude === true) | ||
.map(([columnId]) => columnId); | ||
|
||
const { visibleColumns, rows } = tableState; | ||
|
||
const exportedIds = derived(visibleColumns, ($visibleColumns) => | ||
$visibleColumns.map((c) => c.id).filter((id) => !excludedIds.includes(id)) | ||
); | ||
|
||
const exportedData = derived([rows, exportedIds], ([$rows, $exportedIds]) => { | ||
switch (format) { | ||
case 'json': | ||
return JSON.stringify( | ||
getObjectsFromRows($rows, $exportedIds, childrenKey) | ||
) as DataExport<F>; | ||
case 'csv': | ||
return getCsvFromRows($rows, $exportedIds) as DataExport<F>; | ||
default: | ||
return getObjectsFromRows($rows, $exportedIds, childrenKey) as DataExport<F>; | ||
} | ||
}); | ||
|
||
const pluginState: DataExportState<F> = { exportedData }; | ||
|
||
return { | ||
pluginState, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79b44ec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
svelte-headless-table – ./
svelte-headless-table-bryanmylee.vercel.app
svelte-headless-table-bryanmylee-com.vercel.app
svelte-headless-table-git-main-bryanmylee.vercel.app
svelte-headless-table.bryanmylee.com