Skip to content

Commit 11cb3cd

Browse files
committed
docs(table): explain the importance of having ids
1 parent 98f862f commit 11cb3cd

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

src/components/table/examples/table-remote.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { data, Bird } from './birds';
44
import { capitalize } from 'lodash-es';
55

66
const NETWORK_DELAY = 500;
7+
type BirdRow = Bird & { id: string };
78

89
/**
910
* Remote sorting and pagination
@@ -19,9 +20,9 @@ export class TableExampleRemote {
1920
private columns: Array<Column<Bird>> = [];
2021

2122
@State()
22-
private currentData: object[] = [];
23+
private currentData: BirdRow[] = [];
2324

24-
private allData: object[] = data;
25+
private allData: Bird[] = data;
2526

2627
private pageSize = 10;
2728

@@ -106,7 +107,15 @@ export class TableExampleRemote {
106107
setTimeout(() => {
107108
const start = (this.currentPage - 1) * this.pageSize;
108109
const end = start + this.pageSize;
109-
this.currentData = this.allData.slice(start, end);
110+
111+
this.currentData = this.allData
112+
.slice(start, end)
113+
.map((item, index) => ({
114+
...item,
115+
// Provide a stable id to keep scroll position and selection
116+
// intact while the remote dataset refreshes.
117+
id: `${item.binominalName}-${start + index}`,
118+
}));
110119
}, NETWORK_DELAY);
111120
}
112121

src/components/table/examples/table.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ import { Column } from '@limetech/lime-elements';
33
import { Person, persons } from './persons';
44

55
/**
6+
* Basic example
7+
*
8+
* :::note
9+
* Each object is recommended to expose a stable `id` (string or number).
10+
* The table relies on that identifier to update rows in place so that
11+
* scroll position, focus, and selections remain intact across renders.
12+
*
13+
* Rows without an `id` are treated as new data on every update.
14+
* Therefore if the user clicks on a row at the bottom of the table,
15+
* it will be treated as new data and will force a full redraw,
16+
* resulting in the table to scroll to the top again.
17+
* :::
618
*
719
* @sourceFile persons.ts
820
*/

src/components/table/table.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ const FIRST_PAGE = 1;
5151
})
5252
export class Table {
5353
/**
54-
* Data to be displayed in the table
54+
* Data to be displayed in the table. Provide a stable `id` on each row to keep
55+
* scroll position, focus, and selections intact across updates.
5556
*/
5657
@Prop()
5758
public data: object[] = [];

0 commit comments

Comments
 (0)