Sort & Filter
ReoGrid Web lets you sort columns and narrow down data using auto-filters with dropdown controls.
Sort & Filter are available in the Pro edition.
Column Sort
const ws = grid.worksheet
// Ascending sort
ws.sortColumn(0, 'asc')
// Descending sort
ws.sortColumn(0, 'desc')
// Get sort state
const state = ws.getSortState()
// { column: 0, order: 'asc' } or null
getSortState()reflects the active auto-filter’s sort state — it returnsnullwhen no auto-filter exists (a plainsortColumn()call doesn’t record state).
Sorting operates within the auto-filter range. If no filter is set, the entire sheet is sorted.
Auto-Filter
Displays dropdowns on the header row to filter data.
Creating a Filter
const filter = ws.createAutoFilter({
headerRow: 0,
startColumn: 0,
endColumn: 4,
})
Data rows start below headerRow and run to the end of the sheet. To keep a total row under the table out of the filter — neither listed nor hidden — give the last data row with endRow (v1.7.2):
const filter = ws.createAutoFilter({
headerRow: 0,
startColumn: 0,
endColumn: 4,
endRow: 20, // data rows 1–20 (zero-based); the total in row 21 stays put
})
Filters in xlsx files
Since v1.7.2 a sheet’s auto-filter is read from and saved to xlsx files. Opening a file with a filter gives the same filter buttons as createAutoFilter(). The rows stay as the file saved them, and a filtered column reopens with the values on its visible rows selected. Saving writes the filter back, so Excel shows the sheet as filtered.
- A filter overlapping a table is not saved, because Excel cannot open that combination.
- In Lite, a file with a filter loads without the filter buttons.
Filter Operations
// Get unique values for a column
const values = filter.getColumnValues(1)
// ['Tokyo', 'Osaka', 'Nagoya', ...]
// Set filter criteria (show only selected values)
filter.setColumnFilter(1, new Set(['Tokyo', 'Osaka']))
// Apply the filter
filter.apply()
// Get filter state for a specific column
const colFilter = filter.getColumnFilter(1)
// { selectedValues: Set, ... } or null
// Check if there are active filters
const hasFilters = filter.hasActiveFilters()
// Clear all filters
filter.clearAll()
Sorting Within a Filter
filter.sortColumn(0, 'asc')
const sortState = filter.getSortState()
Events
Subscribing to Filter Changes
filter.onFilterChange((event) => {
console.log('Hidden rows:', event.hiddenRowCount)
})
Filter Dropdown Click
ws.onFilterDropdownClick((column) => {
console.log('Filter dropdown clicked for column:', column)
// Display a custom filter UI, etc.
})
Removing a Filter
ws.removeAutoFilter()
// Get the current filter
const currentFilter = ws.getAutoFilter() // AutoColumnFilter | null
Complete Example: Data Table
const ws = grid.worksheet
ws.suspendRender()
// Headers
const headers = ['ID', 'Name', 'Department', 'Hire Year', 'Sales']
headers.forEach((h, i) => {
ws.cell(0, i).setValue(h).setStyle({ bold: true })
})
// Data
const data = [
[1, 'Tanaka', 'Sales', 2020, 1200000],
[2, 'Suzuki', 'Engineering', 2019, 980000],
[3, 'Sato', 'Sales', 2021, 1500000],
// ...
]
data.forEach((row, r) => {
row.forEach((val, c) => {
ws.setCellInput(r + 1, c, String(val))
})
})
// Freeze header row
ws.setFrozenRows(1)
// Alternating row colors
ws.setAlternateRowColors({ evenColor: '#ffffff', oddColor: '#f8fafc' })
// Set up auto-filter
const filter = ws.createAutoFilter({
headerRow: 0,
startColumn: 0,
endColumn: 4,
})
ws.resumeRender()
// Filter operation example — show only Sales department
filter.setColumnFilter(2, new Set(['Sales']))
filter.apply()
Notes
- Sorting operates within the auto-filter range. If no filter is set, the entire sheet is sorted.
- Filtering works by toggling row visibility. Data in hidden rows is preserved.
- A column of formulas is listed and filtered by its results — what the cells show — not by the formula text (v1.7.2; earlier versions listed
=SUM(D5:G5)once per row). - Escape closes the filter dropdown without applying it, and keyboard focus goes back to the grid after OK, Cancel or a sort button (v1.7.2).
- A sort moves everything the cell holds. Since v1.6.0 cell comments, cell-type configuration (checkboxes and friends) and explicit lock state travel with the row, alongside the values, styles and borders that always did. Before that they stayed behind, so after a sort a comment appeared to belong to a different row’s content. Comments keep their id; anything outside the sorted column range, or on a hidden (non-participating) row, stays put.