@zhangt58/svelte-vtable 0.4.0 → 0.4.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 +27 -17
- package/dist/components/DataTable.svelte +16 -7
- package/dist/components/DataTable.svelte.d.ts +2 -0
- package/dist/components/DataTableControls.svelte +16 -4
- package/dist/components/DataTableControls.svelte.d.ts +6 -2
- package/dist/components/filters/FilterDateRange.svelte +57 -13
- package/dist/components/flowbite/DataTableControls.svelte +16 -4
- package/dist/components/flowbite/DataTableControls.svelte.d.ts +6 -2
- package/dist/filterUtils.d.ts +31 -0
- package/dist/filterUtils.js +65 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -157,7 +157,8 @@ A virtualized table component for efficient rendering of large datasets. The com
|
|
|
157
157
|
| `onselect` | `function` | `undefined` | Callback when a row is selected: `({item, index}) => void` |
|
|
158
158
|
| `onsort` | `function` | `undefined` | Callback when sort changes: `({key, dir}) => void`. When provided, local sorting is skipped (server-side sort pattern). Omit entirely (do not pass `() => {}`) to enable local sorting. |
|
|
159
159
|
| `inlineFilters` | `boolean` | `false` | Renders column filter buttons in table headers. Without `onfilter`, filters are applied locally to `items`. |
|
|
160
|
-
| `columnFilters` | `Array \| null` | `null` | Optional filter configurations, usually from `buildColumnFilters(
|
|
160
|
+
| `columnFilters` | `Array \| null` | `null` | Optional explicit filter configurations, usually from `buildColumnFilters(rows, columns)`. Overrides derived filter options. |
|
|
161
|
+
| `filterItems` | `Array \| null` | `null` | Base data source used to derive inline filter options when `columnFilters` is omitted. Defaults to `items`; pass the unpaged/search-scoped rows when `items` is paged. |
|
|
161
162
|
| `activeFilters` | `Object` | `{}` | Controlled filter state for inline header filters. |
|
|
162
163
|
| `onfilter` | `function` | `undefined` | Callback when inline filters change: `({key, values, allFilters}) => void`. When provided, parent code is responsible for applying filters. |
|
|
163
164
|
| `rowSnippet` | `Snippet` | `undefined` | Svelte 5 snippet for rendering rows. Optional when `columns` is provided (a default row is rendered using each column's `cellSnippet` or raw value). |
|
|
@@ -219,7 +220,7 @@ For pagination, server requests, or shared filter controls, keep filters in the
|
|
|
219
220
|
|
|
220
221
|
```svelte
|
|
221
222
|
<script>
|
|
222
|
-
import { DataTable,
|
|
223
|
+
import { DataTable, applyFilters } from '@zhangt58/svelte-vtable';
|
|
223
224
|
|
|
224
225
|
const rows = [
|
|
225
226
|
{ department: 'Engineering', hireDate: '2024-01-15' },
|
|
@@ -230,9 +231,10 @@ For pagination, server requests, or shared filter controls, keep filters in the
|
|
|
230
231
|
{ key: 'hireDate', label: 'Hire Date', filterType: 'daterange' },
|
|
231
232
|
];
|
|
232
233
|
|
|
233
|
-
const columnFilters = buildColumnFilters(rows, columns);
|
|
234
234
|
let activeFilters = $state({});
|
|
235
|
-
const
|
|
235
|
+
const optionRows = $derived(rows); // Apply search or server scoping here when needed.
|
|
236
|
+
const filteredRows = $derived(applyFilters(optionRows, activeFilters));
|
|
237
|
+
const pagedRows = $derived(filteredRows.slice(0, 25));
|
|
236
238
|
|
|
237
239
|
function handleFilter({ allFilters }) {
|
|
238
240
|
activeFilters = { ...allFilters };
|
|
@@ -240,15 +242,19 @@ For pagination, server requests, or shared filter controls, keep filters in the
|
|
|
240
242
|
</script>
|
|
241
243
|
|
|
242
244
|
<DataTable
|
|
243
|
-
items={
|
|
245
|
+
items={pagedRows}
|
|
244
246
|
{columns}
|
|
245
247
|
inlineFilters
|
|
246
|
-
{
|
|
248
|
+
filterItems={optionRows}
|
|
247
249
|
{activeFilters}
|
|
248
250
|
onfilter={handleFilter}
|
|
249
251
|
/>
|
|
250
252
|
```
|
|
251
253
|
|
|
254
|
+
When `filterItems` and `activeFilters` are provided, derived filter options are faceted per column: each column is built from `filterItems` after applying the other active filters, but not that column's own filter. This keeps date range bounds and value choices available when the current column filter temporarily matches zero rows.
|
|
255
|
+
|
|
256
|
+
Date range quick presets such as `Last 7d` are relative to the current wall-clock date/time. Presets that would match no rows in the current option source are disabled.
|
|
257
|
+
|
|
252
258
|
#### Column Widths
|
|
253
259
|
|
|
254
260
|
Column widths can be specified as:
|
|
@@ -271,17 +277,21 @@ Controls component for search and pagination.
|
|
|
271
277
|
|
|
272
278
|
#### Props
|
|
273
279
|
|
|
274
|
-
| Prop | Type
|
|
275
|
-
| ----------------- |
|
|
276
|
-
| `search` | `string`
|
|
277
|
-
| `currentPage` | `number`
|
|
278
|
-
| `perPage` | `number`
|
|
279
|
-
| `totalItems` | `number`
|
|
280
|
-
| `onpage` | `function`
|
|
281
|
-
| `onsearch` | `function`
|
|
282
|
-
| `onfilterstoggle` | `function`
|
|
283
|
-
| `onperpage` | `function`
|
|
284
|
-
| `
|
|
280
|
+
| Prop | Type | Default | Description |
|
|
281
|
+
| ----------------- | --------------- | ----------- | ---------------------------------------------------------------------------------------------------------------------------------- |
|
|
282
|
+
| `search` | `string` | `''` | Current search query |
|
|
283
|
+
| `currentPage` | `number` | `1` | Current page number |
|
|
284
|
+
| `perPage` | `number` | `25` | Items per page |
|
|
285
|
+
| `totalItems` | `number` | `0` | Total number of items |
|
|
286
|
+
| `onpage` | `function` | `undefined` | Callback for page changes: `({page}) => void` |
|
|
287
|
+
| `onsearch` | `function` | `undefined` | Callback for search changes: `({search}) => void` |
|
|
288
|
+
| `onfilterstoggle` | `function` | `undefined` | Callback when filters panel toggled: `({visible}) => void` |
|
|
289
|
+
| `onperpage` | `function` | `undefined` | Callback when per-page changes: `({perPage}) => void` |
|
|
290
|
+
| `columnFilters` | `Array \| null` | `null` | Optional explicit modal filter configurations. Overrides derived filter options. |
|
|
291
|
+
| `columns` | `ColumnDef[]` | `[]` | Column definitions used with `filterItems` to derive modal filter options. |
|
|
292
|
+
| `filterItems` | `Array \| null` | `null` | Base data source used to derive modal filter options. Pass unpaged/search-scoped rows; active peer filters are applied per column. |
|
|
293
|
+
| `activeFilters` | `Object` | `{}` | Current active filters used for the modal state and faceted option derivation. |
|
|
294
|
+
| `onfilter` | `function` | `undefined` | Callback when filters change (passed through to DataTableFilters): `({key, values, allFilters}) => void` |
|
|
285
295
|
|
|
286
296
|
### DataTableFilters
|
|
287
297
|
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
import {
|
|
6
6
|
DEFAULT_RELATIVE_RANGE_PRESETS,
|
|
7
7
|
applyFilters,
|
|
8
|
-
buildColumnFilters,
|
|
9
8
|
hasActiveFilterValue,
|
|
9
|
+
resolveColumnFilters,
|
|
10
10
|
} from '../filterUtils.js';
|
|
11
11
|
|
|
12
12
|
// Runic props: accept props via $props() so the component matches the
|
|
@@ -31,8 +31,10 @@
|
|
|
31
31
|
// Render per-column filter controls in table headers.
|
|
32
32
|
inlineFilters = false,
|
|
33
33
|
// Filter configs can be provided from all source rows. When omitted and
|
|
34
|
-
// inlineFilters is true, configs are derived from this component's items.
|
|
34
|
+
// inlineFilters is true, configs are derived from filterItems or this component's items.
|
|
35
35
|
columnFilters = null,
|
|
36
|
+
// Base data source used to build inline filter option lists when columnFilters is omitted.
|
|
37
|
+
filterItems = null,
|
|
36
38
|
activeFilters = {},
|
|
37
39
|
// onfilter({ key, values, allFilters }) makes filtering parent-managed.
|
|
38
40
|
// When omitted, inline filters are applied locally to this table's items.
|
|
@@ -65,9 +67,13 @@
|
|
|
65
67
|
const colDefMap = $derived(Object.fromEntries(columns.map((c) => [c.key, c])));
|
|
66
68
|
|
|
67
69
|
const effectiveColumnFilters = $derived.by(() => {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
70
|
+
return resolveColumnFilters({
|
|
71
|
+
columnFilters,
|
|
72
|
+
columns,
|
|
73
|
+
filterItems: Array.isArray(filterItems) ? filterItems : items,
|
|
74
|
+
activeFilters,
|
|
75
|
+
enabled: inlineFilters,
|
|
76
|
+
});
|
|
71
77
|
});
|
|
72
78
|
|
|
73
79
|
const filterDefMap = $derived(
|
|
@@ -233,12 +239,12 @@
|
|
|
233
239
|
<svelte:window onclick={closeFilter} onkeydown={handleKeydown} />
|
|
234
240
|
|
|
235
241
|
<section class="virtual-data-table {className}" {style}>
|
|
236
|
-
{#if
|
|
242
|
+
{#if columns.length > 0}
|
|
237
243
|
<!-- When `virtualize` is false we instruct VirtualList to render as a normal list/table
|
|
238
244
|
by setting `isDisabled`. This ensures pagination based on item counts shows the
|
|
239
245
|
exact number of rows expected (useful when rows have variable height). -->
|
|
240
246
|
<VirtualList
|
|
241
|
-
items={sortedItems}
|
|
247
|
+
items={Array.isArray(sortedItems) ? sortedItems : []}
|
|
242
248
|
isTable
|
|
243
249
|
isDisabled={!virtualize}
|
|
244
250
|
class="datatable-table max-w-full max-h-full overflow-auto"
|
|
@@ -334,6 +340,9 @@
|
|
|
334
340
|
</tr>
|
|
335
341
|
{/snippet}
|
|
336
342
|
</VirtualList>
|
|
343
|
+
{#if !sortedItems || sortedItems.length === 0}
|
|
344
|
+
<div class="p-4 text-center text-slate-500">{emptyMessage}</div>
|
|
345
|
+
{/if}
|
|
337
346
|
{:else}
|
|
338
347
|
<div class="p-4 text-center text-slate-500">{emptyMessage}</div>
|
|
339
348
|
{/if}
|
|
@@ -16,6 +16,7 @@ declare const DataTable: import("svelte").Component<{
|
|
|
16
16
|
onsort?: any;
|
|
17
17
|
inlineFilters?: boolean;
|
|
18
18
|
columnFilters?: any;
|
|
19
|
+
filterItems?: any;
|
|
19
20
|
activeFilters?: Record<string, any>;
|
|
20
21
|
onfilter?: any;
|
|
21
22
|
oncolumnsort?: any;
|
|
@@ -41,6 +42,7 @@ type $$ComponentProps = {
|
|
|
41
42
|
onsort?: any;
|
|
42
43
|
inlineFilters?: boolean;
|
|
43
44
|
columnFilters?: any;
|
|
45
|
+
filterItems?: any;
|
|
44
46
|
activeFilters?: Record<string, any>;
|
|
45
47
|
onfilter?: any;
|
|
46
48
|
oncolumnsort?: any;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
<script>
|
|
2
2
|
import FiltersModal from './FiltersModal.svelte';
|
|
3
3
|
import PaginationBar from './PaginationBar.svelte';
|
|
4
|
-
import { countActiveFilters } from '../filterUtils.js';
|
|
4
|
+
import { countActiveFilters, resolveColumnFilters } from '../filterUtils.js';
|
|
5
5
|
|
|
6
6
|
// Props for search, pagination (perPage made bindable using Svelte 5 rune $bindable)
|
|
7
7
|
let {
|
|
@@ -15,8 +15,11 @@
|
|
|
15
15
|
filtersVisible = false,
|
|
16
16
|
// callback when toggle changes: onfilterstoggle({ visible })
|
|
17
17
|
onfilterstoggle = undefined,
|
|
18
|
-
// optional DataTableFilters inputs -
|
|
19
|
-
columnFilters =
|
|
18
|
+
// optional DataTableFilters inputs - explicit configs override derived configs
|
|
19
|
+
columnFilters = null,
|
|
20
|
+
// optional column definitions + base row source used to derive modal filter options
|
|
21
|
+
columns = [],
|
|
22
|
+
filterItems = null,
|
|
20
23
|
activeFilters = {},
|
|
21
24
|
// onfilter({ key, values, allFilters }) — passed through to DataTableFilters
|
|
22
25
|
/** @type {((..._args: any[]) => void) | undefined} */
|
|
@@ -37,6 +40,15 @@
|
|
|
37
40
|
|
|
38
41
|
const activeFilterCount = $derived(countActiveFilters(activeFilters));
|
|
39
42
|
|
|
43
|
+
const effectiveColumnFilters = $derived.by(() =>
|
|
44
|
+
resolveColumnFilters({
|
|
45
|
+
columnFilters,
|
|
46
|
+
columns,
|
|
47
|
+
filterItems,
|
|
48
|
+
activeFilters,
|
|
49
|
+
}),
|
|
50
|
+
);
|
|
51
|
+
|
|
40
52
|
// totalPages derived from totalItems and perPage
|
|
41
53
|
const totalPages = $derived(Math.max(1, Math.ceil(totalItems / perPage)));
|
|
42
54
|
|
|
@@ -310,7 +322,7 @@
|
|
|
310
322
|
|
|
311
323
|
<FiltersModal
|
|
312
324
|
bind:open={filtersVisible}
|
|
313
|
-
{
|
|
325
|
+
columnFilters={effectiveColumnFilters}
|
|
314
326
|
{activeFilters}
|
|
315
327
|
{onfilter}
|
|
316
328
|
{direction}
|
|
@@ -12,7 +12,9 @@ declare const DataTableControls: import("svelte").Component<{
|
|
|
12
12
|
onsearch?: any;
|
|
13
13
|
filtersVisible?: boolean;
|
|
14
14
|
onfilterstoggle?: any;
|
|
15
|
-
columnFilters?: any
|
|
15
|
+
columnFilters?: any;
|
|
16
|
+
columns?: any[];
|
|
17
|
+
filterItems?: any;
|
|
16
18
|
activeFilters?: Record<string, any>;
|
|
17
19
|
onfilter?: any;
|
|
18
20
|
direction?: string;
|
|
@@ -29,7 +31,9 @@ type $$ComponentProps = {
|
|
|
29
31
|
onsearch?: any;
|
|
30
32
|
filtersVisible?: boolean;
|
|
31
33
|
onfilterstoggle?: any;
|
|
32
|
-
columnFilters?: any
|
|
34
|
+
columnFilters?: any;
|
|
35
|
+
columns?: any[];
|
|
36
|
+
filterItems?: any;
|
|
33
37
|
activeFilters?: Record<string, any>;
|
|
34
38
|
onfilter?: any;
|
|
35
39
|
direction?: string;
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* - Clear button
|
|
8
8
|
*
|
|
9
9
|
* Props:
|
|
10
|
-
* column — column config { key, label, type, minValue, maxValue }
|
|
10
|
+
* column — column config { key, label, type, minValue, maxValue, dateValues }
|
|
11
11
|
* selection — current range selection { from?: string, to?: string }
|
|
12
12
|
* relativeRangePresets — array of { label, value, unit } presets
|
|
13
13
|
* onchange — callback({ from?, to? }) called with the updated selection
|
|
@@ -29,6 +29,13 @@
|
|
|
29
29
|
: new Date(str).getTime();
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
function parseFilterDateEnd(str) {
|
|
33
|
+
if (!str) return NaN;
|
|
34
|
+
return /^\d{4}-\d{2}-\d{2}$/.test(str)
|
|
35
|
+
? new Date(str + 'T23:59:59.999').getTime()
|
|
36
|
+
: new Date(str).getTime();
|
|
37
|
+
}
|
|
38
|
+
|
|
32
39
|
function formatAsInputValue(ms, type) {
|
|
33
40
|
const d = new Date(ms);
|
|
34
41
|
const pad = (n) => String(n).padStart(2, '0');
|
|
@@ -44,6 +51,11 @@
|
|
|
44
51
|
const totalMs = $derived(maxMs - minMs);
|
|
45
52
|
const fromMs = $derived(parseFilterDate(selection.from));
|
|
46
53
|
const toMs = $derived(parseFilterDate(selection.to));
|
|
54
|
+
const dateValueMs = $derived(
|
|
55
|
+
Array.isArray(column.dateValues)
|
|
56
|
+
? column.dateValues.map(Number).filter((ms) => Number.isFinite(ms))
|
|
57
|
+
: [],
|
|
58
|
+
);
|
|
47
59
|
|
|
48
60
|
const fromPct = $derived(
|
|
49
61
|
totalMs > 0 && !isNaN(fromMs)
|
|
@@ -72,7 +84,7 @@
|
|
|
72
84
|
if (onchange) onchange({ ...selection, to: formatAsInputValue(newToMs, inputType) });
|
|
73
85
|
}
|
|
74
86
|
|
|
75
|
-
function
|
|
87
|
+
function createRelativeRange(value, unit) {
|
|
76
88
|
const now = new Date();
|
|
77
89
|
const from = new Date(now);
|
|
78
90
|
switch (unit) {
|
|
@@ -80,10 +92,12 @@
|
|
|
80
92
|
from.setHours(from.getHours() - value);
|
|
81
93
|
break;
|
|
82
94
|
case 'day':
|
|
83
|
-
from.setDate(from.getDate() - value);
|
|
95
|
+
from.setDate(from.getDate() - (inputType === 'date' ? Math.max(value - 1, 0) : value));
|
|
84
96
|
break;
|
|
85
97
|
case 'week':
|
|
86
|
-
from.setDate(
|
|
98
|
+
from.setDate(
|
|
99
|
+
from.getDate() - (inputType === 'date' ? Math.max(value * 7 - 1, 0) : value * 7),
|
|
100
|
+
);
|
|
87
101
|
break;
|
|
88
102
|
case 'month':
|
|
89
103
|
from.setMonth(from.getMonth() - value);
|
|
@@ -92,13 +106,40 @@
|
|
|
92
106
|
from.setFullYear(from.getFullYear() - value);
|
|
93
107
|
break;
|
|
94
108
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
109
|
+
return {
|
|
110
|
+
from: formatAsInputValue(from.getTime(), inputType),
|
|
111
|
+
to: formatAsInputValue(now.getTime(), inputType),
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function relativeRangeHasMatches(range) {
|
|
116
|
+
const rangeFromMs = parseFilterDate(range.from);
|
|
117
|
+
const rangeToMs = parseFilterDateEnd(range.to);
|
|
118
|
+
if (isNaN(rangeFromMs) || isNaN(rangeToMs)) return true;
|
|
119
|
+
|
|
120
|
+
if (dateValueMs.length > 0) {
|
|
121
|
+
return dateValueMs.some((ms) => ms >= rangeFromMs && ms <= rangeToMs);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (!isNaN(minMs) && !isNaN(maxMs)) {
|
|
125
|
+
return maxMs >= rangeFromMs && minMs <= rangeToMs;
|
|
100
126
|
}
|
|
127
|
+
|
|
128
|
+
return column.minValue === undefined && column.maxValue === undefined;
|
|
101
129
|
}
|
|
130
|
+
|
|
131
|
+
function setRelativeRange(value, unit) {
|
|
132
|
+
const range = createRelativeRange(value, unit);
|
|
133
|
+
if (!relativeRangeHasMatches(range)) return;
|
|
134
|
+
if (onchange) onchange(range);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const presetStates = $derived(
|
|
138
|
+
applicablePresets.map((preset) => ({
|
|
139
|
+
...preset,
|
|
140
|
+
disabled: !relativeRangeHasMatches(createRelativeRange(preset.value, preset.unit)),
|
|
141
|
+
})),
|
|
142
|
+
);
|
|
102
143
|
</script>
|
|
103
144
|
|
|
104
145
|
<div class="p-3 flex flex-col gap-2">
|
|
@@ -233,13 +274,16 @@
|
|
|
233
274
|
{/if}
|
|
234
275
|
|
|
235
276
|
<!-- Relative-range quick buttons -->
|
|
236
|
-
{#if
|
|
277
|
+
{#if presetStates.length > 0}
|
|
237
278
|
<div class="flex flex-wrap gap-1 pt-1 border-t border-gray-100 dark:border-gray-600">
|
|
238
|
-
{#each
|
|
279
|
+
{#each presetStates as preset (preset.label)}
|
|
239
280
|
<button
|
|
240
281
|
type="button"
|
|
241
|
-
class="px-2 py-0.5 text-xs rounded border border-blue-400 text-blue-700 dark:text-blue-300 hover:bg-blue-50 dark:hover:bg-blue-900/20 transition-colors"
|
|
242
|
-
title=
|
|
282
|
+
class="px-2 py-0.5 text-xs rounded border border-blue-400 text-blue-700 dark:text-blue-300 hover:bg-blue-50 dark:hover:bg-blue-900/20 transition-colors disabled:opacity-50 disabled:cursor-not-allowed disabled:hover:bg-transparent dark:disabled:hover:bg-transparent"
|
|
283
|
+
title={preset.disabled
|
|
284
|
+
? `No matching rows in the last ${preset.value} ${preset.unit}${preset.value !== 1 ? 's' : ''}`
|
|
285
|
+
: `Last ${preset.value} ${preset.unit}${preset.value !== 1 ? 's' : ''}`}
|
|
286
|
+
disabled={preset.disabled}
|
|
243
287
|
onclick={() => setRelativeRange(preset.value, preset.unit)}
|
|
244
288
|
>
|
|
245
289
|
Last {preset.label}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { Search, Badge, Select } from 'flowbite-svelte';
|
|
3
3
|
import FiltersModal from './FiltersModal.svelte';
|
|
4
4
|
import PaginationBar from '../PaginationBar.svelte';
|
|
5
|
-
import { countActiveFilters } from '../../filterUtils.js';
|
|
5
|
+
import { countActiveFilters, resolveColumnFilters } from '../../filterUtils.js';
|
|
6
6
|
|
|
7
7
|
// Props for search, pagination (perPage made bindable using Svelte 5 rune $bindable)
|
|
8
8
|
let {
|
|
@@ -16,8 +16,11 @@
|
|
|
16
16
|
filtersVisible = false,
|
|
17
17
|
// callback when toggle changes: onfilterstoggle({ visible })
|
|
18
18
|
onfilterstoggle = undefined,
|
|
19
|
-
// optional DataTableFilters inputs -
|
|
20
|
-
columnFilters =
|
|
19
|
+
// optional DataTableFilters inputs - explicit configs override derived configs
|
|
20
|
+
columnFilters = null,
|
|
21
|
+
// optional column definitions + base row source used to derive modal filter options
|
|
22
|
+
columns = [],
|
|
23
|
+
filterItems = null,
|
|
21
24
|
activeFilters = {},
|
|
22
25
|
// onfilter({ key, values, allFilters }) — passed through to DataTableFilters
|
|
23
26
|
/** @type {((..._args: any[]) => void) | undefined} */
|
|
@@ -38,6 +41,15 @@
|
|
|
38
41
|
|
|
39
42
|
const activeFilterCount = $derived(countActiveFilters(activeFilters));
|
|
40
43
|
|
|
44
|
+
const effectiveColumnFilters = $derived.by(() =>
|
|
45
|
+
resolveColumnFilters({
|
|
46
|
+
columnFilters,
|
|
47
|
+
columns,
|
|
48
|
+
filterItems,
|
|
49
|
+
activeFilters,
|
|
50
|
+
}),
|
|
51
|
+
);
|
|
52
|
+
|
|
41
53
|
// totalPages derived from totalItems and perPage
|
|
42
54
|
const totalPages = $derived(Math.max(1, Math.ceil(totalItems / perPage)));
|
|
43
55
|
|
|
@@ -261,7 +273,7 @@
|
|
|
261
273
|
|
|
262
274
|
<FiltersModal
|
|
263
275
|
bind:open={filtersVisible}
|
|
264
|
-
{
|
|
276
|
+
columnFilters={effectiveColumnFilters}
|
|
265
277
|
{activeFilters}
|
|
266
278
|
{onfilter}
|
|
267
279
|
{direction}
|
|
@@ -12,7 +12,9 @@ declare const DataTableControls: import("svelte").Component<{
|
|
|
12
12
|
onsearch?: any;
|
|
13
13
|
filtersVisible?: boolean;
|
|
14
14
|
onfilterstoggle?: any;
|
|
15
|
-
columnFilters?: any
|
|
15
|
+
columnFilters?: any;
|
|
16
|
+
columns?: any[];
|
|
17
|
+
filterItems?: any;
|
|
16
18
|
activeFilters?: Record<string, any>;
|
|
17
19
|
onfilter?: any;
|
|
18
20
|
direction?: string;
|
|
@@ -29,7 +31,9 @@ type $$ComponentProps = {
|
|
|
29
31
|
onsearch?: any;
|
|
30
32
|
filtersVisible?: boolean;
|
|
31
33
|
onfilterstoggle?: any;
|
|
32
|
-
columnFilters?: any
|
|
34
|
+
columnFilters?: any;
|
|
35
|
+
columns?: any[];
|
|
36
|
+
filterItems?: any;
|
|
33
37
|
activeFilters?: Record<string, any>;
|
|
34
38
|
onfilter?: any;
|
|
35
39
|
direction?: string;
|
package/dist/filterUtils.d.ts
CHANGED
|
@@ -10,6 +10,37 @@ export function getFilterType(column: object | string | undefined | null): strin
|
|
|
10
10
|
* @returns {boolean}
|
|
11
11
|
*/
|
|
12
12
|
export function isDateRangeColumn(column: object | string | undefined | null): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Return active filters excluding one column key.
|
|
15
|
+
* This is used by faceted option builders so a column's own active filter
|
|
16
|
+
* cannot remove the choices needed to change or clear that filter.
|
|
17
|
+
* @param {Object|null|undefined} activeFilters
|
|
18
|
+
* @param {string} excludedKey
|
|
19
|
+
* @returns {Object}
|
|
20
|
+
*/
|
|
21
|
+
export function filtersWithoutKey(activeFilters: any | null | undefined, excludedKey: string): any;
|
|
22
|
+
/**
|
|
23
|
+
* Resolve column filter configs from either explicit configs or a row source + columns.
|
|
24
|
+
* Explicit `columnFilters` always wins; otherwise `filterItems` is treated as the
|
|
25
|
+
* base source for derived option lists. In faceted mode, each column is built from
|
|
26
|
+
* rows filtered by the other active filters, but not by that column's own filter.
|
|
27
|
+
* @param {object} opts
|
|
28
|
+
* @param {Array|null|undefined} opts.columnFilters - Explicit filter configs.
|
|
29
|
+
* @param {Array} [opts.columns] - Column definitions used for derived filter configs.
|
|
30
|
+
* @param {Array|null|undefined} [opts.filterItems] - Base row source used to derive filter options.
|
|
31
|
+
* @param {Object|null|undefined} [opts.activeFilters] - Current active filter state.
|
|
32
|
+
* @param {boolean} [opts.enabled] - Whether derived filters should be built.
|
|
33
|
+
* @param {boolean} [opts.faceted] - Whether to derive each column from rows filtered by peer columns.
|
|
34
|
+
* @returns {Array}
|
|
35
|
+
*/
|
|
36
|
+
export function resolveColumnFilters({ columnFilters, columns, filterItems, activeFilters, enabled, faceted, }: {
|
|
37
|
+
columnFilters: any[] | null | undefined;
|
|
38
|
+
columns?: any[];
|
|
39
|
+
filterItems?: any[] | null | undefined;
|
|
40
|
+
activeFilters?: any | null | undefined;
|
|
41
|
+
enabled?: boolean;
|
|
42
|
+
faceted?: boolean;
|
|
43
|
+
}): any[];
|
|
13
44
|
/**
|
|
14
45
|
* Check whether a single filter value is active.
|
|
15
46
|
* @param {unknown} value
|
package/dist/filterUtils.js
CHANGED
|
@@ -32,6 +32,65 @@ export function isDateRangeColumn(column) {
|
|
|
32
32
|
return type === 'daterange' || type === 'datetimerange';
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
+
/**
|
|
36
|
+
* Return active filters excluding one column key.
|
|
37
|
+
* This is used by faceted option builders so a column's own active filter
|
|
38
|
+
* cannot remove the choices needed to change or clear that filter.
|
|
39
|
+
* @param {Object|null|undefined} activeFilters
|
|
40
|
+
* @param {string} excludedKey
|
|
41
|
+
* @returns {Object}
|
|
42
|
+
*/
|
|
43
|
+
export function filtersWithoutKey(activeFilters, excludedKey) {
|
|
44
|
+
if (!activeFilters || typeof activeFilters !== 'object') return {};
|
|
45
|
+
const filters = {};
|
|
46
|
+
for (const [key, value] of Object.entries(activeFilters)) {
|
|
47
|
+
if (key !== excludedKey && hasActiveFilterValue(value)) filters[key] = value;
|
|
48
|
+
}
|
|
49
|
+
return filters;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Resolve column filter configs from either explicit configs or a row source + columns.
|
|
54
|
+
* Explicit `columnFilters` always wins; otherwise `filterItems` is treated as the
|
|
55
|
+
* base source for derived option lists. In faceted mode, each column is built from
|
|
56
|
+
* rows filtered by the other active filters, but not by that column's own filter.
|
|
57
|
+
* @param {object} opts
|
|
58
|
+
* @param {Array|null|undefined} opts.columnFilters - Explicit filter configs.
|
|
59
|
+
* @param {Array} [opts.columns] - Column definitions used for derived filter configs.
|
|
60
|
+
* @param {Array|null|undefined} [opts.filterItems] - Base row source used to derive filter options.
|
|
61
|
+
* @param {Object|null|undefined} [opts.activeFilters] - Current active filter state.
|
|
62
|
+
* @param {boolean} [opts.enabled] - Whether derived filters should be built.
|
|
63
|
+
* @param {boolean} [opts.faceted] - Whether to derive each column from rows filtered by peer columns.
|
|
64
|
+
* @returns {Array}
|
|
65
|
+
*/
|
|
66
|
+
export function resolveColumnFilters({
|
|
67
|
+
columnFilters,
|
|
68
|
+
columns = [],
|
|
69
|
+
filterItems = [],
|
|
70
|
+
activeFilters = null,
|
|
71
|
+
enabled = true,
|
|
72
|
+
faceted = true,
|
|
73
|
+
}) {
|
|
74
|
+
if (Array.isArray(columnFilters)) return columnFilters;
|
|
75
|
+
if (!enabled || !Array.isArray(columns) || columns.length === 0) return [];
|
|
76
|
+
const sourceItems = Array.isArray(filterItems) ? filterItems : [];
|
|
77
|
+
|
|
78
|
+
if (!faceted || !hasActiveFilters(activeFilters)) {
|
|
79
|
+
return buildColumnFilters(sourceItems, columns);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return columns
|
|
83
|
+
.filter((col) => col.filterType !== 'none')
|
|
84
|
+
.map((col) => {
|
|
85
|
+
const peerFilters = filtersWithoutKey(activeFilters, col.key);
|
|
86
|
+
const filteredItems = hasActiveFilters(peerFilters)
|
|
87
|
+
? applyFilters(sourceItems, peerFilters)
|
|
88
|
+
: sourceItems;
|
|
89
|
+
return buildColumnFilters(filteredItems, [col])[0];
|
|
90
|
+
})
|
|
91
|
+
.filter(Boolean);
|
|
92
|
+
}
|
|
93
|
+
|
|
35
94
|
/**
|
|
36
95
|
* Check whether a single filter value is active.
|
|
37
96
|
* @param {unknown} value
|
|
@@ -93,14 +152,17 @@ export function buildColumnFilters(data, columns) {
|
|
|
93
152
|
// offer a dual-range slider and Earliest/Latest shortcut buttons.
|
|
94
153
|
let minMs = Infinity;
|
|
95
154
|
let maxMs = -Infinity;
|
|
155
|
+
const dateValueSet = new Set();
|
|
96
156
|
for (const item of data) {
|
|
97
157
|
const v = item[col.key];
|
|
98
158
|
if (v === null || v === undefined || v === '') continue;
|
|
99
159
|
const str = String(v);
|
|
100
160
|
const d = /^\d{4}-\d{2}-\d{2}$/.test(str) ? new Date(str + 'T00:00:00') : new Date(str);
|
|
101
161
|
if (!isNaN(d.getTime())) {
|
|
102
|
-
|
|
103
|
-
|
|
162
|
+
const ms = d.getTime();
|
|
163
|
+
dateValueSet.add(ms);
|
|
164
|
+
if (ms < minMs) minMs = ms;
|
|
165
|
+
if (ms > maxMs) maxMs = ms;
|
|
104
166
|
}
|
|
105
167
|
}
|
|
106
168
|
|
|
@@ -126,6 +188,7 @@ export function buildColumnFilters(data, columns) {
|
|
|
126
188
|
counts: {},
|
|
127
189
|
minValue: hasRange ? fmt(minMs) : null,
|
|
128
190
|
maxValue: hasRange ? fmt(maxMs) : null,
|
|
191
|
+
dateValues: hasRange ? [...dateValueSet].sort((a, b) => a - b) : [],
|
|
129
192
|
};
|
|
130
193
|
}
|
|
131
194
|
const { uniqueValues, counts } = getUniqueValuesWithCounts(data, col.key);
|
package/dist/index.d.ts
CHANGED
|
@@ -58,4 +58,4 @@ export type ColumnDef<T extends Record<string, unknown> = Record<string, unknown
|
|
|
58
58
|
index: number;
|
|
59
59
|
}]>;
|
|
60
60
|
};
|
|
61
|
-
export { DEFAULT_RELATIVE_RANGE_PRESETS, getFilterType, getUniqueValuesWithCounts, buildColumnFilters, applyFilters, matchesDateRange, isDateRangeColumn, hasActiveFilterValue, countActiveFilterValue, hasActiveFilters, countActiveFilters, clearAllFilters, filtersToSearchParams, searchParamsToFilters } from "./filterUtils.js";
|
|
61
|
+
export { DEFAULT_RELATIVE_RANGE_PRESETS, getFilterType, filtersWithoutKey, resolveColumnFilters, getUniqueValuesWithCounts, buildColumnFilters, applyFilters, matchesDateRange, isDateRangeColumn, hasActiveFilterValue, countActiveFilterValue, hasActiveFilters, countActiveFilters, clearAllFilters, filtersToSearchParams, searchParamsToFilters } from "./filterUtils.js";
|
package/dist/index.js
CHANGED
|
@@ -13,6 +13,8 @@ export { useFilterState } from './components/filters/useFilterState.svelte.js';
|
|
|
13
13
|
export {
|
|
14
14
|
DEFAULT_RELATIVE_RANGE_PRESETS,
|
|
15
15
|
getFilterType,
|
|
16
|
+
filtersWithoutKey,
|
|
17
|
+
resolveColumnFilters,
|
|
16
18
|
getUniqueValuesWithCounts,
|
|
17
19
|
buildColumnFilters,
|
|
18
20
|
applyFilters,
|