@zhangt58/svelte-vtable 0.1.1 → 0.1.2
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 +86 -71
- package/package.json +7 -3
- package/src/components/DataTable.svelte +67 -16
- package/src/components/DataTableControls.svelte +136 -54
- package/src/components/DataTableFilters.svelte +182 -82
- package/src/components/FiltersModal.svelte +21 -17
- package/src/lib/dist/styles.css +12 -9
- package/src/lib/filterUtils.js +14 -13
- package/src/lib/index.js +1 -1
- package/src/lib/styles.css +13 -10
|
@@ -26,20 +26,23 @@
|
|
|
26
26
|
showCounts = true,
|
|
27
27
|
className = '',
|
|
28
28
|
// new callback when perPage changes
|
|
29
|
-
perpagechange = () => {}
|
|
29
|
+
perpagechange = () => {},
|
|
30
30
|
} = $props();
|
|
31
31
|
|
|
32
32
|
const perPageOptions = [
|
|
33
|
-
{value: 10, name:
|
|
34
|
-
{value: 25, name:
|
|
35
|
-
{value: 50, name:
|
|
36
|
-
{value: 100, name:
|
|
37
|
-
]
|
|
33
|
+
{ value: 10, name: '10 rows' },
|
|
34
|
+
{ value: 25, name: '25 rows' },
|
|
35
|
+
{ value: 50, name: '50 rows' },
|
|
36
|
+
{ value: 100, name: '100 rows' },
|
|
37
|
+
];
|
|
38
38
|
|
|
39
39
|
// count of active filters (sum of selected values per column)
|
|
40
40
|
const activeFilterCount = $derived(() => {
|
|
41
41
|
try {
|
|
42
|
-
return Object.values(activeFilters || {}).reduce(
|
|
42
|
+
return Object.values(activeFilters || {}).reduce(
|
|
43
|
+
(sum, v) => sum + (Array.isArray(v) ? v.length : 0),
|
|
44
|
+
0,
|
|
45
|
+
);
|
|
43
46
|
} catch (e) {
|
|
44
47
|
return 0;
|
|
45
48
|
}
|
|
@@ -75,7 +78,9 @@
|
|
|
75
78
|
return;
|
|
76
79
|
}
|
|
77
80
|
if (filtersVisible !== _prevFiltersVisible) {
|
|
78
|
-
try {
|
|
81
|
+
try {
|
|
82
|
+
filterstoggle?.({ filtersVisible });
|
|
83
|
+
} catch (err) {}
|
|
79
84
|
_prevFiltersVisible = filtersVisible;
|
|
80
85
|
}
|
|
81
86
|
});
|
|
@@ -103,15 +108,15 @@
|
|
|
103
108
|
});
|
|
104
109
|
|
|
105
110
|
// Calculate range for display
|
|
106
|
-
const startItem = $derived(() => totalItems === 0 ? 0 : (currentPage - 1) * perPage + 1);
|
|
111
|
+
const startItem = $derived(() => (totalItems === 0 ? 0 : (currentPage - 1) * perPage + 1));
|
|
107
112
|
const endItem = $derived(() => Math.min(currentPage * perPage, totalItems));
|
|
108
113
|
|
|
109
114
|
// visiblePages: number of middle numeric buttons to show (not counting first/last)
|
|
110
115
|
const visiblePages = 5;
|
|
111
116
|
|
|
112
117
|
// stable id to query the pagination DOM if needed (fallback)
|
|
113
|
-
const paginationId = `pagination-${Math.random().toString(36).slice(2,9)}`;
|
|
114
|
-
const perPageSelectId = `dpc-perpage-${Math.random().toString(36).slice(2,7)}`;
|
|
118
|
+
const paginationId = `pagination-${Math.random().toString(36).slice(2, 9)}`;
|
|
119
|
+
const perPageSelectId = `dpc-perpage-${Math.random().toString(36).slice(2, 7)}`;
|
|
115
120
|
|
|
116
121
|
// pages array for Pagination component (condensed with ellipses)
|
|
117
122
|
const pages = $derived(() => {
|
|
@@ -120,7 +125,10 @@
|
|
|
120
125
|
|
|
121
126
|
// If small number of pages, show all
|
|
122
127
|
if (total <= visiblePages + 2) {
|
|
123
|
-
return Array.from({ length: total }, (_, i) => ({
|
|
128
|
+
return Array.from({ length: total }, (_, i) => ({
|
|
129
|
+
name: String(i + 1),
|
|
130
|
+
active: i + 1 === current,
|
|
131
|
+
}));
|
|
124
132
|
}
|
|
125
133
|
|
|
126
134
|
const pagesArr = [];
|
|
@@ -155,7 +163,10 @@
|
|
|
155
163
|
// compute right jump (midpoint of hidden range end+1..total-1)
|
|
156
164
|
const rightHiddenStart = end + 1;
|
|
157
165
|
const rightHiddenEnd = total - 1;
|
|
158
|
-
const rightJump = Math.min(
|
|
166
|
+
const rightJump = Math.min(
|
|
167
|
+
rightHiddenEnd,
|
|
168
|
+
Math.ceil((rightHiddenStart + rightHiddenEnd) / 2),
|
|
169
|
+
);
|
|
159
170
|
pagesArr.push({ name: '…', active: false, ellipsis: 'right', jump: rightJump });
|
|
160
171
|
}
|
|
161
172
|
|
|
@@ -189,8 +200,8 @@
|
|
|
189
200
|
}
|
|
190
201
|
if (!ul) return;
|
|
191
202
|
// Ensure the <ul> has Tailwind utility classes for layout
|
|
192
|
-
ul.classList.remove('flex','gap-0.5','p-0','m-0','list-none','items-center');
|
|
193
|
-
ul.classList.add('flex','gap-0.5','p-0','m-0','list-none','items-center');
|
|
203
|
+
ul.classList.remove('flex', 'gap-0.5', 'p-0', 'm-0', 'list-none', 'items-center');
|
|
204
|
+
ul.classList.add('flex', 'gap-0.5', 'p-0', 'm-0', 'list-none', 'items-center');
|
|
194
205
|
const lis = Array.from(ul.children || []);
|
|
195
206
|
// Build a list of page-button <li>s (skip prev/next controls) by checking their textContent
|
|
196
207
|
const pageLis = [];
|
|
@@ -216,32 +227,57 @@
|
|
|
216
227
|
// Apply Tailwind utility classes instead of inline styles
|
|
217
228
|
// Remove any previously-applied utility classes we manage
|
|
218
229
|
btn.classList.remove(
|
|
219
|
-
'px-2',
|
|
220
|
-
'
|
|
221
|
-
'
|
|
222
|
-
'
|
|
230
|
+
'px-2',
|
|
231
|
+
'py-1',
|
|
232
|
+
'text-sm',
|
|
233
|
+
'border',
|
|
234
|
+
'border-gray-300',
|
|
235
|
+
'bg-transparent',
|
|
236
|
+
'min-w-[2rem]',
|
|
237
|
+
'inline-flex',
|
|
238
|
+
'items-center',
|
|
239
|
+
'justify-center',
|
|
240
|
+
'transition-colors',
|
|
241
|
+
'hover:bg-gray-100',
|
|
242
|
+
'bg-green-100',
|
|
243
|
+
'text-green-600',
|
|
244
|
+
'border-transparent',
|
|
245
|
+
'opacity-60',
|
|
246
|
+
'cursor-default',
|
|
247
|
+
'pointer-events-none',
|
|
248
|
+
'tw-ellipsis',
|
|
223
249
|
);
|
|
224
250
|
|
|
225
251
|
// Base Tailwind classes for pagination buttons
|
|
226
252
|
btn.classList.add(
|
|
227
|
-
'px-2',
|
|
228
|
-
'
|
|
253
|
+
'px-2',
|
|
254
|
+
'py-1',
|
|
255
|
+
'text-sm',
|
|
256
|
+
'border',
|
|
257
|
+
'border-gray-300',
|
|
258
|
+
'bg-transparent',
|
|
259
|
+
'min-w-[2rem]',
|
|
260
|
+
'inline-flex',
|
|
261
|
+
'items-center',
|
|
262
|
+
'justify-center',
|
|
263
|
+
'transition-colors',
|
|
264
|
+
'hover:bg-gray-100',
|
|
229
265
|
);
|
|
230
266
|
|
|
231
267
|
// active page styling
|
|
232
268
|
if (pageObj.active) {
|
|
233
|
-
btn.classList.add('bg-green-100','text-green-600','border-transparent');
|
|
269
|
+
btn.classList.add('bg-green-100', 'text-green-600', 'border-transparent');
|
|
234
270
|
btn.setAttribute('aria-current', 'page');
|
|
235
271
|
} else {
|
|
236
272
|
btn.removeAttribute('aria-current');
|
|
237
|
-
btn.classList.remove('bg-green-100','text-green-600','border-transparent');
|
|
273
|
+
btn.classList.remove('bg-green-100', 'text-green-600', 'border-transparent');
|
|
238
274
|
}
|
|
239
275
|
|
|
240
276
|
// disabled styling
|
|
241
277
|
if (btn.disabled || btn.getAttribute('aria-disabled') === 'true') {
|
|
242
|
-
btn.classList.add('opacity-60','cursor-default','pointer-events-none');
|
|
278
|
+
btn.classList.add('opacity-60', 'cursor-default', 'pointer-events-none');
|
|
243
279
|
} else {
|
|
244
|
-
btn.classList.remove('opacity-60','cursor-default','pointer-events-none');
|
|
280
|
+
btn.classList.remove('opacity-60', 'cursor-default', 'pointer-events-none');
|
|
245
281
|
}
|
|
246
282
|
|
|
247
283
|
if (pageObj.name === '…' && typeof pageObj['jump'] === 'number') {
|
|
@@ -250,7 +286,7 @@
|
|
|
250
286
|
// restore native tooltip for compatibility (browser tooltip)
|
|
251
287
|
btn.setAttribute('title', `Jump to page ${pageObj['jump']}`);
|
|
252
288
|
// visually indicate ellipsis (smaller text) via Tailwind
|
|
253
|
-
btn.classList.add('text-sm','opacity-90');
|
|
289
|
+
btn.classList.add('text-sm', 'opacity-90');
|
|
254
290
|
}
|
|
255
291
|
});
|
|
256
292
|
}
|
|
@@ -269,7 +305,10 @@
|
|
|
269
305
|
if (!paginationContainerEl) return;
|
|
270
306
|
const run = () => requestAnimationFrame(() => requestAnimationFrame(() => decorateEllipsis()));
|
|
271
307
|
_observer = new MutationObserver(run);
|
|
272
|
-
const root =
|
|
308
|
+
const root =
|
|
309
|
+
paginationContainerEl instanceof Element
|
|
310
|
+
? paginationContainerEl
|
|
311
|
+
: document.querySelector(`[data-pagination-id="${paginationId}"]`);
|
|
273
312
|
if (root) _observer.observe(root, { childList: true, subtree: true, attributes: true });
|
|
274
313
|
// initial run
|
|
275
314
|
run();
|
|
@@ -302,7 +341,7 @@
|
|
|
302
341
|
|
|
303
342
|
// Map clicked li to decide what to do. If clicked is not a page button (i.e. prev/next),
|
|
304
343
|
// do nothing here because the Pagination component already calls the `previous`/`next` callbacks.
|
|
305
|
-
const clickedIsPage = pageLis.some(p => p.li === li);
|
|
344
|
+
const clickedIsPage = pageLis.some((p) => p.li === li);
|
|
306
345
|
if (!clickedIsPage) {
|
|
307
346
|
return; // rely on Pagination's built-in previous/next handling to avoid double-invocation
|
|
308
347
|
}
|
|
@@ -310,7 +349,7 @@
|
|
|
310
349
|
const pageList = pages();
|
|
311
350
|
|
|
312
351
|
// Map clicked li to page index
|
|
313
|
-
const pageIdx = pageLis.findIndex(p => p.li === li);
|
|
352
|
+
const pageIdx = pageLis.findIndex((p) => p.li === li);
|
|
314
353
|
if (pageIdx < 0 || pageIdx >= pageList.length) return;
|
|
315
354
|
const pageObj = pageList[pageIdx];
|
|
316
355
|
if (!pageObj) return;
|
|
@@ -334,16 +373,16 @@
|
|
|
334
373
|
<div class="flex items-center gap-1 relative">
|
|
335
374
|
<button
|
|
336
375
|
type="button"
|
|
337
|
-
class={
|
|
338
|
-
"inline-flex items-center gap-2 px-3 py-1 text-sm rounded-md border transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:ring-green-500 dark:focus-visible:ring-green-400 " +
|
|
376
|
+
class={'inline-flex items-center gap-2 px-3 py-1 text-sm rounded-md border transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:ring-green-500 dark:focus-visible:ring-green-400 ' +
|
|
339
377
|
(filtersVisible
|
|
340
378
|
? 'bg-slate-100 dark:bg-slate-700 border-slate-300 dark:border-slate-600 text-slate-900 dark:text-slate-100'
|
|
341
|
-
: 'bg-transparent border-transparent text-slate-700 dark:text-slate-300 hover:bg-slate-100 dark:hover:bg-slate-700')
|
|
342
|
-
}
|
|
379
|
+
: 'bg-transparent border-transparent text-slate-700 dark:text-slate-300 hover:bg-slate-100 dark:hover:bg-slate-700')}
|
|
343
380
|
aria-pressed={filtersVisible}
|
|
344
381
|
aria-label="Toggle filters"
|
|
345
382
|
title="Filters"
|
|
346
|
-
onclick={() => {
|
|
383
|
+
onclick={() => {
|
|
384
|
+
filtersVisible = !filtersVisible;
|
|
385
|
+
}}
|
|
347
386
|
>
|
|
348
387
|
<!-- Funnel icon (Flowbite icon component) -->
|
|
349
388
|
<FilterOutline class="h-4 w-4 shrink-0" />
|
|
@@ -352,12 +391,23 @@
|
|
|
352
391
|
|
|
353
392
|
{#if activeFilterCount() > 0}
|
|
354
393
|
<!-- positioned badge -->
|
|
355
|
-
<span
|
|
394
|
+
<span
|
|
395
|
+
class="absolute top-0 right-0 -translate-y-1/2 translate-x-1/2 inline-flex items-center justify-center h-5 min-w-[1.25rem] px-1.5 rounded-full bg-green-600 text-white text-xs font-medium"
|
|
396
|
+
>{activeFilterCount()}</span
|
|
397
|
+
>
|
|
356
398
|
{/if}
|
|
357
399
|
</div>
|
|
358
400
|
|
|
359
|
-
<Search
|
|
360
|
-
|
|
401
|
+
<Search
|
|
402
|
+
size="sm"
|
|
403
|
+
bind:value={search}
|
|
404
|
+
placeholder="Search..."
|
|
405
|
+
clearable
|
|
406
|
+
clearableOnClick={() => {
|
|
407
|
+
search = '';
|
|
408
|
+
searchchange?.({ search });
|
|
409
|
+
}}
|
|
410
|
+
/>
|
|
361
411
|
</div>
|
|
362
412
|
</div>
|
|
363
413
|
|
|
@@ -369,14 +419,17 @@
|
|
|
369
419
|
|
|
370
420
|
<!-- Per-page selector -->
|
|
371
421
|
<div class="flex items-center gap-2">
|
|
372
|
-
<Select id={perPageSelectId} size="sm"
|
|
373
|
-
items={perPageOptions} bind:value={perPage}
|
|
374
|
-
/>
|
|
422
|
+
<Select id={perPageSelectId} size="sm" items={perPageOptions} bind:value={perPage} />
|
|
375
423
|
</div>
|
|
376
424
|
</div>
|
|
377
425
|
|
|
378
426
|
<!-- Pagination navigation: use Pagination with chevrons and page buttons -->
|
|
379
|
-
<div
|
|
427
|
+
<div
|
|
428
|
+
bind:this={paginationContainerEl}
|
|
429
|
+
data-pagination-id={paginationId}
|
|
430
|
+
class="flex items-center gap-0.5"
|
|
431
|
+
aria-label="Pagination"
|
|
432
|
+
>
|
|
380
433
|
<Pagination
|
|
381
434
|
pages={pages()}
|
|
382
435
|
{previous}
|
|
@@ -387,16 +440,40 @@
|
|
|
387
440
|
{#snippet prevContent()}
|
|
388
441
|
<span class="sr-only">Previous</span>
|
|
389
442
|
<!-- Left chevron SVG (replaces ChevronLeftOutline) -->
|
|
390
|
-
<svg
|
|
391
|
-
|
|
443
|
+
<svg
|
|
444
|
+
class="shrink-0 h-6 w-6"
|
|
445
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
446
|
+
fill="none"
|
|
447
|
+
viewBox="0 0 24 24"
|
|
448
|
+
stroke="currentColor"
|
|
449
|
+
aria-hidden="true"
|
|
450
|
+
>
|
|
451
|
+
<path
|
|
452
|
+
stroke-linecap="round"
|
|
453
|
+
stroke-linejoin="round"
|
|
454
|
+
stroke-width="1"
|
|
455
|
+
d="M15 19l-7-7 7-7"
|
|
456
|
+
/>
|
|
392
457
|
</svg>
|
|
393
458
|
{/snippet}
|
|
394
459
|
|
|
395
460
|
{#snippet nextContent()}
|
|
396
461
|
<span class="sr-only">Next</span>
|
|
397
462
|
<!-- Right chevron SVG (replaces ChevronRightOutline) -->
|
|
398
|
-
<svg
|
|
399
|
-
|
|
463
|
+
<svg
|
|
464
|
+
class="shrink-0 h-6 w-6"
|
|
465
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
466
|
+
fill="none"
|
|
467
|
+
viewBox="0 0 24 24"
|
|
468
|
+
stroke="currentColor"
|
|
469
|
+
aria-hidden="true"
|
|
470
|
+
>
|
|
471
|
+
<path
|
|
472
|
+
stroke-linecap="round"
|
|
473
|
+
stroke-linejoin="round"
|
|
474
|
+
stroke-width="1"
|
|
475
|
+
d="M9 5l7 7-7 7"
|
|
476
|
+
/>
|
|
400
477
|
</svg>
|
|
401
478
|
{/snippet}
|
|
402
479
|
</Pagination>
|
|
@@ -404,15 +481,20 @@
|
|
|
404
481
|
</div>
|
|
405
482
|
</div>
|
|
406
483
|
|
|
407
|
-
<FiltersModal
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
484
|
+
<FiltersModal
|
|
485
|
+
bind:open={filtersVisible}
|
|
486
|
+
{columnFilters}
|
|
487
|
+
{activeFilters}
|
|
488
|
+
filterChange={(...args) => {
|
|
489
|
+
try {
|
|
490
|
+
return filterChange(...args);
|
|
491
|
+
} catch (e) {}
|
|
492
|
+
}}
|
|
493
|
+
{direction}
|
|
494
|
+
{showCounts}
|
|
495
|
+
{className}
|
|
414
496
|
/>
|
|
415
497
|
|
|
416
498
|
<style>
|
|
417
|
-
|
|
418
|
-
</style>
|
|
499
|
+
@import '../lib/dist/styles.css';
|
|
500
|
+
</style>
|