mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-14 18:01:06 +00:00
fix(data-table): handle ToolbarSearch
filtering in DataTable
(#2037)
This commit is contained in:
parent
1acd713537
commit
3192824322
2 changed files with 36 additions and 24 deletions
|
@ -147,6 +147,10 @@
|
|||
const dispatch = createEventDispatcher();
|
||||
const batchSelectedIds = writable(false);
|
||||
const tableRows = writable(rows);
|
||||
|
||||
// Store a copy of the original rows for filter restoration.
|
||||
$: originalRows = [...rows];
|
||||
|
||||
$: thKeys = headers.reduce((a, c) => ({ ...a, [c.key]: c.key }), {});
|
||||
const resolvePath = (object, path) => {
|
||||
if (path in object) return object[path];
|
||||
|
@ -164,6 +168,36 @@
|
|||
selectedRowIds = [];
|
||||
if (refSelectAll) refSelectAll.checked = false;
|
||||
},
|
||||
filterRows: (searchValue, customFilter) => {
|
||||
const value = searchValue.trim().toLowerCase();
|
||||
|
||||
if (value.length === 0) {
|
||||
// Reset to original rows.
|
||||
tableRows.set(originalRows);
|
||||
return originalRows.map((row) => row.id);
|
||||
}
|
||||
|
||||
let filteredRows = [];
|
||||
|
||||
if (typeof customFilter === "function") {
|
||||
// Apply custom filter if provided.
|
||||
filteredRows = originalRows.filter((row) => customFilter(row, value));
|
||||
} else {
|
||||
// Default filter checks all non-id fields for a basic, case-insensitive match (non-fuzzy).
|
||||
filteredRows = originalRows.filter((row) => {
|
||||
return Object.entries(row)
|
||||
.filter(([key]) => key !== "id")
|
||||
.some(([key, _value]) => {
|
||||
if (typeof _value === "string" || typeof _value === "number") {
|
||||
return (_value + "")?.toLowerCase().includes(value);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
tableRows.set(filteredRows);
|
||||
return filteredRows.map((row) => row.id);
|
||||
},
|
||||
});
|
||||
|
||||
let expanded = false;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue