Skip to content

Multi-Sort Plugin

The Multi-Sort plugin enables sorting by multiple columns at once—hold Shift and click additional column headers to build up a sort stack. Priority badges show the sort order, so users always know which column takes precedence.

import '@toolbox-web/grid/features/multi-sort';

Mark columns as sortable: true and enable the feature. Users Shift+click to add columns to the sort stack, and regular click to reset to single-column sort.

import { queryGrid } from '@toolbox-web/grid';
const grid = queryGrid('tbw-grid');
grid.gridConfig = {
columns: [
{ field: 'name', header: 'Name', sortable: true },
{ field: 'department', header: 'Department', sortable: true },
{ field: 'salary', header: 'Salary', type: 'number', sortable: true },
{ field: 'startDate', header: 'Start Date', type: 'date', sortable: true },
],
features: {
multiSort: {
maxSortColumns: 3,
showSortIndex: true,
},
},
};
// Listen for sort changes
grid.on('sort-change', ({ sortModel }) => {
console.log('Active sorts:', sortModel);
});

Use the controls to explore different multi-sort configurations: adjust the maximum number of sort levels, toggle priority badges, or apply an initial sort.

Max sort columns Maximum number of sort levels
Show sort index Display sort priority number on headers
Apply initial sort Pre-sort by Department ↑ then Salary ↓

Hold Shift and click column headers to add columns to the sort stack. Set Max sort columns to 1 for single-column-only sorting.

See MultiSortConfig for the full list of options and their defaults.

Use setSortModel() to set a pre-configured sort order after the grid initializes:

const plugin = grid.getPluginByName('multiSort');
plugin.setSortModel([
{ field: 'department', direction: 'asc' },
{ field: 'salary', direction: 'desc' },
]);

Hold Shift and click column headers to add sort levels.

Event Log:

The grid fires a sort-change event whenever the sort model changes:

grid.on('sort-change', ({ sortModel }) => {
console.log('Sort model:', sortModel);
// [{ field: 'name', direction: 'asc' }, { field: 'salary', direction: 'desc' }]
});

See MultiSortPlugin for the full list of methods.

const plugin = grid.getPluginByName('multiSort');
plugin.setSortModel([{ field: 'department', direction: 'asc' }]);
plugin.getSortModel(); // SortModel[]
plugin.clearSort();
plugin.getSortDirection('salary'); // 'asc' | 'desc' | undefined
plugin.getSortIndex('salary'); // number | undefined
ShortcutAction
Click headerSort by column (clears other sorts)
Shift + ClickAdd column to multi-sort

The grid uses Light DOM, so standard CSS selectors work for styling sort indicators:

/* Sort indicator icon */
tbw-grid .sort-indicator {
margin-left: 4px;
}
/* Sort priority badge */
tbw-grid .sort-index {
background: var(--tbw-multi-sort-badge-bg, var(--tbw-color-panel-bg));
color: var(--tbw-multi-sort-badge-color, var(--tbw-color-fg));
width: var(--tbw-multi-sort-badge-size, 1em);
height: var(--tbw-multi-sort-badge-size, 1em);
}

When multi-sort is active, assigning rows automatically re-sorts the data — so a newly inserted row will jump to its sorted position. If you want the row to stay exactly where you placed it (e.g., after a user clicks “Add Row”), use insertRow():

grid.insertRow(3, newRow); // stays at visible index 3, auto-animates

The row is also added to source data, so the next full grid.rows = freshData assignment re-sorts normally. See the API Reference → Insert & Remove Rows for full details.

AI assistants: For complete API documentation, implementation guides, and code examples for this library, see https://raw.githubusercontent.com/OysteinAmundsen/toolbox/main/llms-full.txt