Components Data display
Data table
Filter a local set of records and sort the visible table by any column.
Usage
Supply Caption, Headers and Rows. Columns sort with numeric-aware text comparison. Enter a term to filter all cells. Select the same heading again to reverse its sort direction. The unenhanced HTML displays every row.
Register data-table with Head and pass its properties to Render. Go setup guide
Go usage
err := mono.UI.Render(w, "data-table", mono.Props{
"Caption": "Print orders",
"Headers": []any{
"Order",
"Customer",
"Sheets",
},
"Rows": []any{
[]any{
"ED-104",
"Roundhouse",
72,
},
[]any{
"ED-099",
"Quiet Press",
120,
},
[]any{
"ED-108",
"North Studio",
35,
},
[]any{
"ED-102",
"Aster Books",
80,
},
},
})Rendered HTML
<section data-mono="data-table" id="demo-data-table" class="mu-stack">
<div data-filter-wrap hidden><label class="mu-label" for="demo-data-table-filter">Filter records</label>
<input type="search" class="mu-field" id="demo-data-table-filter" placeholder="Search all columns" data-filter>
</div>
<div class="mu-table-viewport" role="region" aria-label="Print orders" tabindex="0"><table>
<caption>Print orders</caption>
<thead><tr><th scope="col">Order</th><th scope="col">Customer</th><th scope="col">Sheets</th></tr></thead>
<tbody><tr><td>ED-104</td><td>Roundhouse</td><td>72</td></tr><tr><td>ED-099</td><td>Quiet Press</td><td>120</td></tr><tr><td>ED-108</td><td>North Studio</td><td>35</td></tr><tr><td>ED-102</td><td>Aster Books</td><td>80</td></tr></tbody>
</table></div>
<p data-summary role="status" class="mu-muted mu-small"></p>
</section>Go template
<section data-mono="data-table" id="{{.ID}}" class="mu-stack">
<div data-filter-wrap hidden><label class="mu-label" for="{{.ID}}-filter">Filter records</label>
<input type="search" class="mu-field" id="{{.ID}}-filter" placeholder="Search all columns" data-filter>
</div>
<div class="mu-table-viewport" role="region" aria-label="{{.Caption}}" tabindex="0"><table>
<caption>{{.Caption}}</caption>
<thead><tr>{{range .Headers}}<th scope="col">{{.}}</th>{{end}}</tr></thead>
<tbody>{{range .Rows}}<tr>{{range .}}<td>{{.}}</td>{{end}}</tr>{{end}}</tbody>
</table></div>
<p data-summary role="status" class="mu-muted mu-small"></p>
</section>Component CSS
[data-mono="data-table"] .mu-table-viewport { overflow-x:auto; }
[data-mono="data-table"] table { width:100%; border-collapse:collapse; text-align:start; font-size:.9rem; }
[data-mono="data-table"] caption { padding:0 0 .6rem; text-align:start; font-weight:650; }
[data-mono="data-table"] :is(th,td) { padding:.6rem .75rem; text-align:start; border-bottom:1px solid #d7d7d7; }
[data-mono="data-table"] thead th { background:#f1f1f1; color:#252525; }
[data-mono="data-table"] th button { display:inline-flex; align-items:center; gap:.4rem; background:none; border:0; font:inherit; font-weight:650; color:inherit; padding:.2rem 0; cursor:pointer; }
[data-mono="data-table"] .mu-field { display:block; width:min(100%,18rem); margin-top:.3rem; }
[data-mono="data-table"] [data-sort-mark] {
flex:none; width:1rem; height:1rem; background:currentColor; color:var(--mu-muted); forced-color-adjust:none;
mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='m7 9 5-5 5 5m-10 6 5 5 5-5' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / contain no-repeat;
}
[data-mono="data-table"] [aria-sort] [data-sort-mark] {
color:inherit;
mask-image:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 19V5m-6 6 6-6 6 6' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
}
[data-mono="data-table"] [aria-sort="descending"] [data-sort-mark] { transform:rotate(180deg); }JavaScript module
export function mount(root) {
const body = root.querySelector('tbody');
const rows = [...body.querySelectorAll(':scope > tr')];
const headers = [...root.querySelectorAll('thead th')];
const input = root.querySelector('[data-filter]');
const summary = root.querySelector('[data-summary]');
const collator = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' });
let sortColumn = -1;
let direction = 1;
function refresh() {
const query = input.value.trim().toLocaleLowerCase();
let visible = 0;
for (const row of rows) {
row.hidden = query.length > 0 && ![...row.children].some(cell => cell.textContent.toLocaleLowerCase().includes(query));
if (!row.hidden) visible++;
}
summary.textContent = visible ? visible + ' of ' + rows.length + ' records' : 'No matching records.';
}
headers.forEach((header, index) => {
const label = header.textContent.trim();
const action = document.createElement('button');
const mark = document.createElement('span');
action.type = 'button';
action.textContent = label;
mark.dataset.sortMark = '';
mark.setAttribute('aria-hidden', 'true');
action.append(mark);
header.replaceChildren(action);
action.addEventListener('click', () => {
direction = index === sortColumn ? -direction : 1;
sortColumn = index;
for (const cell of headers) cell.removeAttribute('aria-sort');
header.setAttribute('aria-sort', direction === 1 ? 'ascending' : 'descending');
const ordered = [...rows].sort((a, b) => direction * collator.compare(a.children[index]?.textContent ?? '', b.children[index]?.textContent ?? ''));
body.append(...ordered);
});
});
input.addEventListener('input', refresh);
root.querySelector('[data-filter-wrap]').hidden = false;
refresh();
}Use the HTML in a static site
Copy the rendered HTML above. Download these assets into the same folder as your page, then add the tags below to its head.
Shared CSS Component CSS Ready-to-use module
<link rel="stylesheet" href="./base.css">
<link rel="stylesheet" href="./data-table.css">
<script type="module" src="./data-table.js"></script>Copy into your project
Run this command from the Mono checkout. Set -out to the component directory in your application.
go run ./cmd/mono-ui add -out ./components data-tableExample properties (JSON)
{
"Caption": "Print orders",
"Headers": [
"Order",
"Customer",
"Sheets"
],
"Rows": [
[
"ED-104",
"Roundhouse",
72
],
[
"ED-099",
"Quiet Press",
120
],
[
"ED-108",
"North Studio",
35
],
[
"ED-102",
"Aster Books",
80
]
]
}