Provide a PersistData class to help manipulate the state management

Hi,

There are some cases where the persisted data is not valid anymore and we have to detect that the cache needs to be cleared ou updated programatically.


You should provide a class so that we can deserialize the string json we get with grid.GetPersistDataAsync().

It would ensure that the properties are up to date according to the data that Syncfusion persists and we could set some properties more easily.


As a result we could do for example :
var persistDataObj = JsonSerializer.Deserialize<PersistDataClass>(await grid.GetPersistDataAsync());

persistDataObj.PageSettings.PageSize = x;

await grid.SetPersistDataAsync(persistDataObj.ToJson());


Regards.


3 Replies 1 reply marked as answer

GR Guhanathan Ramanathan Syncfusion Team March 24, 2025 12:07 PM UTC

Hi Julien Barach,

 

From your query we suspect that you need to persist only certain operations in grid. We would like to inform by using EnablePersistence it will persist all the possible state of DataGrid. It is the default behavior. So we suggest you to handle Grid state manually. Here we have cleared the paging before saving the grid state on Save method. Kindly refer the attached code snippet and sample for your reference.

 

<SfButton  @onclick="Save">save</SfButton>
<SfButton  @onclick="Load">Load</SfButton>
<SfButton @onclick="Reset">Reset</SfButton>

<SfGrid TValue="Order" ID="Grid1" @ref="Grid" DataSource="Orders" AllowSorting="true" AllowFiltering="true" ShowColumnChooser="true" AllowPaging="true" Toolbar="@(new List<string>() { "ColumnChooser"})">
    <GridFilterSettings Type="Syncfusion.Blazor.Grids.FilterType.Menu"></GridFilterSettings>
    <GridPageSettings PageSize="8"></GridPageSettings>
</SfGrid>

@code{
    public static List<Order> Orders { get; set; }
    SfGrid<Order> Grid;
    public string _state;

    protected override void OnInitialized()
    {
        Orders = Enumerable.Range(1, 75).Select(x => new Order()
        {
            OrderID = 1000 + x,
            CustomerID = (new string[] { "ALFKI", "ANANTR", "ANTON", "BLONP", "BOLID" })[new Random().Next(5)],
            Freight = 2.1 * x,
        }).ToList();
    }

    public class Order
    {
        public int OrderID { get; set; }
        public string CustomerID { get; set; }
        public double Freight { get; set; }
    }
    public async void Save()
    {
        _state = await Grid.GetPersistDataAsync();
        dynamic PersistProp = JsonSerializer.Deserialize<Dictionary<string, object>>(_state.ToString());   // we can remove/save only the required settings.
        dynamic _statek = JsonSerializer.Deserialize<GridSearchSettings>(PersistProp["pageSettings"].ToString());    
        _statek.Key = "";  
       
        PersistProp["pageSettings"] = JsonSerializer.Serialize(_statek).ToString();
        _state = JsonSerializer.Serialize(PersistProp);

        Service.setGridState(_state);
    }
    public void Load()
    {
        string Griddata = Service.GetGridState();
        Grid.SetPersistDataAsync(Griddata);
    }
    public void Reset()
    {
        Grid.ResetPersistDataAsync();
    }

   
}

Reference: https://blazor.syncfusion.com/documentation/datagrid/state-management#handling-grid-state-manually


Kindly get back to us if you have further queries.

 

Regards,

Guhanathan R


Attachment: DataGrid_fe80d78d.zip


JB Julien Barach March 24, 2025 01:00 PM UTC

That's why it would be convenient to have a class to deserialize to instead of using dynamic or my own class.

What if Syncfusion decides to rename or move some properties ? This code would fail at runtime.



GR Guhanathan Ramanathan Syncfusion Team March 25, 2025 07:31 AM UTC

Hi Julien Barach,

Thank you for your patience.

From your requirement, we understand that are handling the Persistence externally using method and using custom logic to persist certain properties alone instead of persisting all the properties. We have already identified this as a usability improvement: 'Need to provide a support to customize list of persisted properties'. We would like to inform you that this improvement will be included in any of upcoming release.

You can track the status of your request, review the proposed resolution timeline, and contact us for any further inquiries via this link.

We request you to track our roadmap to consider this improvement in any of our upcoming release. Till then we request you to achieve your requirement using the solution provided in previous update.


Regards,

Guhanathan R


Marked as answer
Loader.
Up arrow icon