uui-table is just a display component it renders rows/cells but doesn’t have any built-in sorting, so you’ll need to wire that up yourself.
If you tried using the Sorter and couldn’t get it working, that’s probably because it’s not actually for this — UmbSorterController is for drag-and-drop reordering of a list, not for sorting a column by clicking its header. Different problem, so it won’t help here.
For click-to-sort-a-column, handle it the same way you handled paging by attaching listeners after the markup is in the DOM, not inside the string-building functions themselves:
- Give each header cell an identifiable attribute in
tableHeader(), e.g.<uui-table-head-cell data-field="name" role="columnheader">Name</uui-table-head-cell>. - After render (alongside your
setupPaging()call), add asetupSorting()function that grabs the header cells from_rootand attaches click listeners:
js
function setupSorting() {
var headers = _root.querySelectorAll("uui-table-head-cell[data-field]");
headers.forEach(function (header) {
header.addEventListener("click", function () {
sortData(header.getAttribute("data-field"));
});
});
}
- Write
sortData(field)to toggle direction and sort_datadirectly:
js
let _sortColumn = null;
let _sortDirection = "asc";
function sortData(field) {
if (_sortColumn === field) {
_sortDirection = _sortDirection === "asc" ? "desc" : "asc";
} else {
_sortColumn = field;
_sortDirection = "asc";
}
_data.sort((a, b) => {
const valA = a[field];
const valB = b[field];
if (valA < valB) return _sortDirection === "asc" ? -1 : 1;
if (valA > valB) return _sortDirection === "asc" ? 1 : -1;
return 0;
});
_currentPage = 1;
clearTable(buildtable(_currentPage));
}
- Call
setupSorting()again afterclearTable()rebuilds the markup, since the old header elements (and their listeners) get wiped out along with the rest of the table’sinnerHTML. - Also call
setupSorting()once inloadData(), right next to your existingsetupPaging()call otherwise the first render won’t have any listeners attached at all.