编辑
给列设置 editable 后,用户就可以修改该单元格。表格把值直接写入传入的行对象,并记住修改过的单元格。保存由页面负责。
editing.js
import { createGrid } from 'horee-grid'
const kinds = [{ value: 'P', label: '零件' }, { value: 'C', label: '耗材' }, { value: 'S', label: '安全用品' }]
const rows = [
{ id: 1, name: '螺栓 M8', kind: 'P', qty: 1200, day: '2026-11-02', use: 'Y', memo: '' },
{ id: 2, name: '螺母 M8', kind: 'P', qty: 1500, day: '2026-11-02', use: 'Y', memo: '' },
{ id: 3, name: '切削油', kind: 'C', qty: 40, day: '2026-11-10', use: 'N', memo: '即将停产' },
{ id: 4, name: '轴承 6204', kind: 'P', qty: 64, day: '2026-11-15', use: 'Y', memo: '' },
{ id: 5, name: '手套(锁定)', kind: 'S', qty: 300, day: '2026-11-21', use: 'Y', memo: '', locked: true }
]
const grid = createGrid(host, {
locale: 'zh',
rowKey: 'id',
columns: [
{ field: 'name', title: '物料名称', width: 140, editable: true, maxLength: 40 },
{
field: 'kind', title: '类别', width: 90, editable: true, editor: 'select', options: kinds,
format: ({ value }) => kinds.find(o => o.value === value)?.label ?? ''
},
{ field: 'qty', title: '数量', type: 'number', width: 90, editable: row => !row.locked },
{ field: 'day', title: '入库日期', type: 'date', width: 110, editable: true },
{ field: 'use', title: '启用', width: 60, align: 'center', editable: true, editor: 'check', checkValues: ['Y', 'N'] },
{ field: 'memo', title: '备注', flex: 1, editable: true }
],
rows,
onCellChange: () => show()
})
toolbar.innerHTML = `<button data-a="save">保存</button><button data-a="undo">恢复原值</button><span class="ex-out"></span>`
toolbar.onclick = e => {
const a = e.target.dataset.a
if (a === 'save') {
if (!grid.stopEdit()) return // 确认正在输入的值。值无效则停止
const changes = grid.getChanges() // [{ row, original: { 列: 原值 } }]
console.log('要发送到服务器的行', changes.map(c => c.row))
grid.clearChanges() // 保存后清除修改标记
}
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 = `已修改 ${grid.getChanges().length} 行`
}
show()开启编辑
{ field: 'name', title: '名称', editable: true, maxLength: 80 }
{ field: 'qty', title: '数量', type: 'number', editable: row => !row.closed } // 按行- 没有
editable或为false时不可修改。为函数时按行决定。 - 要暂时把整个表格设为只读,使用选项
readOnly: true。 - 默认通过双击 · F2 · 直接输入开始编辑。要单击即开始编辑,使用选项
editOnClick: true。
编辑器
不设置 editor 时跟随 type。
editor | 输入 | 保存的值 |
|---|---|---|
text | 文字。用 maxLength 限制长度 | 文字 |
number | 数字。千分位逗号可有可无 | 数字,为空时 null |
date | 2026-11-15、20261115、2026/11/15 等 | 跟随原值的形式(见下文) |
select | 从列表中选择(options) | 所选项的 value |
check | 点击或 Space(checkValues) | [选中值, 未选中值],默认 [true, false] |
{
field: 'kind', title: '类别', editable: true, editor: 'select',
options: [{ value: 'P', label: '零件' }, { value: 'C', label: '耗材' }],
format: ({ value }) => ({ P: '零件', C: '耗材' })[value] ?? ''
}
{ field: 'use', title: '启用', editable: true, editor: 'check', checkValues: ['Y', 'N'] }select的options设为函数时,每行可以显示不同的列表(row => …)。select单元格中显示的文字由format决定。不设置时显示值(代码)。- 日期:原值是
Date则保存为Date,是'yyyy-mm-dd'则保存为相同形式,其他情况保存为'yyyymmdd'。这样可以按服务器提供的形式原样返回。
值校验
数字单元格中的文字、不存在的日期(2026-02-30)等无效值不会被接受。输入框会出现红色边框并保持编辑状态。按 Esc 可以取消。
要自行校验,在 parse 中返回 undefined。
{
field: 'acct', title: '科目', editable: true,
parse: text => (/^\d{5}$/.test(text) ? text : undefined) // 仅限 5 位数字
}| 属性 | 作用 |
|---|---|
parse(text, cell) | 输入的文字 → 要保存的值。undefined = 无效值 |
editText(cell) | 开始编辑时放入输入框的文字 |
setValue(row, value) | 改变把值写入行的方式,用于同时修改其他字段。false = 不修改 |
// 修改代码时同时修改名称
{
field: 'deptCode', title: '部门', 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
}
}修改跟踪
修改过的单元格角上会出现标记。恢复为原值后标记消失。
grid.hasChanges() // 是否有修改过的单元格
grid.getChanges() // [{ row, original: { 列 id: 原值 } }]
grid.clearChanges() // 保存后清除标记,值保持不变用户每次修改单元格都会调用 onCellChange({ row, column, field, oldValue, value })。编辑、勾选、Delete、粘贴都一样。
保存流程
async function save() {
// 1. 确认正在输入的值。值无效时返回 false — 不保存
if (!grid.stopEdit()) return
// 2. 只收集修改过的行
const changed = grid.getChanges().map(c => c.row)
if (!changed.length) return toast('没有修改')
// 3. 发送到服务器,成功后清除标记
await api.save(changed)
grid.clearChanges()
}先调用 stopEdit
用户可能在单元格中输入数字后不按 Enter 就直接点击保存按钮。不调用 stopEdit() 的话,最后的输入会丢失。
用代码修改值
grid.setValue(row, 'amount', 5000) // 会出现修改标记。隐藏的列也可以不需要修改跟踪时,直接修改行对象再重绘。
row.total = row.qty * row.price
grid.refreshRow(row) // 一行
grid.refresh() // 全部(重新计算排序 · 筛选)添加 · 删除行
表格不会自行增加或删除行。修改数组后用 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)))