Selective Persistance in SfTreeGrid
I have the task of backing up and restoring tree grid persistence with user data. The preferred way is to call SetPersistDataAsync to save the state. However, I don't want to store everything in the state; currently, the entire column class with all properties is being serialized. Additionally, I don't want to persist grouping or other specific settings. Is there a way to select only the data I want to persist?
The second task is to restore this data when the tree grid is loaded. I tried to do this in the OnInitializedAsync method or the Created and Loaded events. However, regardless of which event I use to restore the data, the tree grid is rendered with the initial defaults. The state is restored correctly only after I expand a node, change the column order, or trigger a refresh. Is there a way to restore the state before the tree grid is rendered? Am I using the wrong events?
Hi,
Thank you for reaching out regarding tree grid persistence. Let's address your concerns:
- Selective Persistence:
From your query we understand that you want to persist only specific properties of TreeGrid. We've prepared a sample demonstrating how to selectively persist data. Please review the attached sample, which shows how to customize the persistence process to include only the desired properties.
- State Restoration:
Regarding the issue with state restoration, we've been unable to reproduce the problem on our end. In our tests, the persist property loads correctly after a refresh. To help us investigate further, could you please:
- Try to reproduce the issue using the attached sample and let us know the results.
- If the issue persists, provide a simple sample with video demonstration along with detailed steps to reproduce the issue.
This information will greatly assist us in understanding and resolving the state restoration issue.
Best regards,
saran.
Attachment: TreeGridSelectivePersistence_438a3b6f.zip
Hi,
Thank you for your detailed answer.
Regarding the first part, deconstructing the JSON worked, but I still have a very large serialized list containing all properties of the TreeGridColumn class. In my case, I only need the column width information. Is it possible to further refine the JSON and remove all other unused property values?
For the second part, in your example, the grid settings are restored with a button, which also works in my case. However, I need to restore the settings when the grid is loading. The user should not see the initial grid state but only the persisted state. My question is: which event should I use to achieve this? If I knew the correct event, I could modify the sample accordingly.
Hi Frederic Beckmann,
For Query 1, you can reset settings such as filterSettings, sortSettings, searchSettings, and pageSettings in the saved persisted data. Please refer to the following code snippet:
|
_state = await TreeGrid.GetPersistDataAsync();
dynamic PersistProp = JsonSerializer.Deserialize<Dictionary<string, object>>(_state.ToString()); // we can remove/save only the required settings.
dynamic _statek = JsonSerializer.Deserialize<TreeGridSearchSettings>(PersistProp["filterSettings"].ToString()); dynamic _statek2 = JsonSerializer.Deserialize<TreeGridSortSettings>(PersistProp["sortSettings"].ToString()); dynamic _statek3 = JsonSerializer.Deserialize<TreeGridPageSettings>(PersistProp["pageSettings"].ToString()); dynamic _statek4 = JsonSerializer.Deserialize<TreeGridSearchSettings>(PersistProp["searchSettings"].ToString());
_statek.Key = ""; //Set your custom search value
PersistProp["filterSettings"] = JsonSerializer.Serialize(_statek).ToString(); PersistProp["sortSettings"] = JsonSerializer.Serialize(_statek2).ToString(); PersistProp["pageSettings"] = JsonSerializer.Serialize(_statek3).ToString(); PersistProp["searchSettings"] = JsonSerializer.Serialize(_statek4).ToString(); _state = JsonSerializer.Serialize(PersistProp);
Service.setGridState(_state); |
For Query 2, you can use the created event. Every time the TreeGrid is initialized, the created event will trigger.
Let us know if you need further assistance.
Regards,
Shek
- 3 Replies
- 3 Participants
-
FB Frederic Beckmann
- Feb 10, 2025 11:16 AM UTC
- Mar 18, 2025 05:07 AM UTC