horee-gridb0.0.1

Editing

Give a column editable and users can change its cells. The grid writes the value straight into the row object you passed and remembers which cells changed. Saving is up to your screen.

editing.js
import { createGrid } from 'horee-grid'

const kinds = [{ value: 'P', label: 'Part' }, { value: 'C', label: 'Consumable' }, { value: 'S', label: 'Safety gear' }]
const rows = [
  { id: 1, name: 'Bolt M8', kind: 'P', qty: 1200, day: '2026-11-02', use: 'Y', memo: '' },
  { id: 2, name: 'Nut M8', kind: 'P', qty: 1500, day: '2026-11-02', use: 'Y', memo: '' },
  { id: 3, name: 'Cutting oil', kind: 'C', qty: 40, day: '2026-11-10', use: 'N', memo: 'To be discontinued' },
  { id: 4, name: 'Bearing 6204', kind: 'P', qty: 64, day: '2026-11-15', use: 'Y', memo: '' },
  { id: 5, name: 'Gloves (locked)', kind: 'S', qty: 300, day: '2026-11-21', use: 'Y', memo: '', locked: true }
]

const grid = createGrid(host, {
  locale: 'en',
  rowKey: 'id',
  columns: [
    { field: 'name', title: 'Item', width: 140, editable: true, maxLength: 40 },
    {
      field: 'kind', title: 'Type', width: 90, editable: true, editor: 'select', options: kinds,
      format: ({ value }) => kinds.find(o => o.value === value)?.label ?? ''
    },
    { field: 'qty', title: 'Qty', type: 'number', width: 90, editable: row => !row.locked },
    { field: 'day', title: 'Received', type: 'date', width: 110, editable: true },
    { field: 'use', title: 'Active', width: 60, align: 'center', editable: true, editor: 'check', checkValues: ['Y', 'N'] },
    { field: 'memo', title: 'Notes', flex: 1, editable: true }
  ],
  rows,
  onCellChange: () => show()
})

toolbar.innerHTML = `<button data-a="save">Save</button><button data-a="undo">Revert</button><span class="ex-out"></span>`
toolbar.onclick = e => {
  const a = e.target.dataset.a
  if (a === 'save') {
    if (!grid.stopEdit()) return // Commit the value being typed. Stop if it is invalid
    const changes = grid.getChanges() // [{ row, original: { column: original value } }]
    console.log('Rows to send to the server', changes.map(c => c.row))
    grid.clearChanges() // After saving, clear the change marks
  }
  if (a === 'undo') {
    for (const { row, original } of grid.getChanges()) {
      for (const [column, value] of Object.entries(original)) grid.setValue(row, column, value)
    }
  }
  show()
}
function show() {
  toolbar.querySelector('.ex-out').textContent = `Changed rows: ${grid.getChanges().length}`
}
show()

Turning editing on

{ field: 'name', title: 'Name', editable: true, maxLength: 80 }
{ field: 'qty', title: 'Qty', type: 'number', editable: row => !row.closed }   // per row
  • Without editable, or with false, the cell cannot be changed. A function decides per row.
  • To make the whole grid read-only for a while, use the option readOnly: true.
  • By default editing starts with a double-click, F2 or simply typing. To start editing with a single click, use the option editOnClick: true.

Editors

Without editor, the editor follows type.

editorInputStored value
textText. Limit length with maxLengthText
numberNumber. Thousands separators are optionalNumber; null when empty
date2026-11-15, 20261115, 2026/11/15 and so onFollows the shape of the original value (below)
selectPick from a list (options)The value of the picked item
checkClick or Space (checkValues)[on value, off value], default [true, false]
{
  field: 'kind', title: 'Type', editable: true, editor: 'select',
  options: [{ value: 'P', label: 'Part' }, { value: 'C', label: 'Consumable' }],
  format: ({ value }) => ({ P: 'Part', C: 'Consumable' })[value] ?? ''
}
{ field: 'use', title: 'Active', editable: true, editor: 'check', checkValues: ['Y', 'N'] }
  • If options of select is a function, each row can show a different list (row => …).
  • The text shown in a select cell comes from format. Without it, the value (code) is shown.
  • Dates are stored as Date if the original value was a Date, in the same shape if it was 'yyyy-mm-dd', and as 'yyyymmdd' otherwise. This sends values back in the shape your server provided.

Validation

Invalid values, such as text in a number cell or a date that does not exist (2026-02-30), are not accepted. The input gets a red border and stays in edit mode. Press Esc to cancel.

To validate yourself, return undefined from parse.

{
  field: 'acct', title: 'Account', editable: true,
  parse: text => (/^\d{5}$/.test(text) ? text : undefined)   // exactly 5 digits
}
PropertyWhat it does
parse(text, cell)Typed text → value to store. undefined = invalid
editText(cell)Text placed in the input when editing starts
setValue(row, value)Changes how the value is written to the row, e.g. to update other fields too. false = do not change
// changing the code also changes the name
{
  field: 'deptCode', title: 'Dept.', editable: true,
  setValue: (row, v) => {
    const d = depts.find(x => x.code === v)
    if (!d) return false
    row.deptCode = d.code
    row.deptName = d.name
  }
}

Change tracking

Changed cells get a mark in the corner. Restoring the original value removes the mark.

grid.hasChanges()     // is any cell changed?
grid.getChanges()     // [{ row, original: { column id: original value } }]
grid.clearChanges()   // clear the marks after saving; values stay

Every time a user changes a cell, onCellChange({ row, column, field, oldValue, value }) is called. Editing, checkboxes, Delete and paste all behave the same.

Saving

async function save() {
  // 1. Commit the value being typed. false if it is invalid — do not save
  if (!grid.stopEdit()) return

  // 2. Collect only the changed rows
  const changed = grid.getChanges().map(c => c.row)
  if (!changed.length) return toast('Nothing has changed')

  // 3. Send them to the server and clear the marks on success
  await api.save(changed)
  grid.clearChanges()
}
Call stopEdit first

A user can type a number in a cell and click the save button without pressing Enter. Without stopEdit(), the last input is lost.

Changing values in code

grid.setValue(row, 'amount', 5000)   // adds a change mark; works for hidden columns too

If you do not need change tracking, change the row object directly and redraw.

row.total = row.qty * row.price
grid.refreshRow(row)       // one row
grid.refresh()             // everything (recalculates sort and filter)

Adding and removing rows

The grid never adds or removes rows by itself. Change the array and pass it to setRows.

const row = { id: tempId(), qty: 0 }
grid.setRows([...grid.getRows(), row])
grid.setActiveCell(row, 'item')
grid.startEdit()
const picked = new Set(grid.getSelectedRows())
grid.setRows(grid.getRows().filter(r => !picked.has(r)))