Sorted and Searchable DataTable with Pagination from backend
See original GitHub issueWhat package(s) are you using?
-
carbon-components
-
carbon-components-react
Summary
The current DataTable is suitable for situations where you can download the entire dataset and use the built-in sorting mechanism. However, with large enough datasets, you must stream the data in a page at a time to prevent overloading either the client or the server. This is incompatible with the current built-in sorting mechanism, because it only sorts the page. It’s preferable to simply retrieve the current sort state, i.e.
const [sortHeader, setSortHeader] = useState('created');
const [sortDirection, setSortDirection] = useState('None');
const [page, setPage] = useState(1);
const [pageSize, setPageSize] = useState(10);
const start = pageSize * (page - 1)
const end = start + pageSize
const { data } = useSWR([start, end, sortHeader, sortDirection, 'myDataset'], getDataset, {suspense: true});
<DataTable
onSortChanged={(key: string, direction: 'ASC' | 'DESC' | 'None') => setSortHeader(key); setSortDirection(direction)}
...
/>
However, it doesn’t seem to be possible to do so. A similar story applies to search. What is the best way to scale the DataTable component so that its various functionality works well when populated by a backend that can perform its own search/sort/etc?
Relevant information
For the early stages of my work, I intend to just retrieve the entire dataset since it is still small. In the future, a feature may end up being requested that requires this functionality.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:8 (1 by maintainers)
Top GitHub Comments
This is indeed solved with a custom state manager, here is an actual example: https://github.com/carbon-design-system/carbon/tree/main/packages/react/examples/custom-data-table-state-manager
most likely you would need to implement this with a custom state manager, meaning you would be implementing that logic using Carbon table primitives like
Table
,TableHead
,TableRow
, etc. rather than using the built in state manager inDataTable
, if that makes senseupdated link to reflect
master
tomain
branch rename