Skip to content

Commit f687cdf

Browse files
committed
docs(data-table): include on:sort usage
1 parent ce9b0b6 commit f687cdf

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

docs/src/pages/components/DataTable.svx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,8 @@ Set `size="compact"` for minimal row height.
893893

894894
Set `sortable` to `true` to enable column sorting. Disable sorting on specific columns by setting `sort: false` in the header object.
895895

896+
When a sortable header is clicked, the table dispatches a cancelable `sort` event with `event.detail` as `{ key, direction }` (the sort that would apply). `key` is the header `key` (or `null` when unsorted), and `direction` is `"none"`, `"ascending"`, or `"descending"`. Listen with `on:sort`.
897+
896898
<DataTable sortable
897899
headers="{[
898900
{ key: "name", value: "Name" },
@@ -944,8 +946,19 @@ Set `sortable` to `true` to enable column sorting. Disable sorting on specific c
944946
rule: "DNS delegation"
945947
},
946948
]}"
949+
on:sort={(e) => console.log(e.detail.key, e.detail.direction)}
947950
/>
948951

952+
## Sortable (server-side, preventDefault)
953+
954+
When rows and sort order come from an API, the canonical order is usually defined on the server (collation, nullable fields, joined columns, security filters).
955+
956+
Client-side sorting on the same payload can disagree with that behavior or waste work on large datasets. In those cases, call `preventDefault()` on the `sort` event so the table does not reorder `rows` or apply its own sort for that click. Use `event.detail.key` and `event.detail.direction` as your sort parameters (query string, GraphQL variables, RPC args), wait for the response, then assign the new `rows`. The sort indicators should match what the server applied: bind `sortKey` and `sortDirection` from the parent and set them after the response (as in the example), or pass them as one-way props if you do not need child-to-parent sync.
957+
958+
The framed example uses `setTimeout` to simulate a network request; `preventDefault()` runs first, then simulated “sorted” rows replace `rows` and the bound sort props update the header.
959+
960+
<FileSource src="/framed/DataTable/DataTableSortPreventDefault" />
961+
949962
## Sortable (sort always)
950963

951964
Sortable columns support three directions: `none` (unsorted, original row order), `ascending`, and `descending`. Clicking a column header cycles through them in order.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<script>
2+
import { DataTable } from "carbon-components-svelte";
3+
4+
// Mock data from the server.
5+
const unsortedFromServer = [
6+
{ id: "1", name: "Charlie", port: 3 },
7+
{ id: "2", name: "Alpha", port: 1 },
8+
{ id: "3", name: "Bravo", port: 2 },
9+
];
10+
11+
let rows = [...unsortedFromServer];
12+
let sortKey = null;
13+
let sortDirection = "none";
14+
15+
// Simulate fetching data from a remote server.
16+
function fetchDataFromServer(key, direction) {
17+
if (direction === "none" || key == null) {
18+
return [...unsortedFromServer];
19+
}
20+
const mul = direction === "ascending" ? 1 : -1;
21+
return [...unsortedFromServer].sort((a, b) => {
22+
const va = a[key];
23+
const vb = b[key];
24+
if (va === vb) return 0;
25+
return va < vb ? -mul : mul;
26+
});
27+
}
28+
29+
function fakeServerSort(key, direction) {
30+
return new Promise((resolve) => {
31+
setTimeout(() => resolve(fetchDataFromServer(key, direction)), 250);
32+
});
33+
}
34+
</script>
35+
36+
<DataTable
37+
headers={[
38+
{ key: "name", value: "Name" },
39+
{ key: "port", value: "Port" },
40+
]}
41+
{rows}
42+
bind:sortKey
43+
bind:sortDirection
44+
sortable
45+
on:sort={async (e) => {
46+
e.preventDefault();
47+
const { key, direction } = e.detail;
48+
rows = await fakeServerSort(key, direction);
49+
sortKey = key;
50+
sortDirection = direction;
51+
}}
52+
/>

0 commit comments

Comments
 (0)