Sorting · Filtering
Users sort and filter with the headers and the right-click menu. Your screen can do the same in code.
sort-filter.js
import { createGrid } from 'horee-grid'
const regions = ['New York', 'London', 'Sydney', 'Toronto', 'Dublin', 'Auckland']
const rows = Array.from({ length: 2000 }, (_, i) => ({
id: i + 1,
region: regions[i % 6],
shop: `${regions[i % 6]} #${Math.floor(i / 6) + 1}`,
sales: ((i * 7919) % 50000) * 100,
opened: `20${10 + (i % 16)}-0${(i % 9) + 1}-15`
}))
const grid = createGrid(host, {
locale: 'en',
rowKey: 'id',
columns: [
{ field: 'region', title: 'Region', width: 80, align: 'center' },
{ field: 'shop', title: 'Store', flex: 1, minWidth: 140 },
{ field: 'sales', title: 'Sales', type: 'number', width: 130 },
{ field: 'opened', title: 'Opened', type: 'date', width: 110 }
],
rows,
onSortChange: () => show(),
onFilterChange: () => show()
})
toolbar.innerHTML = `
<button data-a="sort">Region ↑, Sales ↓</button>
<button data-a="filter">Sales >= 4,000,000</button>
<button data-a="mine">Row filter: New York only</button>
<button data-a="clear">Clear all</button>
<span class="ex-out"></span>`
toolbar.onclick = e => {
const a = e.target.dataset.a
if (a === 'sort') grid.setSort([{ id: 'region', dir: 'asc' }, { id: 'sales', dir: 'desc' }])
if (a === 'filter') grid.setFilters([{ id: 'sales', op: 'ge', value: '4000000' }])
if (a === 'mine') grid.setRowFilter(r => r.region === 'New York')
if (a === 'clear') {
grid.setSort([])
grid.setFilters([])
grid.setRowFilter(null)
}
show()
}
function show() {
toolbar.querySelector('.ex-out').textContent = `${grid.getViewRows().length.toLocaleString()} / ${rows.length.toLocaleString()} rows`
}
show()Sorting
| Action | Result |
|---|---|
| Click a header | Ascending → descending → off |
| Shift + click a header | Adds a level to the current sort (multi-sort). The header shows its order number |
grid.setSort([{ id: 'region', dir: 'asc' }, { id: 'sales', dir: 'desc' }]) // array order = priority
grid.getSort() // current sort
grid.setSort([]) // clear- Sorting does not change the original array.
getRows()returns the order you passed;getViewRows()returns the displayed order. - Values are compared by type: numbers as numbers, dates as dates, text in natural order (
Item2<Item10). - To turn off multi-sort, use the option
multiSort: false. - When the user changes the sort,
onSortChange(sort)is called.
Grid filter
Right-click a header or cell → Grid filter… opens a dialog. Enter a condition and value per column. With several conditions, only rows matching all of them are shown (AND).
| Condition | op | Notes |
|---|---|---|
| Equals · Not equal | eq · ne | Numbers compare as numbers (1,000 = 1000) |
| Greater · Greater or equal · Less · Less or equal | gt · ge · lt · le | Blank values match neither side |
| Contains · Starts with · Ends with | contains · starts · ends | Case-insensitive |
| Blank · Not blank | blank · notBlank | No value needed |
grid.setFilters([
{ id: 'sales', op: 'ge', value: '4000000' },
{ id: 'shop', op: 'contains', value: 'Downtown' }
])
grid.getFilters()
grid.setFilters([])- Values are always strings, exactly as the user types them.
- Date columns treat
2026-11-15,2026/11/15and20261115as the same day. - Text comparisons (equals · contains · starts · ends) pass if either the displayed text or the original value matches. If
formatshows codes as names, users can filter by name or by code. - Filtered columns get a different header text color.
- When the user changes filters,
onFilterChange(filters)is called.
Row filter: setRowFilter
Conditions decided by your screen, such as a "show open items only" checkbox, are given as a function. It applies together with the grid filter.
showOpenOnly.onchange = () => grid.setRowFilter(showOpenOnly.checked ? row => row.status === 'OPEN' : null)How it differs from the grid filter:
| Grid filter | setRowFilter | |
|---|---|---|
| Who sets it | The user (right-click menu) | Your screen code |
Included in saved state (getState) | Yes | No |
| With server data | Sent to the server | Applied only to rows already received |
With server data
If you set the dataSource option, the grid does not sort or filter itself; it sends the sort and grid filters to the server with the request. See Server data.