Overview
A table is a <table class="nds-table"> in a .nds-table-wrapper, with a <thead>, a <tbody>, and an optional <tfoot> for a total. Markup turns on the rest: sort buttons in the header, row checkboxes, a column menu, and detail rows under a row. Tables work with Pagination, Filter, Selection and Export on the same rows.
Pick the options by the data. Records adds a count, search, filters, a column menu, export and pages. Compact fits one short line in each cell. Interactive fits a row that opens something. A Total row holds the sum of a column. The builder and the Variants table in this page’s source list every option.
Pick another component when:
- the content is label and value pairs: Definition List
- each record is a tile with an image or actions: Cards in a Grid
- one number with a trend is the content: Metric
Markup
| Reference | Service | Submitted | Status | Fee |
|---|---|---|---|---|
| REQ-2026-118 | Commercial registration | 22/07/2026 | Pending | 1200 |
| REQ-2026-117 | Building permit | 21/07/2026 | Approved | 4500 |
| REQ-2026-116 | Passport renewal | 20/07/2026 | Approved | 300 |
| REQ-2026-115 | Vehicle registration | 18/07/2026 | Rejected | 150 |
Variants
Every option goes on the outer <table>, never on a table in a sub-row.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Standard (default) | — | — | A plain table in its scroll box, .nds-table-wrapper. The script adds nothing else |
| Structure | Records (hint: Adds search, filter, sort, selection, pages and detail rows) | canon #tbl-records |
— | The records screen most services need: a count, per page, a column menu, export, search, filter, sort buttons, row checkboxes, detail rows, a row menu and pages. Start from it and delete the parts you do not need |
| Density | Standard (default) | — | — | 64px rows, for cells with tags, buttons or two lines |
| Density | Compact | .nds-compact |
#tbl-requests |
48px rows, for dense data with one line in each cell |
| Density | Compact | .nds-compact |
#tbl-rec-requests |
|
| Interactive | Interactive (hint: Rows highlight on hover) | .nds-interactive |
#tbl-requests |
Highlights the row under the pointer. Use it only when a row does something: it opens, selects or links |
| Interactive | Interactive (hint: Rows highlight on hover) | .nds-interactive |
#tbl-rec-requests |
|
| Center | Center (hint: Cell text is centered) | .nds-center |
#tbl-requests |
Centers the text in every cell. For short values, such as a score sheet |
| Center | Center (hint: Cell text is centered) | .nds-center |
#tbl-rec-requests |
|
| Baseline | Baseline (hint: Cells line up on their first text line) | .nds-baseline |
#tbl-requests |
For a table with fields in its cells: a field that shows its message grows the row downward, and the other cells stay in line. A row with two lines of text lines up on its first line |
| Baseline | Baseline (hint: Cells line up on their first text line) | .nds-baseline |
#tbl-rec-requests |
|
| Sub-rows | Several open (hint: Detail rows stay open together) | [data-state~="always-open"] |
#tbl-rec-requests |
Opens more than one detail row at a time. Records only |
| Total row | Total row (hint: A total under the rows) | canon #tbl-total |
#tbl-requests |
A <tfoot> row after the body, such as the sum of a column. The label may span the first columns with colspan. Shown on Standard. It works on any table |
| Loading | Loading (hint: Skeleton bars in place of the rows) | .nds-loading |
#tbl-requests |
Shows skeleton bars in place of the cells while new rows load. data-state="loading" does the same |
| Loading | Loading (hint: Skeleton bars in place of the rows) | .nds-loading |
#tbl-rec-requests |
Behavior
Sorting
A .nds-sort-btn in a header cell makes its column sortable. Each click moves the column through ascending, descending and the first order. Sort does the work, and sets aria-sort on the header cell. The script writes data-state="sorted-asc" or "sorted-desc" on the sorted header cell, which turns the icon. Write it in the markup when the server sends the rows already sorted: the script marks the column and does not reorder the rows. A sub-row stays under its row, and a paged table goes back to page 1.
A Row That Opens a Page
A <tr> cannot be a link, so put a real link in the row’s identifier cell and add nds-interactive to the table. The link serves the keyboard and screen readers. A small page script serves a click anywhere else in the row:
document.getElementById('requests-rows').addEventListener('click', function (e) {
// A control in the row, a sub-row or a text selection keeps its own click.
if (e.target.closest('a, button, input, label, select, textarea, .nds-sub')) return;
if (String(window.getSelection())) return;
var row = e.target.closest('tr');
var link = row && row.querySelector('a.nds-link');
if (link) link.click();
});
The listener sits on the <tbody>, so rows that the page adds later work too.
Row Selection
A checkbox in a header cell selects every row on the current page, and the script links it to the <tbody> with data-selection-target. A checked row gets data-state="selected" and a tinted background. The counter, the Select all link and the selection API belong to Selection.
Column Menu
A Dropmenu with data-columns-target names the id of a table. The script fills its [data-columns-list] with one checkbox per column the first time it opens. A column with data-columns-lock and the selection column stay off the list. A badge on the trigger counts the hidden columns. A row added later takes the hidden columns too. While a column is hidden, a Reset button in the menu shows them all. When the table has an id, the browser keeps the choice for the next visit. The browser drops it when the column count changes. Export skips a hidden column.
Sub-Rows
A <tr class="nds-sub"> placed right after a row is that row’s detail, most often a compact table. A data-sub-toggle button opens it: in the row, in the row’s menu, or inside the sub-row. aria-controls on the button names the sub-row’s id. One sub-row is open at a time. data-state="always-open" on the table lets several stay open.
To start one open, leave out its hidden attribute. Write aria-expanded="true" and data-state="open" on its button, and a colspan that spans every column on its cell. The script sets the colspan of every other sub-row. Every toggle of one sub-row shows the same state, and nds-menu-btn on a toggle turns its arrow while the sub-row is open. Closing hides the sub-row and keeps its content, so text typed in a field there stays. Sorting, column hiding and column alignment of the outer table skip a table in a sub-row.
Sub-Rows on Demand
A toggle whose row has no sub-row yet shows a spinner and fires nds:table:sub-request. Your code fetches the detail and calls NDS.Table.getRow(row).sub.setContent(html).open(). On an error, call close(), or the spinner keeps turning. A second click on a spinning toggle cancels the request through detail.signal. NDS never fetches the detail itself. The content stays, so the next open shows it with no new request. setContent() starts a table inside the content: for any other component in it, call NDS.init.mount(sub).
Column Alignment
data-align on a header cell aligns the header and every cell in its column: start, center or end. It also covers rows that arrive later from sort, filter or pages, and the total row. It works on a header with a sort button. A colspan in the body shifts the columns, so the cells after it align with the wrong header.
Total Row
A row in a <tfoot> after the <tbody> is a total. Write <th scope="row"> for its label; it may span the first columns with colspan. Any other colspan in the row shifts the columns. The row has the header background and semibold text, and each cell lines up with its column. Sorting, filters and pages leave it at the end, and a hidden column hides its cell or narrows the label. Export skips it, so a file holds only the data rows.
Loading
nds-loading on the table, or on its <tbody>, shows a pulsing bar in each body cell and hides the cell content. The header stays, and the column widths do not change. Remove it when the new rows are in place.
Pages and Filters
For Pagination, the <tbody> gets nds-paged-content and each row gets nds-page-item, but never a sub-row. For Filter, the <tbody> gets data-filter-items="tr", and the cells mark their values with data-filter. A sub-row hides with its row when a filter or a page change hides that row.
Records Parts
Records holds every part. To leave a part out, delete its markup and everything in its Also delete cell. Left behind, those attributes point at nothing. Delete a toolbar row when it is empty, and the toolbar when all its rows are gone. The script sets each sub-row’s colspan, so a deleted column needs no other change.
| Part | Holds | Also delete |
|---|---|---|
| Search and filter | the search box, .nds-filter and .nds-filter-applied |
data-filter-items on the <tbody>, and data-filter and data-filter-value on the cell spans. Keep the spans’ text. To drop one filter group only, delete its data-filter element, the <hr> next to it, and its data-filter on the cell spans |
| Sorting | .nds-col-header with its .nds-sort-btn, in each header cell |
data-sort-value on the cells. Keep the header label as the cell’s text |
| Selection | the checkbox header cell and the checkbox cell of every row | .nds-selection-view, [data-selection-all] and [data-selection-clear] in the count line, and data-selection-target on it |
| Pages | <nav class="nds-pagination"> and the per-page menu (data-per-page-target) |
nds-paged-content and --per-page on the <tbody>, nds-page-item on the rows, and .nds-records-view and data-paged-target on the count line. In .nds-selection-view, change data-paged-count to data-selection-total: Selection writes the row count in it |
| Column menu | the .nds-dropmenu with data-columns-target |
data-columns-lock on the header cell |
| Export | the .nds-dropmenu with the data-export buttons |
data-export-name on the table, and data-export-skip on the header cell |
| Sub-rows | the data-sub-toggle buttons and every tr.nds-sub |
— |
| Row menu | the .nds-dropmenu in the actions cell |
— |
| Actions column | the .nds-actions-column header cell and the cell of every row |
Delete it when neither sub-rows nor the row menu stay |
Keep Selection only when the page has a bulk action that acts on the selected rows; Records ships none. Delete the count line (.nds-toolbar-text) when neither Selection nor Pages stays. Keep the table’s id while the column menu or export stays.
Built-in Features
-
Auto Start
The script starts every
.nds-tableon the page. Sorting and selection start when the header holds their buttons or checkbox. -
Sideways Scroll
The
.nds-table-wrapperaround each table scrolls sideways when the columns do not fit, and the script adds one when a table has none. It checks again when the size changes or a hidden tab opens. A phone gets the same scroll: there is no stacked layout. While it scrolls, the keyboard can reach the scroll box, and a table with a<caption>or a label names it for a screen reader. -
Plain Table Styling
A
<table>with no class gets the table look, such as a table from a content editor. It gets no script. -
Striped Rows
Every second row is tinted. The stripes count only the rows on view, so they stay even after a filter, a page change or an open sub-row.
-
Start-Up Skeleton
The cells show skeleton bars until the script starts the table. A paged or filtered body keeps them until its own script starts.
-
Clean Cell Text
Sort and export read the cell's own text, not the text of the icons or buttons in it.
NDS.getCellText()gives the same value to your code.
Best Practices
- Use a table for records that share the same fields. For label and value pairs, use a Definition List.
- Do not use a table for page layout or for a grid of cards.
- Put the identifier in the first column, and the status and actions in the last columns.
- Keep header labels short. A long label widens its column on every row.
- Write
data-align="center"on the header of a column of short values, such as a status tag or a count, anddata-align="end"on a column of amounts. The actions column is centered already. - On a right-to-left page, never write
dir="ltr"ordir="auto"on a cell, a row or the table: the values jump to the left edge, out of line with the rest of the page. Wrap a value that reorders, such as a phone in groups or a leading+, in aspan dir="ltr"inside the cell. See Helpers. - Add sort buttons only to columns that users compare, such as dates, amounts and status.
- Set
--min-widthno wider than the room the page gives the table. Below it the box scrolls, so a larger value hides the last columns, such as the actions, on every desktop. The audit’stable-min-widthcheck finds one. - Write
data-sort-valueon a cell whose text sorts wrong: a date that is not in the table’s date format (one with a month name, or a Hijri date), an amount with a currency, a status that has an order. Write a date there asYYYY-MM-DD. A table whose dates use another order carries its owndata-date-format: see Date. - Put a total in a
<tfoot>, never as the last body row: sorting would move it, and Export would add it to the data. - Add
nds-baselineto a table with fields in its cells. Without it, a field that shows its message moves the other fields in its row. The audit’sform-row-aligncheck finds one. - Use selection only with a bulk action that acts on the selected rows.
- Give each row checkbox an
aria-labelthat names the row: “Select REQ-2026-118”. - Give each icon-only button an
aria-label, such as the sub-row toggle and the row menu. - Give the table an
idwhen it has a column menu, so the choice is kept. - Lock the identifier column with
data-columns-lock, so the user cannot hide it. - The sub-row background is a sunken surface. Put its content on a surface of its own, never straight on the sub-row: most often a compact table (
nds-table nds-compact), or a card (nds-card). - Put a short detail in a sub-row, not a whole record. Link to a page for the full record.
- Write a short sub-row detail in the markup: it needs no code. Load it on demand when it is large or the users rarely open it.
- Page a table of more than about 20 rows.
API
Other Classes
| Class | Element | Effect |
|---|---|---|
nds-table-wrapper |
the parent of .nds-table |
The scroll box. The canon writes it around every table: keep it. The script adds one to a table that has none by moving the table into it, which a framework view does not expect. See Tables in Init |
nds-col-header |
div in a th |
Holds the header label and its actions on one line |
nds-col-actions |
div in .nds-col-header |
Holds the header buttons, at 32px |
nds-sort-btn |
button in .nds-col-actions |
Makes its column sortable |
nds-sort-icon |
the icon in .nds-sort-btn |
Turns to show the sort order |
nds-sub |
tr |
A detail row. It belongs to the row right before it |
nds-actions-column |
th and td |
Shrinks the column to its content and centers it. data-align on its header changes the alignment |
nds-table-actions |
div in a cell |
A row of buttons with a small gap |
nds-checkbox-column |
th and td |
A 40px column with its content centered. A cell that holds a checkbox gets the same look with no class |
Data Attributes
| Attribute | Element | Effect |
|---|---|---|
id |
.nds-table |
The name the column menu uses. The browser keeps the hidden columns under it, in localStorage as nds-cols-{id}. Remove that key to forget the choice |
data-state~="always-open" |
.nds-table |
Lets several sub-rows stay open. Without it, opening one closes the others |
data-state~="loading" |
.nds-table or its <tbody> |
The same as the nds-loading class |
data-align |
th |
start, center or end. Aligns the header cell and its column |
data-state~="sorted-asc", "sorted-desc" |
a th with a sort button |
The sorted column. Write one at load when the rows are already in that order: the script does not reorder them. The script moves it on each sort, and removes it when the sort is cleared |
data-columns-lock |
th |
The column menu leaves this column off its list |
data-export-label |
th |
The column name in the column menu when the header shows no text. Export uses it too |
data-export-skip |
th |
The script sets it on a column the menu hides, and removes it when the column shows again. Write it yourself on a column that is never exported, such as the actions |
data-sort-value |
td |
The value to sort by, in place of the cell text. Export does not read it: it reads data-export-value |
data-sub-toggle |
a button in a row, in its menu, or in its sub-row |
Opens and closes the row’s sub-row |
aria-controls |
button[data-sub-toggle] |
The id of the sub-row. The script sets it on the row’s toggles when it is missing |
aria-expanded, data-state~="open" |
button[data-sub-toggle] |
The script sets both when the sub-row opens, and clears them when it closes. Write them at load on the toggle of a sub-row that starts open |
data-state~="loading" |
button[data-sub-toggle] |
The script sets it while a sub-request waits. setContent() and close() remove it |
hidden |
tr.nds-sub |
A closed sub-row. The script removes it to open the sub-row, and adds it to close |
data-columns-target |
.nds-dropmenu |
The id of the table whose columns the menu hides |
data-columns-list |
a fieldset in the menu |
The script fills it with one checkbox per column |
data-selection-target |
the header checkbox | The script sets it to the id of the <tbody>, and gives the <tbody> an id when it has none |
data-state~="selected" |
tr |
A selected row. Selection sets it |
data-state~="has-more", "at-start", "at-end" |
.nds-table-wrapper |
The script writes them: has-more while the table is wider than the box, and at-start and at-end at each end |
CSS Custom Properties
Set these on .nds-table.
| Property | Default | Controls |
|---|---|---|
--table-row-height |
64px, 48px with nds-compact |
Row height |
--table-cell-padding-block |
var(--spacing-md) |
Top and bottom padding of a cell |
--table-cell-padding-inline |
var(--spacing-xl) |
Start and end padding of a cell |
--table-sub-padding |
var(--spacing-xl) |
Padding of a sub-row cell |
--table-sub-background |
var(--background-surface-sunken) |
Background of a sub-row |
--min-width |
none | The smallest width of the table. Below it, the box scrolls |
--max-width |
100% |
The widest the scroll box gets. Set it in the table’s style attribute: the script copies it to the box |
Tokens
Set a token at :root, or on a wrapper to reach every table inside it. Give a token with a Dark mode value a dark override too: see Tokens.
Source: the table group in _sass/tokens/_components.scss.
| Token | Preview | Value |
|---|---|---|
--table-cell-border | var(--colors-neutral-300)Dark mode var(--colors-neutral-700) | |
--table-text-head | var(--colors-neutral-700)Dark mode var(--colors-neutral-300) | |
--table-text-body | var(--text-default)Dark mode var(--colors-neutral-200) | |
--table-background-hover-selected | var(--colors-primary-100)Dark mode var(--colors-primary-alpha-40) | |
--table-background-header | var(--colors-neutral-100)Dark mode var(--colors-neutral-900) | |
--table-background-row | var(--background-stripe)Dark mode var(--colors-neutral-850) | |
--table-background-cell | var(--background-default)Dark mode var(--colors-neutral-800) | |
--table-background-row-selected | var(--colors-primary-50)Dark mode var(--colors-primary-alpha-20) |
It also reads these shared tokens. A change to one also changes other components.
Source: the var() reads in _sass/components/_table.scss.
| Token | Preview | Value |
|---|---|---|
--background-surface-sunkenRead by --table-sub-background | var(--colors-neutral-200)Dark mode var(--colors-neutral-950) |
JavaScript
The NDS.Table script loads after the page shows. Until it loads, each call loads it and returns a promise of the result, so await a call whose result you use.
| Method | Effect |
|---|---|
NDS.Table.init() |
Starts every table and column menu that has not started yet. Call it after you add a new table. When only the rows change, call NDS.init.refresh(tbody): see Init |
NDS.Table.refresh() |
Measures every scroll box again, after a change the script cannot see |
NDS.Table.create(table) |
Starts one table: the scroll box, column alignment, sub-rows, sorting and selection. Use it for a table added after the page loads. It returns its instance, or the instance it already has. Returns null when the table has no <thead> or <tbody> |
NDS.Table.createResponsive(table) |
Adds the scroll box to one table and returns its instance |
NDS.Table.createColumnToggle(menu) |
Starts one column menu, and returns its instance, or the instance it already has. Returns null when the table or [data-columns-list] is missing |
NDS.Table.destroy(el) |
Undoes whichever of the three that lives on el: sorting, the scroll box or the column menu |
NDS.Table.setColumnHidden(table, index, hidden) |
Hides or shows the column at this index: it sets hidden on the header cell and on its cell in every row, and fires nds:table:columns. In a total row, it may narrow the label’s span instead. The checkbox and badge of the column menu follow it, but the menu does not save this change |
NDS.Table.getRow(tr).sub |
The handle of a row’s sub-row. tr is the row or its sub-row |
.sub.setContent(html) |
Puts your markup or node in the sub-row. It builds the sub-row when there is none, and stops the spinner. It returns the handle |
.sub.open(), .close(), .toggle() |
Opens, closes or flips the sub-row. open() with no sub-row fires nds:table:sub-request. Each returns the handle |
.sub.el |
The tr.nds-sub, or null |
| Instance | Method | Effect |
|---|---|---|
create() |
getSortColumn() |
The cell index of the sorted column, or -1 |
create() |
getSortOrder() |
"asc", "desc" or null |
create() |
resetSort() |
Clears the sort and puts the rows back in their first order |
create() |
destroy() |
Stops sorting, and removes the data-selection-target link and the <tbody> id it wrote. The table keeps its scroll box |
createResponsive() |
refresh() |
Measures this table again |
createResponsive() |
destroy() |
Stops the scroll checks, removes the column alignment, and marks the table as not started. A box the script built goes and the table returns to its place. A box you wrote stays |
| Event | Fired on | Detail |
|---|---|---|
nds:table:columns |
.nds-table |
{ table, index, hidden, restored }. restored is true for a saved choice that the script applies at load |
nds:table:sub-request |
.nds-table |
{ row, sub, table, signal }. sub is null. Pass signal to your request, so a cancel stops it |
nds:table:sub-opened |
.nds-table |
{ row, sub, table } |
nds:table:sub-closed |
.nds-table |
{ row, sub, table } |
The full API is in the banner of _js/nds-table.js.