How to save column chooser selected values as user preference

Hi All,

I want to add a column chooser and want to save my selected values as a preference. So whenever i navigate to other page or reload the page it will stay what i selected (i.e, only those columns should be displayed which I selected. on my last visit )

Could you please help[ me with this.

Thanks in advance !!

Best Regards
Madhur

9 Replies

AG Ajith Govarthan Syncfusion Team April 16, 2020 09:51 AM UTC

Hi Madhur, 

Greetings from Syncfusion. 

To achieve your requirement we suggest you to use State Persistence feature of our EJ2 Grid. By enabling this feature which will maintain Grid’s state in the browser’s localStorage even if the browser is refreshed or if you move to the next page within the browser. State persistence stores grid’s model object in the local storage when the enablePersistence is defined as true.  We have attached the prepared sample So please refer the sample for your reference. 



Regards, 

Ajith G. 



MG Madhur Goyal April 16, 2020 12:39 PM UTC

Hi Ajith,

Thanks for your response. It works.great !!

But it is working for a single browser i want my all browser to show same settings. For this may be i can use indexeddb or is there other way of having same layout  across all browsers.

With this I also want to know if I can override the css classes and give my own styling to it with changing text Column and Column Chooser.

Waiting for your reply !!

Thanks & Regards,
Madhur 





AG Ajith Govarthan Syncfusion Team April 17, 2020 03:00 PM UTC

Hi Madhur,  
 
Query : But it is working for a single browser i want my all browser to show same settings. For this may be i can use indexeddb or is there other way of having same layout  across all browsers. 
 
By default , the State Persistence feature worked based on browser local storage. If you the run same sample in that same browser(without clear history) , The Grid loaded the settings from the browser local storage. So the Grid sample rendered with your  last changes. But if you run the sample in another browser , the Grid can render in that first time in that browser (local storage is not any information , this is default behavior of browser). So it will be rendered based on your Grid model definition . So this is not possible load the Grid settings from one browser to another browser. 
 
 
Query : if I can override the css classes and give my own styling to it with changing text Column and Column Chooser. 
 
Yes, you can apply customized style for column chooser by using the below css. 
 
Apply style for column chooser column names ( column name appeared in near by checkbox)  
 
<style> 
.e-grid .e-cc .e-label{ 
  Color: red; 
} 
</style> 
 
 
Apply style for column chooser header style (choose column) 
 
 
.e-grid .e-cc .e-dlg-header{ 
  Color: red; 
} 
 
 
 
Please find the documentation for customized style for Grid  
 

Please find the documentation for apply specific column customization  

 
Regards, 
Ajith G 



MG Madhur Goyal April 17, 2020 04:19 PM UTC

Hi Ajith,

I understand what you explained but I have more queries:

1. Today I noticed while using statePersistence property : what is happening if i choose2 columns out of say 10 I am getting same when I refresh the page or navigate to different screen. But in some of cases it is showing 3 columns which I choose earlier before my last choice.
My last set preference was 2 columns but some times it shows 3 or even all columns.

2. My  column chooser is not same as shown in demo. I am getting extra checkboxes, a dot and there is no search button, border and scroll bar as shown in demo.
That's why I asked If I can override the css and apply those.

3. In addition to this I want to change the name of Columns button  to Columns Chooser and same for Column Chooser dialog header.

Sorry for the inconvenience caused.

Best Regards,
Madhur 


MF Mohammed Farook J Syncfusion Team April 20, 2020 07:34 AM UTC

Hi Madhur , 
 
Thanks for your update. 
 
Query 1: what is happening if i choose2 columns out of say 10 I am getting same when I refresh the page or navigate to different screen. But in some of cases it is showing 3 columns which I choose earlier before my last choice.My last set preference was 2 columns but some times it shows 3 or even all columns. 
 
We are tried to reproduce the reported issue but it was unsuccessful. So please share the following information to proceed  further. 
 
  1. Share the full code example of Grid rendering
  2. Share Grid sample if it is possible
  3. Share the demonstration about your issue
  4. Share the EJ2 package version details and share your package.json .
 
 
Query 2:  My  column chooser is not same as shown in demo. I am getting extra checkboxes, a dot and there is no search button, border and scroll bar as shown in demo. 
That's why I asked If I can override the css and apply those. 
  
If want to hide the search option in the Column-chooser by using the following CSS. 
 
.e-grid .e-cc.e-cc-searchdiv{ 
  display: none; 
} 
 
 
By default Column-chooser checkbox  and its names rendered based on Grid Column definition and its Column field Names. So please share the screen shot about your requirement and it will be helps to further proceeding your requirement. 
 
 
Query 3: In addition to this I want to change the name of Columns button  to Columns Chooser and same for Column Chooser dialog header. 
 
 
Yes, you can customize the column chooser header and its button text  by using following code. 
 
import { Component, OnInit, ViewChild, ViewEncapsulation } from '@angular/core'; 
import { orderDetails, data } from './data'; 
import { L10n, setCulture } from '@syncfusion/ej2-base'; 
import {  ToolbarService, ColumnChooserService,GridComponent } from '@syncfusion/ej2-angular-grids'; 
 
L10n.load({ 
    'en-US': { 
        grid: { 
         ChooseColumns : 'Select columns', 
         OKButton:'Confirm', 
         CancelButton:'Canceled' 
        } 
      } 
  }) 
@Component({ 
    selector: 'app-root', 
    templateUrl: 'app.component.html', 
    providers: [ToolbarService,ColumnChooserService], 
    styleUrls: ['app.component.css'], 
    encapsulation: ViewEncapsulation.None 
     
}) 
export class AppComponent { 
     public data: Object[]; 
    public toolbar: string[]; 
    public pageSettings: Object; 
    beforeOpenColumnChooser(args){ 
   
    } 
    ngOnInit(): void { 
         this.data = data; 
         this.toolbar = ['ColumnChooser']; 
         this.pageSettings ={ pageCount: 5 }; 
    } 
} 
 
 
In the above code, we have changed the Column-chooser header text and Ok and cancel button text. 
 
Please find the documentation for override the default Grid display text. 
 
 
 
you can get the Column-chooser argument in beforeColumnChooser event of Grid while opening the column-chooser. 
 
 
Regards, 
J Mohammed Farook  
  
   
 



MG Madhur Goyal April 20, 2020 08:56 AM UTC

Hi J Mohammed,

Thanks for your response!!

For the first problem it is now working fine. I think something got changed which caused the problem. But Could you please let me know how can I save the state in indexeddb.

For my second query I have applied the css and hide the extra checkboxes. Is it possible i can apply a background Image instead of Cloumn Chooser button text Columns.

For my third query I tried your solution it worked but I want it globally not for a particular locale. So is it possible that I can change the text globally. 

Regards,
Madhur Goyal


MF Mohammed Farook J Syncfusion Team April 21, 2020 11:25 AM UTC

Hi Madhur ,  
 
Query 1: For the first problem it is now working fine. I think something got changed which caused the problem. But Could you please let me know how can I save the state in indexeddb. 
 
We are happy to hear that your issue has been resolved. We have suspect that , you want to save what are the column has changed the visibility(show/hide) from the Grid. We have suggest to  use the following code you can get show columns and hide columns from the grid . 
 
 <ejs-grid #grid [dataSource]='data' (actionComplete)='actionComplete($event)' allowPaging='true' showColumnChooser='true' [toolbar]="toolbar" > 
. .  
    </ejs-grid> 
  </div> 
 
import { ToolbarService, ColumnChooserService, GridComponent, Column } from '@syncfusion/ej2-angular-grids'; 
import { data } from './data'; 
 
@Component({ 
    selector: 'app-root', 
    templateUrl: 'app.component.html', 
    providers: [ToolbarService, ColumnChooserService] 
}) 
export class AppComponent { 
    public data: Object[]; 
    public toolbar: string[]; 
    public pageSettings: Object; 
    @ViewChild('grid') 
    public grid : GridComponent; 
    public showColumns : Column[]; 
    public hideColumns : Column[]; 
    actionComplete(args){ 
      if(args.requestType==='columnstate'){ 
        this.showColumns = this.grid.getVisibleColumns(); 
        this.hideColumns = this.grid.getHiddenColumns(); 
 
      } 
    } 
 .. . 
 
In the above code, we can get the show and hide column when pressed ok button of the column chooser through actionComplete  event with its requestType as ‘columnstate’  by using ‘getVisibleColumns()’ and ‘getHiddenColumns()’  method of Grid. Please find the documentation for your reference. 
 
 
you can store the column details in your database as you want.  This is not meet your requirement could you please explain more details about your requirement. Also share the details about in which state you want to save in your database. 
 
 
Query 2: Is it possible i can apply a background Image instead of Cloumn Chooser button text Columns.    
 
You can change the column chooser button from the toolbar by using the following code. 
 
[app.component.html] 
 <ejs-grid #grid (dataBound)="dataBound($event)"[dataSource]='data' allowPaging='true' showColumnChooser='true' [toolbar]="toolbar" [pageSettings]='pageSettings'> 
        <e-columns> 
            <e-column field='OrderID' isPrimaryKey='true' headerText='Order ID' width='120' textAlign='Right'></e-column> 
            <e-column field='CustomerName' headerText='Customer Name' width='150' [showInColumnChooser]='false'></e-column> 
            <e-column field='OrderDate' headerText='Order Date' width='130' format="yMd" textAlign='Right'></e-column> 
            <e-column field='Freight' headerText='Freight' width='120' format='C2' textAlign='Right'></e-column> 
            <e-column field='ShippedDate' headerText='Shipped Date' width='130' format="yMd" textAlign='Right'></e-column> 
            <e-column field='ShipCountry' headerText='Ship Country' [visible]='false' width='150'></e-column> 
            <e-column field='ShipCity' headerText='Ship City' [visible]='false' width='150'></e-column> 
        </e-columns> 
    </ejs-grid> 
 
[app.component.ts] 
 
export class AppComponent { 
    public data: Object[]; 
    public toolbar: string[]; 
    public pageSettings: Object; 
    public editSettings: Object; 
. . . 
    dataBound(e){ 
      if(this.grid.element.querySelector('#' + this.grid.element.id + '_columnchooser')){ 
        this.grid.element.querySelector('#' + this.grid.element.id + '_columnchooser').remove(); 
        var img = document.createElement('img'); 
        img.src = "https://ej2.syncfusion.com/angular/demos/assets/grid/images/1.png"; 
        this.grid.element.querySelector('.e-cc-toolbar').append(img); 
      } 
    } 
    } 
 
 
[app.component.css] 
  
.e-grid .e-cc-toolbar img{ 
  width: 30px; 
  height: 30px; 
   
} 
 
 
In the above code, we have remove the column chooser button from the toolbar and add the ‘img’ tag. Please find the screenshot for your reference. 
 
 
 
 
 
 
Query 3: So is it possible that I can change the text globally.  
 
Yes, you can change the text of Column chooser from the default by using the following code. 
 
[app.component.html] 
<ejs-grid #grid (load)='load($event)' [dataSource]='data' allowPaging='true' showColumnChooser='true' [toolbar]="toolbar" [pageSettings]='pageSettings'> 
       .  . . 
 
    </ejs-grid> 
 
[app.component.ts] 
export class DataBindingComponent  implements OnInit { 
    public data: Object[]; 
    public toolbar: string[]; 
    public pageSettings: Object; 
    @ViewChild('grid') 
    public grid : GridComponent; 
    load(e){ 
      this.grid.localeObj['localeStrings']['CancelButton']= 'Canceled'; 
      this.grid.localeObj['localeStrings']['OKButton'] = 'Confirm'; 
      this.grid.localeObj['localeStrings']['ChooseColumns'] = 'Select Columns'; 
 
    } 
 
 
In above code, we have changed the text column chooser text by using ‘load’  event  and localeStrings of localObj in Grid. 
 
 
Regards, 
J Mohammed Farook  
 
 



MG Madhur Goyal April 21, 2020 04:07 PM UTC

Hi J Mohammed,

Thanks for the solution second and third query is now resolved.

Regarding first query 

My requirement is to save data in indexeddb. I want to save and load the grid columns state. I should save all columns and their detatils but when i go back to screen I sholud see only choosed columns. So basically I want to save columns with their visibility state and same want to map back when I reload, login or navigate to the screen.

Also, I checked the local storage in chrome it was storing my grid state with key like "gridgrid_1177XXXXX_0, gridgrid_1177XXXXX_1. So basically what is happening when my column chooser choosen columns are different, it creates a new state and save it to local storage which I think may causing the issue of rendering different states which I reported earlier.

I would like to render my latest state but it happens for some time only after that it starts rendering other states which are available in my local storage.

Could you please help me in that.

Thanks & Regards,
Madhur Goyal


AG Ajith Govarthan Syncfusion Team April 22, 2020 01:41 PM UTC

Hi Madhur, 
 
We have already explained the State Persistence feature and browser local storage clearly in our previous update . Please check the previous update as follows. 
 
 
 
By default , the State Persistence feature worked based on browser local storage. If you the run same sample in that same browser(without clear history) , The Grid loaded the settings from the browser local storage. So the Grid sample rendered with your  last changes. But if you run the sample in another browser , the Grid can render in that first time in that browser (local storage is not any information , this is default behavior of browser). So it will be rendered based on your Grid model definition . So this is not possible load the Grid settings from one browser to another browser.  
 
 
 
 
By default if you have run the same sample in different screen or different browser we are unable to get state persistence . this is default behavior.  In the last update we suggest to store the column state inform through actionComplete event when you change the column state. Please find the code example. 
 
 
     
actionComplete(args){  
      if(args.requestType==='columnstate'){  
        this.showColumns = this.grid.getVisibleColumns();  
        this.hideColumns = this.grid.getHiddenColumns();  
      }  
    } 
 
 
You can store the column names and its visibility in your data base.   
 
 
 
 
Once if you have run the same sample in different screen or different browser .  you can change the column visibility based on saved column state from your database through ‘load’ event of Grid 
 
 
load(e){ 
for(var i=0; i< this.grid.columns.length; i++){ 
// check column visibility from your database here. And if your condition is satisfied , then you can set the column visibility as  
 
// column has visible you can set the visibility as  
this.grid.columns[i]. visible = true 
 
// if the column has hidden you can set the visibility as  
 
    this.grid.columns[i]. visible = false 
 
} 
 
} 
 
 
 
Now you can rendered the Grid based on your last settings based on your database saved column information 
 
Please get back to us if you need further assistance. 
 
Regards, 
Ajith G. 
 


Loader.
Up arrow icon