|
1 | 1 | <script lang="ts"> |
| 2 | + import { createEventDispatcher } from 'svelte'; |
2 | 3 | import { createTable, Subscribe, Render, createRender } from 'svelte-headless-table'; |
3 | 4 | import { |
4 | 5 | addSortBy, |
|
25 | 26 |
|
26 | 27 | type AccessorType = keyof (typeof $data)[0]; |
27 | 28 |
|
| 29 | + const dispatch = createEventDispatcher(); |
| 30 | + const actionDispatcher = (obj) => dispatch('action', obj); |
| 31 | +
|
28 | 32 | const table = createTable(data, { |
29 | 33 | colFilter: addColumnFilters(), |
30 | 34 | tableFilter: addTableFilter({ |
|
35 | 39 | expand: addExpandedRows() |
36 | 40 | }); |
37 | 41 |
|
38 | | - const accessors: AccessorType[] = Object.keys($data[0]) as AccessorType[]; |
| 42 | + const accessors: AccessorType[] = |
| 43 | + $data.length > 0 ? (Object.keys($data[0]) as AccessorType[]) : []; |
39 | 44 |
|
40 | 45 | const tableColumns = [ |
41 | 46 | ...accessors |
|
49 | 54 | .map((accessor) => { |
50 | 55 | const key = accessor as string; |
51 | 56 | if (columns !== undefined && key in columns) { |
52 | | - const { header, colFilterFn, colFilterComponent } = columns[key]; |
| 57 | + const { |
| 58 | + header, |
| 59 | + colFilterFn, |
| 60 | + colFilterComponent, |
| 61 | + instructions, |
| 62 | + disableFiltering = false, |
| 63 | + disableSorting = false |
| 64 | + } = columns[key]; |
| 65 | +
|
| 66 | + const { toSortableValueFn, toFilterableValueFn, toStringFn } = instructions ?? {}; |
| 67 | +
|
53 | 68 | return table.column({ |
54 | 69 | header: header ?? key, |
55 | 70 | accessor: accessor, |
| 71 | + cell: ({ value }) => { |
| 72 | + return toStringFn ? toStringFn(value) : value; |
| 73 | + }, |
56 | 74 | plugins: { |
57 | | - sort: { invert: true }, |
58 | | - colFilter: { |
59 | | - fn: colFilterFn ?? columnFilter, |
60 | | - render: ({ filterValue, values, id }) => |
61 | | - createRender(colFilterComponent ?? TableFilter, { |
62 | | - filterValue, |
63 | | - values, |
64 | | - id, |
65 | | - tableId |
66 | | - }) |
| 75 | + sort: { |
| 76 | + disable: disableSorting, |
| 77 | + invert: true, |
| 78 | + getSortValue: (row) => { |
| 79 | + return toSortableValueFn ? toSortableValueFn(row) : row; |
| 80 | + } |
| 81 | + }, |
| 82 | + colFilter: !disableFiltering |
| 83 | + ? { |
| 84 | + fn: ({ filterValue, value }) => { |
| 85 | + const val = toFilterableValueFn ? toFilterableValueFn(value) : value; |
| 86 | +
|
| 87 | + return colFilterFn |
| 88 | + ? colFilterFn({ filterValue, value: val }) |
| 89 | + : columnFilter({ filterValue, value: val }); |
| 90 | + }, |
| 91 | + render: ({ filterValue, values, id }) => { |
| 92 | + return createRender(colFilterComponent ?? TableFilter, { |
| 93 | + filterValue, |
| 94 | + id, |
| 95 | + tableId, |
| 96 | + values |
| 97 | + }); |
| 98 | + } |
| 99 | + } |
| 100 | + : undefined, |
| 101 | + tableFilter: { |
| 102 | + getFilterValue: (row) => { |
| 103 | + return toStringFn ? toStringFn(row) : row; |
| 104 | + } |
67 | 105 | } |
68 | 106 | } |
69 | 107 | }); |
|
72 | 110 | header: key, |
73 | 111 | accessor: accessor, |
74 | 112 | plugins: { |
75 | | - sort: { invert: true }, |
| 113 | + sort: { |
| 114 | + invert: true |
| 115 | + }, |
76 | 116 | colFilter: { |
77 | 117 | fn: columnFilter, |
78 | 118 | render: ({ filterValue, values, id }) => |
79 | | - createRender(TableFilter, { filterValue, values, id, tableId }) |
| 119 | + createRender(TableFilter, { |
| 120 | + filterValue, |
| 121 | + id, |
| 122 | + tableId, |
| 123 | + values |
| 124 | + }) |
80 | 125 | } |
81 | 126 | } |
82 | 127 | }); |
|
91 | 136 | header: '', |
92 | 137 | cell: ({ row }, _) => { |
93 | 138 | return createRender(optionsComponent!, { |
94 | | - row: row.isData() ? row.original : null |
| 139 | + row: row.isData() ? row.original : null, |
| 140 | + dispatchFn: actionDispatcher |
95 | 141 | }); |
96 | 142 | } |
97 | 143 | }) as any |
|
0 commit comments