Grid filter persistence

Hi there.

Is there any elegant way to capture the current filters that were set/select by the user, write them to a non-volatile storage , I.e. json file, and then later on retrieve that filter settings and apply it to a grid?


I want to support kind of filter bookmarking by the user.


With regards. alon


3 Replies

PS Prathap Senthil Syncfusion Team November 2, 2023 02:51 PM UTC

Hi alon,


Based on your requirements, we recommend manually managing the Grid state using our built-in methods: GetPersistData and SetPersistData. You can use these methods to retrieve the current grid state and apply it to the Grid. By using GetPersistData, you can obtain the grid state and store these persisted settings in a string variable. This string variable can then be customized to include only your preferred settings before storing it. This ensures that it persists according to the changes made to the persisted settings.


To utilize these settings, we suggest assigning the new Grid settings after deserialization and saving the filter settings. You can store the necessary property in a local variable and assign it after removing all other properties using the new Grid settings. Please refer to the code snippet and sample below for a better understanding


 

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

<SfButton @onclick="LoadSateFilter">Load</SfButton>

<SfButton @onclick="Reset">Reset</SfButton>

 

<SfGrid TValue="Order" ID="Grid" @ref="Grid" DataSource="Orders" AllowSorting="true" AllowFiltering="true" AllowPaging="true" Toolbar="@(new List<string>() { "Add", "Delete", "Update", "Cancel","Search" })">

   -----------

</SfGrid>

 

@code {

    public async void Save()

    {

        _state = await Grid.GetPersistData();

 

        dynamic PersistProp = JsonSerializer.Deserialize<Dictionary<string, object>>(_state.ToString()); // we can remove/save only the required settings.

 

 

        dynamic _state1 = JsonSerializer.Deserialize<GridSearchSettings>(PersistProp["searchSettings"].ToString());

        _state1 = new GridSearchSettings();

        PersistProp["searchSettings"] = JsonSerializer.Serialize(_state1).ToString();

 

 

    

        dynamic _state2 = JsonSerializer.Deserialize<GridFilterSettings>(PersistProp["filterSettings"].ToString());

 

        _state1 = JsonSerializer.Serialize(PersistProp);

        Service.setGridState(_state1); //Stored filter settings

   

 

    }

 

 

    public void LoadSateFilter()

    {

        string Griddata = Service.GetGridState();

        Grid.SetPersistData(Griddata);

    }

    public void Reset()

    {

        Grid.ResetPersistDataAsync();

    }

 

 

}


 Note: _state1: After Deserialization we have assigned the new grid settings. We can disable all the settings by using the same steps. 


We have documented this topic, kindly refer to the below documentation for more details. 


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


Regards,

Prathap S


Attachment: DataGridSample_aa6495d9.zip


AL alon November 8, 2023 03:30 AM UTC

Thanks for your helpful reply


Handling grid state is a great feature!

Actually, I think I will save all the settings  that are supported by the grid state.


Regards, alon.  



SP Sarveswaran Palani Syncfusion Team November 8, 2023 04:09 AM UTC

Hi Andy,

Thanks for an update.

We suspect that your issue has been resolved in your end. If you have any further queries, please get back to us.

Regards,
Sarvesh


Loader.
Up arrow icon