So, I'm trying to figure out a way to persist grid customization across user sessions for our application. So I don't need to persist the data being displayed, however, I do need to be able to persist what order the columns are currently in for our table, as the user needs to be able to drag and order the columns around and will want that to persist across sessions. We also need to be able to know which Columns are being shown and hidden such that I would be able show/hide those upon the next time a user logs in.
Any insight would be greatly appreciated.
Thanks,
-Randall Blevins
Hi Randall,
Please see this stackblitz example on how to Save and Load igxGrid State. (please navigate the internal stackblitz window to https://grid-save-state.stackblitz.io/grid-state)
This sample uses an angular directive, that is being applied to the igxGrid and saves the grid state (in terms of sorting, filtering, selection, columns order and state, paging) to the page localStorage. How is that achieved ?
1) Create a directive with the name of igxState or another:
2) Hook to the ngOnInit event of the directive lifecycle, where:
2.1: Subscribe to the NavigationStart event of the page, i.e. this is when the user navigates away from the page. This is the key moment, at which we save the current grid state. When such event occurs, the saveGridState function is called, that contains the logic to write the grid state to the localStorage:
2.2: Call the loadGridState method, that contains the logic to read the data from the localStorage and apply it to the grid.
2.3: Use the ngOnInit lifecycle hook of the grid component to restore the grid columns from the localStorage:
Please refer to the stackblitz example for the complete code, which also contains logic fo the UI buttons on the page, that you will most likely not need. You may want to use the sessionStorage instead of the localStorage. Please let me know if you have any questions.
Hristo
I have similar use case and but for hierarchical grid. I used your sample and change the grid object to hierarchical grid. I can see while navigating settings are getting saved in local storage. but when coming back to page again its not retrieving and the reason I found is its always changing the grid id. on initial navigation grid id was "igx-hierarchical-grid-0", in second load it is "igx-hierarchical-grid-1" and keep on increasing the number and creating new key everytime.
Thanks for this, it was extremely helpful, I appreciate it.