Load tree children in pages
Since 1.6.0
The treeChildrenPaging option, the treeChildrenLoaded and treeChildrenError events, the beforeTreeChildrenLoad hook and the getTreeChildrenState, loadMoreTreeChildren and reloadTreeChildren methods were added.
When one folder has thousands of children you do not have to fetch them all at once: fetch them one page at a time. This adds paging to the existing lazy children (tree.hasChildren), so you give treeChildrenPaging.load instead of tree.loadChildren (using both is an invalid-option error).
createGrid(el, {
columns,
rows: topFolders,
rowKey: 'id',
tree: { parentKey: 'parentId', expandDepth: 0, hasChildren: (row) => row.folder },
treeChildrenPaging: {
pageSize: 50,
mode: 'more',
load: async ({ parentKey, page, pageSize, signal }) => {
const res = await fetch(`/api/children/${parentKey}?page=${page}&size=${pageSize}`, {
signal,
});
const { items, total } = await res.json();
return { rows: items, total };
},
},
});| Key | Meaning | Default |
|---|---|---|
pageSize | Children per page (1 to 1000) | 50 |
mode | 'more' waits for a click on the "Load more" line; 'infinite' loads by itself when the line comes near the viewport | 'more' |
load | Receives { parentKey, page, pageSize, signal } and resolves to { rows, total?, hasMore? } | required |
What load resolves to:
rows: the children of that page. Each row's parent key must beparentKey.total: how many children there are in all (when known). It is used for the "m of n" text and to decide the end.hasMore: whether another page exists. When omitted it compares withtotalif there is one, otherwise it usesrows.length >= pageSize.
First open
Like the existing lazy loading, page 0 is fetched when the user opens the node. A top row that is already open from the start (the default expandDepth is 1) is not fetched, so use expandDepth: 0.
The Load more line
Right below the last loaded child (including its descendants) a "Load more (10 of 50)" line appears. It goes away when there is no next page.
mode: 'more': click the button, or focus the line and press Enter or Space. Press↓on the last child to reach the line.mode: 'infinite': when the line comes within 3 rows of the last visible row, the next page is fetched after a short pause (0.12 s).- While loading it shows "Loading…" (
role="status",aria-busy); on failure it shows the error text (role="alert") and a ↻ button. The button or Enter asks for the same page again. - The line is a synthetic row outside selection, editing, export and sorting;
getDisplayRow(i)returns{ kind: 'more', parentKey, level, state }. Its indent uses logical properties, so it fits RTL. - The texts are the language-pack keys
treeMore,treeMoreNoTotal,treeMoreLoadingandtreeMoreError(ko, en, ja and zh).
Collapse, cache and reload
- Collapsing keeps the loaded children and the page record. Opening again does not call
loadagain. - Collapsing during a request aborts it with its
AbortSignaland drops a late response. If it was the first page the record is cleared, so the next open starts at page 0. api.reloadTreeChildren(parentKey)removes the loaded children, clears the record and, if the node is open, fetches page 0 right away.- Replacing all rows (
setRows) drops every request and record.destroy()stops requests and timers. - Keys that already exist in a response are ignored and reported in
treeChildrenError { duplicates }.
State, events and hook
api.getTreeChildrenState('photos'); // { loaded, total?, hasMore, loading, error? }
api.loadMoreTreeChildren('photos'); // the next page (or the page that failed)
api.on('treeChildrenLoaded', ({ parentKey, page, count }) => {});
api.on('treeChildrenError', ({ parentKey, page, message, duplicates }) => {});treeChildrenErroralso fires on a failed request, together with anerrorevent (children-load-failed).childrenLoadChanged(loading,loaded,error,cancelled) fires as it does for the existing lazy loading.- If the
beforeTreeChildrenLoad({ parentKey, page })hook returnsfalsethat page is not fetched. A throwing hook emits anerrorevent and loading goes on. - Calling a method while the option is off, or with an unknown key, throws
invalid-option.
Sorting and filtering
On a lazy node, sorting and filtering are applied on the client to the children loaded so far only. Pages not yet loaded must be sorted and filtered by the server, so pass your sort and filter conditions to load yourself and call reloadTreeChildren when they change.
Gallery example: tree-lazy-more
Gallery example: tree-lazy-infinite
Gallery example: tree-lazy-error-retry