Saving state · Personalization
Column order, width and visibility, sorting and grid filters changed by the user can be taken out and applied again as one state (GridState). Your screen decides where (server · localStorage) and when to save it.
import { createGrid } from 'horee-grid'
const KEY = 'docs-example-state' // In a real screen, save to the server by (user, screen, grid name)
const rows = Array.from({ length: 200 }, (_, i) => ({
id: i + 1,
emp: `Employee ${i + 1}`,
dept: ['HR', 'Finance', 'Sales'][i % 3],
grade: ['Associate', 'Senior', 'Manager', 'Senior manager'][i % 4],
pay: 3000000 + (i % 40) * 50000
}))
const grid = createGrid(host, {
locale: 'en',
rowKey: 'id',
columns: [
{ field: 'emp', title: 'Name', width: 100 },
{ field: 'dept', title: 'Dept.', width: 80 },
{ field: 'grade', title: 'Level', width: 80 },
{ field: 'pay', title: 'Salary', type: 'number', width: 120 }
],
rows,
// Adds 'Save layout' · 'Reset layout' to the right-click menu
personalize: {
onSave: state => {
localStorage.setItem(KEY, JSON.stringify(state))
grid.setPersonalized(true)
},
onReset: () => {
localStorage.removeItem(KEY)
grid.setState(null)
grid.setPersonalized(false)
}
}
})
// On open, apply the saved state (column order · width · hidden, sort, filter)
const saved = JSON.parse(localStorage.getItem(KEY) || 'null')
if (saved) {
grid.setState(saved)
grid.setPersonalized(true)
}
toolbar.innerHTML = `<span class="ex-out">Move or hide columns, right-click → Save layout, then reload the page</span>`Getting and applying state
const state = grid.getState()
// { columns: [{ id, width, hidden }, …], sort: [{ id, dir }], filters: [{ id, op, value }] }
grid.setState(state) // apply
grid.setState(null) // back to the initial look (as defined)- The order of the
columnsarray is the column order. - You can apply an old state after the column definitions change. Removed columns are ignored, and new columns not in the state are appended after the saved columns in definition order.
- The state is plain JSON. Save it as is.
If you need only part of it:
grid.getColumnState() / grid.setColumnState(columns)
grid.getSort() / grid.setSort(sort)
grid.getFilters() / grid.setFilters(filters)Save and reset from the right-click menu
With the personalize option, Save layout and Reset layout appear in the right-click menu. When the user clicks them, the grid calls you; your screen does the actual saving.
const grid = createGrid(host, {
columns, rows,
personalize: {
onSave: async state => {
await api.saveGridFormat({ screen: 'AP_INVOICE', grid: 'list', state })
grid.setPersonalized(true)
},
onReset: async () => {
await api.deleteGridFormat({ screen: 'AP_INVOICE', grid: 'list' })
grid.setState(null)
grid.setPersonalized(false)
}
}
})
const saved = await api.loadGridFormat({ screen: 'AP_INVOICE', grid: 'list' })
if (saved) {
grid.setState(saved)
grid.setPersonalized(true)
}setPersonalized(true) shows a small marker in the row-number header, so users know the grid is showing their own settings.
A screen can have several grids. Save by user + screen + grid name so they never mix.
Notifications on every change
If you need auto-save, use the events. (Since accidental changes get saved too, saving from the menu is recommended.)
| Event | Called when |
|---|---|
onColumnChange(columns) | Column order, width or visibility changed |
onSortChange(sort) | The sort changed |
onFilterChange(filters) | Grid filters changed |
Customizing the right-click menu
menuItems receives the default items and the clicked position, and returns the items to show.
createGrid(host, {
columns, rows,
menuItems: (items, at) => [
...items.filter(i => i.id !== 'excel'), // remove Download Excel
...(at.row ? [{ id: 'open', label: 'Open document', separator: true, action: () => openSlip(at.row) }] : [])
]
})id of the default items:
id | Item | Shown when |
|---|---|---|
hideColumn | Hide '…' | Over a column |
hide | Hide columns… | Always |
filter | Grid filter… | Always |
clearFilter | Clear filter | Always (dimmed when there is no filter) |
saveFormat · resetFormat | Save layout · Reset layout | When personalize is given |
excel | Download Excel | Unless excel: false |
Language and texts
The texts the grid shows itself (menus, dialogs, the empty list and the license notice) come in 6 languages. Choose one with the locale option.
locale | Language |
|---|---|
'ko' (default) | Korean |
'en' | English |
'ja' | Japanese |
'zh' | Chinese (Simplified) |
'es' | Spanish |
'pt' | Portuguese |
createGrid(host, { columns, rows, locale: 'en' })Change individual texts with the texts option. Give only what you want to change; the rest comes from locale.
createGrid(host, {
columns, rows,
locale: 'en',
texts: {
empty: 'No rows',
menuExcel: 'Export to Excel',
menuHideColumn: title => `Hide '${title}'`,
ops: { contains: 'Contains' }
}
})For the full list see API › Texts.