Paginated Table¶
A searchable, filterable table of rows that reads a paged endpoint. It offers infinite scroll or Load more buttons, optional row selection with bulk actions, status badges, thumbnails, and clickable rows. It is a Filtered List whose items are rows from a server fetch: the search, the multiple selection with its select all and count, the reload button, opening a row, paging, and the live refresh that keeps unchanged rows all come from it, and the table adds its filter select, status cells, and the footer.
Decision¶
| When | Use |
|---|---|
| Any server side list too long to render at once | Paginated Table |
| Rows with column headings that sort, local or from a server | Data Table |
| Items that are not table rows, such as media cards | Filtered List |
| A short, ordered, editable list | Sortable Card List |
| A few fixed rows | Static Table |
Usage¶
<div id="contentTable"></div>
import { createPaginatedTable } from './configurationpage?name=myplugin_stolon.js';
var table = createPaginatedTable(view, shared, {
containerId: 'contentTable',
endpoint: 'ContentItems',
search: { placeholder: 'Search items' },
selection: { enabled: true, idKey: 'Id', onSelectionChange: function (ids) { updateBulkBar(ids.length); } },
pagination: { pageSize: 50 },
filters: { options: [{ value: 'Queued', label: 'Queued' }, { value: 'Errored', label: 'Errored' }] },
columns: [
{ key: 'Name', type: 'custom',
image: function (item) {
return function () {
return ApiClient.fetch({ url: ApiClient.getUrl('MyPlugin/Image', { itemId: item.Id }), type: 'GET' })
.then(function (response) { return response.blob(); });
};
}, imageShape: 'portrait',
render: function (item) {
return '<div class="stolon-table-item-info"><div class="stolon-table-item-title">' + shared.escapeHtml(item.Name) + '</div>' +
'<div class="stolon-table-item-sub">' + shared.escapeHtml(item.Path) + '</div></div>';
} },
{ key: 'Status', type: 'status' }
],
getDisplayStatus: function (item, value) { return value; },
getStatusClass: function (item, value) { return value; },
actions: { onRowClick: function (item) { openDetail(item); }, onReload: function () { loadHealth(); } },
emptyState: { message: 'No items found.' }
});
table.load();
The table is built from Stolon parts. The search is a Search Field, the rows are a List of bordered rows that loads more at its end, the select all, the count, and the bulk actions are the Filtered List's selection bar, the reload button is a Row Button, Load more and Load all are secondary Buttons, the filter select takes the shared select look, a status cell is the bag's statusBadge inside a button, and a thumbnail is the shared stolon-thumb frame. A data view holds the query, the pages, and the loading state, and drops an answer that a newer load replaced.
The endpoint answers a PagedResult, { Items: [...], TotalCount: n }, for endpoint?skip=0&take=50, plus &search=text when searching and &filter=value when a filter is set, or one parameter per filter when there are several. The request goes through shared.apiRequest, so apiPrefix applies.
The handle has load, reload, refresh, setSearch, setFilter, setFilterValue, setFilterOptionVisible, getItems, getTotalCount, getSelectedIds, getSelectedItems, clearSelection, getBulkActionsContainer, getControlsContainer, disconnectObserver, reconnectObserver, and destroy.
Classes: stolon-table-wrapper, stolon-table-controls, stolon-table-search, stolon-table-filter, stolon-table-selection-header, stolon-table-select-all-label, stolon-table-select-all, stolon-table-select-all-text, stolon-table-selected-count, stolon-table-bulk-actions, stolon-table-header-spacer, stolon-table-reload-btn, stolon-table-body, stolon-table-row, stolon-table-row-clickable, stolon-table-cell, stolon-table-cell-checkbox, stolon-table-cell-status, stolon-table-cell-with-thumb, stolon-table-thumb, stolon-table-row-checkbox, stolon-table-item-info, stolon-table-item-title, stolon-table-item-sub, stolon-table-item-error, stolon-table-status-btn, stolon-table-footer, stolon-table-item-count, stolon-table-footer-actions, stolon-table-load-more, stolon-table-load-all, stolon-table-loading.
States and Modifiers¶
- Nested option objects merge over the defaults.
pagination.loadMore: trueturns the scroll observer off, and the footer shows Load more and Load all. - A column is
{ key, type, className, hidden, render, label, image, imageShape }. A plain cell showskeyas text.type: 'custom'inserts the HTML thatrender(item, value)returns.type: 'status'puts the bag'sstatusBadgeinside a button. - A column with
image(item)draws a thumbnail before the cell's content, in the shapeimageShapenames,portrait(default),wide,square, orround. The thumbnail shows the shape's placeholder icon until the image loads, and again when it fails. The cell takesstolon-table-cell-with-thumb. image(item)returns an address, or a loader, a function that returns an address, a Blob, or a promise of one, as a Media Card image does. A loader is called only when its row nears the screen, so a long page fetches only what is seen, such as an image read through a plugin's own proxy endpoint. A Blob shows through an object URL, which is revoked when its row leaves the table and ondestroy().filterstakes{ name, label, options, buildParams, onChange, initialValue, allValue, allLabel }. The first choice is "All". The first load already carriesinitialValue.filtersmay also be an array of those, for a select each. Each value goes on the request under the filter'sname,filterfor the first by default, thenfilter2and on, or through its ownbuildParams.labelis the select's aria-label.setFilter(value, name)andsetFilterValue(value, name)act on the named filter, or the first.setFilterOptions(options, name)swaps a filter's choices in place, drawing its select when it had none, and a picked choice that is gone falls back to All and reloads.searchtakes{ enabled, placeholder, debounceMs }with a default of 300 ms.selectionadds the checkbox column and the selection header with select all, the count, bulk actions, and a reload button. Select all checks or clears the loaded rows and shows an indeterminate state while only some of them are selected.- A selected row has
selected, an accent wash. A re-render keeps it for every id still selected. - A page that builds its own
stolon-table-rowmarkup, outsidecreatePaginatedTable, shows a picked row by settingstolon-selectedon it, such asrow.classList.toggle('stolon-selected', picked). load()starts from the first page.reload()clears the selection, then loads.refresh()loads and keeps the selection.clearSelection()always reports toonSelectionChange.- A newer load supersedes one in flight, and the old answer is dropped.
- A clickable row has
tabindex="0"and answers Enter and Space when the row itself has focus. The status badge button opens the row too, and neither fires the click twice. - The footer reads "1 item", "N items", "Showing X of N items" while more remain, or "X/N items" in Load more mode.
- Until the first answer the rows say "Loading", and with no rows they say the
emptyStatemessage. A next page shows "Loading" under the rows. stolon-table-loadinggoes on the host element while the first page loads. The rows drop to 60 percent opacity and take no pointer events, and the search and filters stay usable.- At 768px and below the toolbar stacks. At 600px and below a row wraps: the first column keeps at least 60 percent of the width, and the other columns move to the next line, with the status cell at the end.
sizeissmall,medium(default), orlarge, and sizes its fields on the size scale.
Gotchas¶
customrender output is not escaped, so escape inside it.- Nothing loads by itself. Call
load()once. A failed request is rethrown fromload()after the state is restored, so catch it if you callload()yourself. - Ids are strings in the selection and in
data-id. The rows answer clicks, keys, and checkbox changes through listeners on the body, so a re-render needs no rebinding. - The reload button is part of the selection header, so a table without
selection.enabledhas no reload button. - The column
labelis written asdata-labelon each cell, but the table draws no header row, so nothing reads it. - The default labels "Select all", "Reload", "Load more", and "Load all" have no option to replace them.
- Call
destroy()onviewhide. It stops loading at the end of the list and drops a pending search.reconnectObserver()turns loading at the end back on. rowClass(item, index)marks a row from its first draw, such asstolon-selectedfor a row picked elsewhere, which takes the table's selected look. It is the Filtered List'sitemClass, so it follows every refresh. With checkbox selection on, the selection ownsstolon-selected, so give a picked row another name.
Reference¶
Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.
createPaginatedTable¶
function createPaginatedTable<T = any>(view: ParentNode, shared: Shared, options: TableOptions<T>): TableHandle<T>
Builds a searchable, filterable, infinite scrolling table backed by an endpoint that returns Items and TotalCount. It renders into the container and returns a controller. It is a Filtered List whose items are rows from a server fetch, with the Filtered List's search, multiple selection, select all, open action, and paging, and its own filter select, status cells, and footer.
| Option | Type | What it does |
|---|---|---|
containerId (required) |
string |
id of the element the table renders into [required] |
endpoint (required) |
string |
path that takes skip, take, search, and filter parameters [required] |
columns (required) |
TableColumn<T>[] |
[{ key, type: 'status' | 'custom', render(item, value), label, className, hidden, image(item), imageShape }], where label is written on each cell as data-label, and image(item) returns the thumbnail drawn before the cell in imageShape, as an address or a function that returns an address, a Blob, or a promise of one, loaded lazily and freed when its row goes, 'portrait' (default), 'wide', 'square', or 'round' [required] |
getDisplayStatus |
(item: T, value: any): string |
function(item, value) returning the text of a status column [optional] |
getStatusClass |
(item: T, value: any): unknown |
function(item, value) returning the state class of a status column [optional] |
rowClass |
(item: T, index: number): unknown |
function(item, index) returning class names for the row, such as 'stolon-selected' for a row picked elsewhere, set after every draw and refresh through the Filtered List's itemClass [optional] |
selection |
TableSelection<T> |
{ enabled, idKey, onSelectionChange(ids) }, where idKey is a property name or a function (default 'id') |
pagination |
TablePagination |
{ pageSize (default 50), loadMore }, where loadMore swaps infinite scroll for Load more and Load all buttons |
filters |
TableFilters | TableFilters[] | null |
{ name, label, options: [{ value, label, id, hidden }], buildParams(value), onChange(value), initialValue, allValue, allLabel }, an array of them for several selects, or null for none. name is the query parameter (default 'filter' for the first, then 'filter2' and on), and label the select's aria-label |
search |
TableSearch |
{ enabled, placeholder, debounceMs } (defaults true, 'Search items', 300) |
actions |
TableActions<T> |
{ onRowClick(item), onReload() } |
emptyState |
{ message?: string; } |
{ message } (default 'No items found.') |
size |
'small' | 'medium' | 'large' |
'small', 'medium', or 'large' for its fields (default 'medium') |
| Handle member | Type |
|---|---|
load |
(): Promise<PagedResult<T> | null> |
reload |
(): Promise<PagedResult<T> | null> |
setFilter |
(value: string, name?: string): Promise<PagedResult<T> | null> |
setSearch |
(query: string): Promise<PagedResult<T> | null> |
getSelectedIds |
(): string[] |
getSelectedItems |
(): T[] |
clearSelection |
(): void |
getItems |
(): T[] |
getTotalCount |
(): number |
getBulkActionsContainer |
(): HTMLElement | null | undefined |
getControlsContainer |
(): HTMLElement | null | undefined |
disconnectObserver |
(): void |
reconnectObserver |
(): void |
refresh |
(): Promise<PagedResult<T> | null> |
destroy |
(): void |
setFilterValue |
(value: string, name?: string): void |
setFilterOptions |
(options: TableFilterOption[] | null | undefined, name?: string): void |
setFilterOptionVisible |
(optionId: string, visible: boolean): void |
| Type | What it is |
|---|---|
TableColumn |
One column of a paginated table. label is written on each cell as data-label, and image puts a thumbnail of imageShape before the cell's content, from an address or a function that returns an address, a Blob, or a promise of one. |
TableFilterOption |
One option of the filter select. A hidden option starts out of view until setFilterOptionVisible shows it. |
TableFilters |
One filter select. buildParams runs only for a value other than empty, and initialValue is the filter the first load carries. name is the query parameter, and label the select's aria-label. |
TableSelection |
Row selection. idKey is a property name or a function that returns a row's id. |
TablePagination |
The page size, and loadMore to swap infinite scroll for Load more and Load all buttons. |
TableSearch |
The search box and how long it waits after typing before it loads. |
TableActions |
onRowClick runs when a row or its status button is chosen, and onReload after the reload button's load. |
| Class | What it looks like |
|---|---|
stolon-table-loading |
Dims the rows while the first page loads, with the search and filters still usable |
stolon-table-status-btn |
A status badge from the bag's statusBadge inside a bare button that opens the row |
stolon-table-header-spacer |
Flexible gap that pushes the controls after it to the end of the header |
stolon-table-reload-btn.stolon-spinning |
The reload row button, whose icon turns while the table reloads |
stolon-table-body |
The rows, a list of bordered rows |
stolon-table-row |
Single table row |
stolon-list.stolon-rows |
A row inside a list, where the list draws the dividers |
stolon-table-cell |
Single cell within a row |
stolon-table-cell.stolon-table-cell-checkbox |
Narrow cell for a row checkbox |
stolon-table-cell.stolon-table-cell-status |
Fixed width cell that right aligns a status badge |
stolon-table-row-checkbox |
Checkbox that selects one row |
stolon-table-item-info |
Title over sub text block for a row cell |
stolon-table-item-title |
Medium weight title that clips with an ellipsis |
stolon-table-item-sub |
Small dimmed sub line that clips with an ellipsis |
stolon-table-item-error |
Truncated error subline shown under a row item when it failed |
stolon-table-footer |
Footer line showing the loaded and total counts |
stolon-table-item-count |
Dimmed count of the loaded and total rows |
stolon-table-footer-actions |
The Load More and Load All buttons, shown in the footer while more rows remain |
stolon-table-cell.stolon-table-cell-with-thumb |
Cell that lines a thumbnail up beside its text |
stolon-table-thumb |
A row thumbnail, a stolon-thumb frame in the column's imageShape, portrait by default |
Example¶
Example Code¶
<!-- title: Paginated Table -->
<div id="contentTable"></div>
<script type="module">
const base = await window.__baseReady;
const view = document.getElementById('view');
const shared = base.createShared(view, 'preview-plugin', 'Preview');
// Stands in for a plugin's image proxy: a short wait, then artwork as a Blob, or a failure every seventh row so its placeholder shows.
const HUES = [210, 280, 30, 140, 0];
function loadArt(item) {
return function () {
const n = Number(item.id.split('-')[1]) || 0;
return new Promise((resolve, reject) => setTimeout(() => {
if (n % 7 === 0) { reject(new Error('No artwork')); return; }
const svg = '<svg xmlns="http://www.w3.org/2000/svg" width="40" height="60"><rect width="40" height="60" fill="hsl(' + HUES[n % 5] + ',45%,45%)"/><text x="20" y="36" font-size="16" text-anchor="middle" fill="#fff" font-family="sans-serif">' + n + '</text></svg>';
resolve(new Blob([svg], { type: 'image/svg+xml' }));
}, 400 + (n % 4) * 200));
};
}
const table = base.createPaginatedTable(view, shared, {
containerId: 'contentTable',
endpoint: 'Items',
search: { placeholder: 'Search episodes' },
selection: { enabled: true, idKey: 'id' },
pagination: { pageSize: 10, loadMore: true },
filters: { options: [{ value: 'Queued', label: 'Queued' }, { value: 'Errored', label: 'Errored' }] },
columns: [
{ key: 'name', type: 'custom', image: loadArt, imageShape: 'portrait', render: (item) =>
'<div class="stolon-table-item-info"><div class="stolon-table-item-title">' + shared.escapeHtml(item.name) + '</div>' +
'<div class="stolon-table-item-sub">' + shared.formatSize(item.sizeBytes) + '</div></div>' },
{ key: 'status', type: 'status' }
],
getDisplayStatus: (item) => item.status,
getStatusClass: (item) => item.status,
actions: { onRowClick: (item) => shared.showAlert('Opened ' + item.name) },
emptyState: { message: 'No items found.' }
});
table.load();
</script>