horee-gridb0.0.1

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

ActionResult
Click a headerAscending → descending → off
Shift + click a headerAdds 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).

ConditionopNotes
Equals · Not equaleq · neNumbers compare as numbers (1,000 = 1000)
Greater · Greater or equal · Less · Less or equalgt · ge · lt · leBlank values match neither side
Contains · Starts with · Ends withcontains · starts · endsCase-insensitive
Blank · Not blankblank · notBlankNo 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/15 and 20261115 as the same day.
  • Text comparisons (equals · contains · starts · ends) pass if either the displayed text or the original value matches. If format shows 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 filtersetRowFilter
Who sets itThe user (right-click menu)Your screen code
Included in saved state (getState)YesNo
With server dataSent to the serverApplied 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.