We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to prevent columns from persisting in ASP.NET MVC Grid

Hi.

I have a grid with both .EnablePersistence() and .AllowReordering().

At some point, based on a condition in a js, I am deleting everything from localstorage, but the MVC grid still persists the columns ordering :(

Here's the JS


                var arr = []; // Array to hold the keys
                var i;

                for (i = 0; i < localStorage.length; i++) {
                    if (localStorage.key(i).substring(0, 3) === "$ej") {
                        arr.push(localStorage.key(i));
                    }
                }

                for (i = 0; i < arr.length; i++) {
                    localStorage.removeItem(arr[i]);
                }

1 Reply

SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team July 17, 2019 06:39 AM UTC

Hi Catalin,  

Thanks for contacting Syncfusion Support. 

We could see you would like to prevent the columns from persisting in the Grid. To avoid the persistence for the Grid columns, override the addOnPersist method of the Grid and remove the columns from the key list given for persistence. In the following code example, we have overrode the addOnPersist method within the DataBound event of the Grid. 

@Html.EJS().Grid("DefaultFunctionalities").DataSource((IEnumerable<object>)ViewBag.dataSource).DataBound("onBound").AllowReordering().EnablePersistence(true) 
.AllowPaging().PageSettings(page=>page.PageCount(5)).Render() 
 
<script> 
    function onBound(args) { 
        var cloned = this.addOnPersist; 
        this.addOnPersist = function (key) { 
            key = key.filter(item => item !== "columns") 
            return cloned.call(this, key); 
        };  
    } 
</script> 

Regards,  
Seeni Sakthi Kumar S. 


Loader.
Live Chat Icon For mobile
Up arrow icon