Data table
A table with sorting, filtering, pagination and row selection, enhancing server-rendered rows.
Installation
Usage
Render every row on the server; the script sorts, filters and pages them in the browser. That suits a few hundred rows. For more, do the same work on the server and keep the markup: the attributes and states are the same.
Without JavaScript all rows show and the table still reads well.
JavaScript API
data-bootcn-data-tableon the wrapper;data-page-size(default 10).- Sortable columns: a button with
data-bootcn-data-table-sort="<column index>"inside ath[aria-sort];data-type="number"sorts numerically, using a cell'sdata-valuewhen present. data-bootcn-data-table-filter(text input),-select-alland-select(checkboxes),-prev/-next(buttons),-statusand-selection(text).- Event
bootcn:data-table:change(bubbles),detail.page,detail.visibleanddetail.selected(rows). Bootcn.dataTable.init(root)/destroy(root).
Accessibility
- Sort buttons sit inside the column headers, whose
aria-sortannounces the order; activating one cycles ascending, descending and original. - Row checkboxes are named after their row ("Select ken99@example.com"); "Select all rows" becomes mixed (indeterminate) when only some rows are selected.
- The "Showing x–y of z" line is a status region, so paging and filtering are announced.
Dependencies
- Bootstrap 5.3+ CSS.
themes/tokens/tokens.css.- The
tablecomponent. - The
inputcomponent. - The
buttoncomponent. - The
checkboxcomponent.
Source
The exact files the CLI installs (data-table@1.0.0). View on GitHub · Report an issue
html
<div class="bootcn-data-table" data-bootcn-data-table data-page-size="5">
<div class="bootcn-data-table__toolbar">
<label class="visually-hidden" for="bootcn-data-table-filter">Filter payments</label>
<input
class="form-control bootcn-input bootcn-data-table__filter"
id="bootcn-data-table-filter"
type="search"
placeholder="Filter payments…"
data-bootcn-data-table-filter
/>
</div>
<div class="table-responsive">
<table class="table bootcn-table">
<caption class="visually-hidden">
Payments
</caption>
<thead>
<tr>
<th scope="col" class="bootcn-data-table__check">
<input
class="form-check-input"
type="checkbox"
aria-label="Select all rows"
data-bootcn-data-table-select-all
/>
</th>
<th scope="col" aria-sort="none">
<button type="button" class="bootcn-data-table__sort" data-bootcn-data-table-sort="1">
Status
<svg
class="bootcn-icon"
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
focusable="false"
>
<path d="m7 15 5 5 5-5" />
<path d="m7 9 5-5 5 5" />
</svg>
</button>
</th>
<th scope="col" aria-sort="none">
<button type="button" class="bootcn-data-table__sort" data-bootcn-data-table-sort="2">
Email
<svg
class="bootcn-icon"
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
focusable="false"
>
<path d="m7 15 5 5 5-5" />
<path d="m7 9 5-5 5 5" />
</svg>
</button>
</th>
<th scope="col" aria-sort="none" class="text-end">
<button
type="button"
class="bootcn-data-table__sort"
data-bootcn-data-table-sort="3"
data-type="number"
>
Amount
<svg
class="bootcn-icon"
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
focusable="false"
>
<path d="m7 15 5 5 5-5" />
<path d="m7 9 5-5 5 5" />
</svg>
</button>
</th>
</tr>
</thead>
<tbody>
<tr>
<td class="bootcn-data-table__check">
<input
class="form-check-input"
type="checkbox"
aria-label="Select ken99@example.com"
data-bootcn-data-table-select
/>
</td>
<td>Success</td>
<td>ken99@example.com</td>
<td class="text-end" data-value="316">€316.00</td>
</tr>
<tr>
<td class="bootcn-data-table__check">
<input
class="form-check-input"
type="checkbox"
aria-label="Select abe45@example.com"
data-bootcn-data-table-select
/>
</td>
<td>Success</td>
<td>abe45@example.com</td>
<td class="text-end" data-value="242">€242.00</td>
</tr>
<tr>
<td class="bootcn-data-table__check">
<input
class="form-check-input"
type="checkbox"
aria-label="Select monserrat44@example.com"
data-bootcn-data-table-select
/>
</td>
<td>Processing</td>
<td>monserrat44@example.com</td>
<td class="text-end" data-value="837">€837.00</td>
</tr>
<tr>
<td class="bootcn-data-table__check">
<input
class="form-check-input"
type="checkbox"
aria-label="Select carmella@example.com"
data-bootcn-data-table-select
/>
</td>
<td>Failed</td>
<td>carmella@example.com</td>
<td class="text-end" data-value="721">€721.00</td>
</tr>
<tr>
<td class="bootcn-data-table__check">
<input
class="form-check-input"
type="checkbox"
aria-label="Select jason78@example.com"
data-bootcn-data-table-select
/>
</td>
<td>Pending</td>
<td>jason78@example.com</td>
<td class="text-end" data-value="450">€450.00</td>
</tr>
<tr>
<td class="bootcn-data-table__check">
<input
class="form-check-input"
type="checkbox"
aria-label="Select sarah23@example.com"
data-bootcn-data-table-select
/>
</td>
<td>Success</td>
<td>sarah23@example.com</td>
<td class="text-end" data-value="125">€125.00</td>
</tr>
<tr>
<td class="bootcn-data-table__check">
<input
class="form-check-input"
type="checkbox"
aria-label="Select lucas.p@example.com"
data-bootcn-data-table-select
/>
</td>
<td>Failed</td>
<td>lucas.p@example.com</td>
<td class="text-end" data-value="980">€980.00</td>
</tr>
<tr>
<td class="bootcn-data-table__check">
<input
class="form-check-input"
type="checkbox"
aria-label="Select emma.w@example.com"
data-bootcn-data-table-select
/>
</td>
<td>Processing</td>
<td>emma.w@example.com</td>
<td class="text-end" data-value="315">€315.00</td>
</tr>
<tr>
<td class="bootcn-data-table__check">
<input
class="form-check-input"
type="checkbox"
aria-label="Select noah.b@example.com"
data-bootcn-data-table-select
/>
</td>
<td>Success</td>
<td>noah.b@example.com</td>
<td class="text-end" data-value="74">€74.00</td>
</tr>
<tr>
<td class="bootcn-data-table__check">
<input
class="form-check-input"
type="checkbox"
aria-label="Select olivia.k@example.com"
data-bootcn-data-table-select
/>
</td>
<td>Pending</td>
<td>olivia.k@example.com</td>
<td class="text-end" data-value="562">€562.00</td>
</tr>
<tr>
<td class="bootcn-data-table__check">
<input
class="form-check-input"
type="checkbox"
aria-label="Select liam.m@example.com"
data-bootcn-data-table-select
/>
</td>
<td>Success</td>
<td>liam.m@example.com</td>
<td class="text-end" data-value="199">€199.00</td>
</tr>
<tr>
<td class="bootcn-data-table__check">
<input
class="form-check-input"
type="checkbox"
aria-label="Select ava.r@example.com"
data-bootcn-data-table-select
/>
</td>
<td>Failed</td>
<td>ava.r@example.com</td>
<td class="text-end" data-value="640">€640.00</td>
</tr>
</tbody>
</table>
</div>
<p class="bootcn-data-table__empty" hidden>No payments match.</p>
<div class="bootcn-data-table__footer">
<p class="bootcn-data-table__status" data-bootcn-data-table-selection>0 of 12 rows selected.</p>
<p class="bootcn-data-table__status" data-bootcn-data-table-status role="status">
Showing 1–5 of 12
</p>
<div class="bootcn-data-table__pages">
<button
type="button"
class="btn bootcn-button bootcn-button--outline bootcn-button--sm"
data-bootcn-data-table-prev
>
Previous
</button>
<button
type="button"
class="btn bootcn-button bootcn-button--outline bootcn-button--sm"
data-bootcn-data-table-next
>
Next
</button>
</div>
</div>
</div>css
.bootcn-data-table__toolbar {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 0.5rem;
margin-bottom: 1rem;
}
.bootcn-data-table__filter {
max-width: 20rem;
}
.bootcn-data-table .table-responsive {
border: var(--bootcn-border-width) solid var(--bootcn-border-color);
border-radius: var(--bootcn-radius-lg);
}
.bootcn-data-table .bootcn-table > tbody > tr:last-child > * {
border-bottom: 0;
}
.bootcn-data-table__check {
width: 2.5rem;
}
.bootcn-data-table__check .form-check-input {
margin: 0;
border-color: var(--bootcn-primary);
}
.bootcn-data-table__check .form-check-input:checked,
.bootcn-data-table__check .form-check-input:indeterminate {
border-color: var(--bootcn-primary);
background-color: var(--bootcn-primary);
}
[data-bs-theme='dark'] .bootcn-data-table__check .form-check-input:checked {
--bs-form-check-bg-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20'%3e%3cpath fill='none' stroke='%2318181b' stroke-linecap='round' stroke-linejoin='round' stroke-width='3' d='m6 10 3 3 6-6'/%3e%3c/svg%3e");
}
[data-bs-theme='dark'] .bootcn-data-table__check .form-check-input:indeterminate {
--bs-form-check-bg-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20'%3e%3cpath fill='none' stroke='%2318181b' stroke-linecap='round' stroke-linejoin='round' stroke-width='3' d='M6 10h8'/%3e%3c/svg%3e");
}
.bootcn-data-table__check .form-check-input:focus-visible {
box-shadow: var(--bootcn-focus-ring);
}
.bootcn-data-table__sort {
display: inline-flex;
align-items: center;
gap: 0.375rem;
margin: -0.25rem -0.5rem;
padding: 0.25rem 0.5rem;
border: 0;
border-radius: var(--bootcn-radius);
background: transparent;
color: inherit;
font: inherit;
}
.bootcn-data-table__sort:hover {
background-color: var(--bootcn-accent);
color: var(--bootcn-foreground);
}
.bootcn-data-table__sort:focus-visible {
outline: 0;
box-shadow: var(--bootcn-focus-ring);
}
.bootcn-data-table__sort .bootcn-icon {
width: 0.875rem;
height: 0.875rem;
}
[aria-sort='ascending'] .bootcn-data-table__sort,
[aria-sort='descending'] .bootcn-data-table__sort {
color: var(--bootcn-foreground);
}
.bootcn-data-table tr.is-selected > * {
background-color: var(--bootcn-muted);
}
.bootcn-data-table__empty {
margin: 0;
padding: 2rem;
color: var(--bootcn-muted-foreground);
text-align: center;
}
.bootcn-data-table__footer {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: space-between;
gap: 0.5rem 1rem;
margin-top: 1rem;
}
.bootcn-data-table__status {
margin: 0;
color: var(--bootcn-muted-foreground);
font-size: 0.875rem;
}
.bootcn-data-table__pages {
display: flex;
gap: 0.5rem;
}js
(function () {
'use strict';
const ns = (window.Bootcn = window.Bootcn || {});
if (ns.dataTable) return; // already loaded: idempotent
const ROOT = '[data-bootcn-data-table]';
const INIT = 'data-bootcn-data-table-initialized';
const tables = new Map();
function cellValue(row, column, type) {
const cell = row.cells[column];
if (!cell) return '';
const raw = cell.hasAttribute('data-value')
? cell.getAttribute('data-value')
: cell.textContent;
return type === 'number' ? Number(raw) : raw.trim().toLowerCase();
}
function setup(root) {
const tbody = root.querySelector('tbody');
if (!tbody) return null;
const rows = Array.from(tbody.rows);
const filterInput = root.querySelector('[data-bootcn-data-table-filter]');
const selectAll = root.querySelector('[data-bootcn-data-table-select-all]');
const prev = root.querySelector('[data-bootcn-data-table-prev]');
const next = root.querySelector('[data-bootcn-data-table-next]');
const status = root.querySelector('[data-bootcn-data-table-status]');
const selection = root.querySelector('[data-bootcn-data-table-selection]');
const empty = root.querySelector('.bootcn-data-table__empty');
const pageSize = Number(root.getAttribute('data-page-size')) || 10;
const t = { rows: rows, order: rows.slice(), page: 0, query: '' };
const checkbox = (row) => row.querySelector('[data-bootcn-data-table-select]');
const matching = () =>
t.order.filter((row) => t.query === '' || row.textContent.toLowerCase().includes(t.query));
function update() {
const list = matching();
const pages = Math.max(1, Math.ceil(list.length / pageSize));
t.page = Math.min(t.page, pages - 1);
const start = t.page * pageSize;
const shown = list.slice(start, start + pageSize);
t.order.forEach((row) => {
row.hidden = !shown.includes(row);
tbody.appendChild(row); // keep DOM order = sort order
});
if (empty) empty.hidden = list.length !== 0;
if (prev) prev.disabled = t.page === 0;
if (next) next.disabled = t.page >= pages - 1;
if (status) {
status.textContent =
list.length === 0
? 'No rows'
: 'Showing ' + (start + 1) + '–' + (start + shown.length) + ' of ' + list.length;
}
const selected = t.rows.filter((row) => checkbox(row) && checkbox(row).checked);
t.rows.forEach((row) => row.classList.toggle('is-selected', selected.includes(row)));
if (selection)
selection.textContent = selected.length + ' of ' + t.rows.length + ' rows selected.';
if (selectAll) {
const visible = list.filter((row) => checkbox(row));
const on = visible.filter((row) => checkbox(row).checked).length;
selectAll.checked = visible.length > 0 && on === visible.length;
selectAll.indeterminate = on > 0 && on < visible.length;
}
root.dispatchEvent(
new CustomEvent('bootcn:data-table:change', {
bubbles: true,
detail: { page: t.page, visible: list.length, selected: selected },
}),
);
}
// ascending -> descending -> original order
function sort(button) {
const th = button.closest('th');
const column = Number(button.getAttribute('data-bootcn-data-table-sort'));
const type = button.getAttribute('data-type') || 'text';
const current = th.getAttribute('aria-sort');
const nextSort =
current === 'ascending' ? 'descending' : current === 'descending' ? 'none' : 'ascending';
root
.querySelectorAll('th[aria-sort]')
.forEach((other) => other.setAttribute('aria-sort', 'none'));
th.setAttribute('aria-sort', nextSort);
if (nextSort === 'none') {
t.order = t.rows.slice();
} else {
const direction = nextSort === 'ascending' ? 1 : -1;
t.order = t.rows.slice().sort((a, b) => {
const x = cellValue(a, column, type);
const y = cellValue(b, column, type);
return (x > y ? 1 : x < y ? -1 : 0) * direction;
});
}
t.page = 0;
update();
}
function onClick(event) {
const sortButton = event.target.closest('[data-bootcn-data-table-sort]');
if (sortButton) return sort(sortButton);
if (event.target.closest('[data-bootcn-data-table-prev]')) {
t.page = Math.max(0, t.page - 1);
return update();
}
if (event.target.closest('[data-bootcn-data-table-next]')) {
t.page += 1;
return update();
}
return undefined;
}
function onChange(event) {
if (event.target === selectAll) {
matching().forEach((row) => {
if (checkbox(row)) checkbox(row).checked = selectAll.checked;
});
}
if (
event.target.matches('[data-bootcn-data-table-select], [data-bootcn-data-table-select-all]')
) {
update();
}
}
function onInput(event) {
if (event.target !== filterInput) return;
t.query = filterInput.value.trim().toLowerCase();
t.page = 0;
update();
}
root.addEventListener('click', onClick);
root.addEventListener('change', onChange);
root.addEventListener('input', onInput);
root.setAttribute(INIT, '');
update();
return function cleanup() {
root.removeEventListener('click', onClick);
root.removeEventListener('change', onChange);
root.removeEventListener('input', onInput);
root.removeAttribute(INIT);
t.rows.forEach((row) => {
row.hidden = false;
});
};
}
function init(root = document) {
root.querySelectorAll(ROOT + ':not([' + INIT + '])').forEach((element) => {
const cleanup = setup(element);
if (cleanup) tables.set(element, cleanup);
});
}
function destroy(root = document) {
tables.forEach((cleanup, element) => {
if (root === document || root.contains(element)) {
cleanup();
tables.delete(element);
}
});
}
ns.dataTable = { init: init, destroy: destroy };
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => init());
} else {
init();
}
})();
