Persist and reset Grid column order and width in localStorage
Environment
| Product Version | 16.1.0 |
| Product | Progress® KendoReact Grid |
Description
I want to persist the order and width of KendoReact Grid columns when users resize or reorder them. My columns are generated by a custom hook from a configuration array, and I want to restore the original configuration order when needed.
This KB also answers the following questions:
- How do I persist KendoReact Grid column order and widths in
localStoragewhen columns are generated by a custom hook? - How can I load saved Grid column state without refreshing the page?
- How do I reset persisted Grid column order and render columns in their original configuration order?
Solution
Use the Grid columnsState prop to control column width and order. Keep the configuration array as the source of truth for column declarations, and persist the state collection separately. The Grid state contains the current field, width, and orderIndex values after a column is resized or reordered. The Grid uses smaller orderIndex values first, so you do not need to sort the configuration array. For more information, see the orderIndex property.
The following example demonstrates how to persist and reset KendoReact Grid column order and width in localStorage when columns are generated by a custom hook.
To implement the solution, follow these steps:
-
Define the columns with stable
idvalues. The Grid uses the IDs to associate the persisted state with each declared column.tsxconst columnConfig = [ { id: 'product-id', field: 'ProductID', width: 200 }, { id: 'product-name', field: 'ProductName', width: 200 }, { id: 'category', field: 'Category', width: 200 }, { id: 'unit-price', field: 'UnitPrice', width: 200 }, { id: 'units-in-stock', field: 'UnitsInStock', width: 200 } ]; -
Create the initial state from the configuration array. It establishes the original width and order that the reset action will restore.
tsxconst createInitialColumnsState = (config) => config.map((column, orderIndex) => ({ id: column.id, field: column.field, width: column.width, orderIndex })); -
Pass the state collection to the Grid and update it through
onColumnsStateChange. This keeps the current column order and width in React state.tsxconst [columnsState, setColumnsState] = React.useState(() => createInitialColumnsState(columnConfig)); <Grid columnsState={columnsState} onColumnsStateChange={(event) => setColumnsState(event.columnsState)} reorderable={true} resizable={true} > {columnConfig.map((column) => ( <GridColumn key={column.id} {...column} /> ))} </Grid> -
Save and load
columnsStatewhen the user requests it. Loading replaces the controlled state, so the Grid immediately applies the saved column widths and order without a page refresh.tsxconst saveColumnState = () => { localStorage.setItem('grid-columns-state', JSON.stringify(columnsState)); }; const loadColumnState = () => { const savedState = localStorage.getItem('grid-columns-state'); if (savedState) { setColumnsState(JSON.parse(savedState)); } }; -
Restore the initial order by replacing
columnsStatewith the state derived from the original configuration array. Select Save afterward if the reset state should persist across future page loads.tsxconst resetColumnOrder = () => { setColumnsState(createInitialColumnsState(columnConfig)); };