API for Column Chooser?

I would like to persist the currently-selected columns (from the column chooser) as a user preference, and then restore that selection of columns for the user next time. After reading the API guide, however, I'm not seeing how to implement this.

Is there an event that is triggered after the user changes the selected columns in the column chooser?

I see that there is a getVisibleColumnNames() method; is there something analogous for setting the visible columns?

Thanks --

--Andy



3 Replies

SE Sathyanarayanamoorthy Eswararao Syncfusion Team April 4, 2018 12:48 PM UTC

Hi Andy, 

We have analyzed your query and found that you need to save the state of the grid after columns are selected from the column chooser and then apply the same later. We have already discussed about this query in a Knowledge Base documentation which can be found from the below link. 


In the above KB we have stored the grouped columns for your  requirement you need to store the columns details in the variable. We have modified the code that need to be changed in  the KB. Please refer the below code example for the changes that need to be made in the code of KB. 

 
function saveState(args) { 
        var gridObj = $("#Grid").ejGrid("instance"); 
        var dropDownObj = $('#list').ejDropDownList("instance"); 
 
        var state = { 
            cols: gridObj.model.columns 
        } 
        var object = JSON.stringify(state);//converting object to string 
                    
                     ….. 
 
function applyState(args) { 
        var gridObj = $("#Grid").ejGrid("instance"); 
        var obj = $('#list').ejDropDownList("instance"); 
        var value = obj.model.value; 
 
        //Post the saved (in ejDropDownlist) time 
        //To retrieve the Grid objects 
        $.ajax({ 
            type: "POST", 
            url: "/Home/Restate", 
            data: { "dropObject": value }, 
            success: function (data) { 
                var obj = JSON.parse(data); 
                if (obj.cols) 
                    gridObj.columns(obj.cols); 
                else 
                    gridObj.refreshContent(); 
 
                     ….. 
 

We have prepared a sample with the same requirement for your convenience which can be downloaded from the below location. 


If you need any further assistance please get back to us. 

Regards, 
Sathyanarayanamoorthy 



AG Andy Gray April 4, 2018 06:32 PM UTC

Thanks, Sathyanarayanamoorthy - this is helpful.

How do I know when to save the state? Is there an event that is raised when the column chooser is dismissed?

Thanks --

--Andy



SE Sathyanarayanamoorthy Eswararao Syncfusion Team April 5, 2018 02:22 PM UTC

Hi Andy, 

In the previous update we have provided a solution as if savestate button is clicked then the state of the grid is saved. But if you need to save the grid state automatically when the done button of column chooser is clicked, then we suggest you to use the actionComplete event of the grid to save the state which is triggered for every complete action. For applying the saved state we suggest you to use the applystate button which is given in the previous update. 

Refer the below code example. 

 
<ej-grid id="Grid" datasource="(IEnumerable<object>)ViewBag.dataSource"  action-complete="onActionComplete" allow-paging="true" allow-filtering="false" SelectionType="Single" show-column-chooser="true"> 
     
    <e-columns> 
         
                  ….. 
 
   </e-columns> 
</ej-grid> 
 
<script type="text/javascript"> 
 
    function onActionComplete(args) { 
 
        if(args.requestType == "columnchooser"){ 
 
                var gridObj = $("#Grid").ejGrid("instance"); 
                var dropDownObj = $('#list').ejDropDownList("instance"); 
 
        var state = { 
            cols: gridObj.model.columns 
        } 
        var object = JSON.stringify(state);//converting object to string 
 
        $.ajax({ 
            type: "POST", 
            url: "/Home/Query", 
            data: { "gridObj": object },//posting the grid object as string 
            success: function (data, status, xhr) { 
                //On Success save the data which is the time 
                //based on the time saving of Grid object in the db takes place 
                var TempData = []; 
                var obj = $('#list').ejDropDownList("instance"); 
 
                if (!ej.isNullOrUndefined(obj.model.dataSource)) 
                    TempData = obj.model.dataSource; 
 
                TempData.push({ dataTime: data, text: data }); 
 
                $('#list').ejDropDownList("destroy"); 
                //destroy and update the dropdownlist's dataSouce 
                $('#list').ejDropDownList({ dataSource: TempData }) 
            }, 
        }); 
        } 
    } 


We have prepared a sample which can be downloaded from below link. 


If you need any further assistance please get back to us. 

Regards, 
Sathyanarayanamoorthy 



Loader.
Up arrow icon