mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-20 12:23:02 +00:00
feat(data-table): pass rows through DataTable context
This commit is contained in:
parent
ceb7abf2e9
commit
a419307c91
1 changed files with 17 additions and 13 deletions
|
@ -140,6 +140,7 @@
|
||||||
sortDirection: "none",
|
sortDirection: "none",
|
||||||
});
|
});
|
||||||
const headerItems = writable([]);
|
const headerItems = writable([]);
|
||||||
|
const tableRows = writable(rows);
|
||||||
const thKeys = derived(headerItems, () =>
|
const thKeys = derived(headerItems, () =>
|
||||||
headers
|
headers
|
||||||
.map(({ key }, i) => ({ key, id: key }))
|
.map(({ key }, i) => ({ key, id: key }))
|
||||||
|
@ -155,6 +156,7 @@
|
||||||
sortHeader,
|
sortHeader,
|
||||||
tableSortable,
|
tableSortable,
|
||||||
batchSelectedIds,
|
batchSelectedIds,
|
||||||
|
tableRows,
|
||||||
resetSelectedRowIds: () => {
|
resetSelectedRowIds: () => {
|
||||||
selectAll = false;
|
selectAll = false;
|
||||||
selectedRowIds = [];
|
selectedRowIds = [];
|
||||||
|
@ -173,7 +175,7 @@
|
||||||
let refSelectAll = null;
|
let refSelectAll = null;
|
||||||
|
|
||||||
$: batchSelectedIds.set(selectedRowIds);
|
$: batchSelectedIds.set(selectedRowIds);
|
||||||
$: rowIds = rows.map((row) => row.id);
|
$: rowIds = $tableRows.map((row) => row.id);
|
||||||
$: expandableRowIds = rowIds.filter(
|
$: expandableRowIds = rowIds.filter(
|
||||||
(id) => !nonExpandableRowIds.includes(id)
|
(id) => !nonExpandableRowIds.includes(id)
|
||||||
);
|
);
|
||||||
|
@ -193,23 +195,25 @@
|
||||||
$: if (radio || batchSelection) selectable = true;
|
$: if (radio || batchSelection) selectable = true;
|
||||||
$: tableSortable.set(sortable);
|
$: tableSortable.set(sortable);
|
||||||
$: headerKeys = headers.map(({ key }) => key);
|
$: headerKeys = headers.map(({ key }) => key);
|
||||||
$: rows = rows.map((row) => ({
|
$: tableRows.set(
|
||||||
...row,
|
rows.map((row) => ({
|
||||||
cells: headerKeys.map((key, index) => ({
|
...row,
|
||||||
key,
|
cells: headerKeys.map((key, index) => ({
|
||||||
value: resolvePath(row, key),
|
key,
|
||||||
display: headers[index].display,
|
value: resolvePath(row, key),
|
||||||
})),
|
display: headers[index].display,
|
||||||
}));
|
})),
|
||||||
$: sortedRows = rows;
|
}))
|
||||||
|
);
|
||||||
|
$: sortedRows = [...$tableRows];
|
||||||
$: ascending = $sortHeader.sortDirection === "ascending";
|
$: ascending = $sortHeader.sortDirection === "ascending";
|
||||||
$: sortKey = $sortHeader.key;
|
$: sortKey = $sortHeader.key;
|
||||||
$: sorting = sortable && sortKey != null;
|
$: sorting = sortable && sortKey != null;
|
||||||
$: if (sorting) {
|
$: if (sorting) {
|
||||||
if ($sortHeader.sortDirection === "none") {
|
if ($sortHeader.sortDirection === "none") {
|
||||||
sortedRows = rows;
|
sortedRows = $tableRows;
|
||||||
} else {
|
} else {
|
||||||
sortedRows = [...rows].sort((a, b) => {
|
sortedRows = [...$tableRows].sort((a, b) => {
|
||||||
const itemA = ascending
|
const itemA = ascending
|
||||||
? resolvePath(a, sortKey, "")
|
? resolvePath(a, sortKey, "")
|
||||||
: resolvePath(b, sortKey, "");
|
: resolvePath(b, sortKey, "");
|
||||||
|
@ -236,7 +240,7 @@
|
||||||
page && pageSize
|
page && pageSize
|
||||||
? rows.slice((page - 1) * pageSize, page * pageSize)
|
? rows.slice((page - 1) * pageSize, page * pageSize)
|
||||||
: rows;
|
: rows;
|
||||||
$: displayedRows = getDisplayedRows(rows, page, pageSize);
|
$: displayedRows = getDisplayedRows($tableRows, page, pageSize);
|
||||||
$: displayedSortedRows = getDisplayedRows(sortedRows, page, pageSize);
|
$: displayedSortedRows = getDisplayedRows(sortedRows, page, pageSize);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue