Selection · Active cell
A grid has three different kinds of "picked" things. They are easy to confuse at first, so let us tell them apart.
| Name | Looks like | Meaning | API |
|---|---|---|---|
| Row selection | Whole row tinted; checkboxes with multi | Rows picked as targets of an action (items to pay, rows to delete) | getSelectedRows() · select() |
| Active cell | Cell border | The cell that receives keyboard input (Excel's current cell) | getActiveCell() · setActiveCell() |
| Range | Block of cells | Target of copy, paste and clear | User actions |
There is one more: the "current row" marker (setCurrent). Use it on master-detail screens to mark in the upper list the row shown below.
import { createGrid } from 'horee-grid'
const rows = Array.from({ length: 500 }, (_, i) => ({ id: i + 1, vendor: `Vendor ${i + 1}`, amount: (((i * 104729) % 9000) + 100) * 1000 }))
const grid = createGrid(host, {
locale: 'en',
rowKey: 'id',
selection: 'multi',
columns: [
{ field: 'id', title: 'No.', width: 70, align: 'center' },
{ field: 'vendor', title: 'Vendor', flex: 1 },
{ field: 'amount', title: 'Payment', type: 'number', width: 140 }
],
rows,
onSelectionChange: picked => {
const sum = picked.reduce((s, r) => s + r.amount, 0)
out(`${picked.length} selected · total ${sum.toLocaleString()}`)
},
onRowDblClick: row => out(`${row.vendor} — double-clicked`)
})
toolbar.innerHTML = `
<button data-a="pick">Select 3 · 5 · 8</button>
<button data-a="go">Go to 400</button>
<button data-a="clear">Clear selection</button>
<span class="ex-out">0 selected</span>`
toolbar.onclick = e => {
const a = e.target.dataset.a
if (a === 'pick') [3, 5, 8].forEach(key => grid.select(key, false))
if (a === 'go') grid.setCurrent(400)
if (a === 'clear') grid.clearSelection()
}
const out = text => (toolbar.querySelector('.ex-out').textContent = text)Row selection
createGrid(host, { columns, rows, rowKey: 'id', selection: 'multi' })selection | Behavior |
|---|---|
'single' (default) | The one clicked row |
'multi' | A checkbox column appears at the start. Click checkboxes to select; the header checkbox selects all visible rows (after filtering) |
'none' | No row selection (active cell and range still work) |
grid.getSelectedRows() // array of selected rows
grid.select(1024) // select by rowKey value; adds to the selection in multi; scrolls into view
grid.select(1024, false) // without scrolling
grid.clearSelection()Every time the selection changes, onSelectionChange(rows) is called. Use it to show the count or total of selected rows.
Even after setRows replaces the rows, rows with the same rowKey value stay selected. Users do not lose what they picked when a screen reloads after saving.
Row click events
createGrid(host, {
columns, rows,
onRowClick: (row, ev) => showDetail(row),
onRowDblClick: row => openEditor(row)
})Current row
grid.setCurrent(row.id) // draws lines above and below and scrolls it into viewIt is independent of row selection. Even with many checked rows, there is only one current row.
Active cell
grid.getActiveCell() // { row, column } or null
grid.setActiveCell(row, 'amount') // move and scroll
grid.startEdit() // start editing that cell (false if it is not editable)Use it to take the user to the cell with an error after saving.
const bad = rows.find(r => !r.account)
if (bad) {
grid.setActiveCell(bad, 'account')
grid.startEdit()
}Range
Drag with the mouse, or extend with Shift+click or Shift+arrow keys. Ctrl+A selects everything. The range is the target of copy, paste and Delete. See Copy · Paste · Excel.
Scrolling
grid.scrollToRow(0) // to the first displayed row
grid.scrollToRow(grid.getViewRows().length - 1) // to the end