Show grid column based on User Preferences
Question
I am using igx-tree grid in angular 7. while loading grid i want to show the grid columns based on user preferences.
is there a way to show column based on user preference.
- igniteui-angular version: 7.3.2
- browser: all browser
@astaev is there a way to show columns based on user preference?
@prabakaran1311, yes. The IgxTreeGrid supports column hiding functionality. Here is our sample - https://www.infragistics.com/products/ignite-ui-angular/angular/components/treegrid/column_hiding.html. Also you can control each column's visibility via Hidden property, which can also be bind as well.
@astaev i am binding remote datas to the grid in component.html file. also grid has remote features like sorting filtering and paging. I want to separately call another api like userpreference and bind that column in Grid. Is there any example or sample available.
No, there isn't such an API, you need to implement it on your own.
@prabakaran1311 I would suggest to call your API in your client side service and to augment the collection that you've already bound the grid to with the results of the call. If the user preferences data is a separate collection from the collection you're binding the grid to, then you wouldn't be able to pass it to the grid alongside your original data. You will need to combine both into a single collection.
@kdinev do u want me to add separate objects collections for user preference in the same grid data service. Can you give me some sample... thanks
@prabakaran1311 I didn't read your question correctly the first time. I understand that you want to load a different column collection depending on the user choice. Here's an example of how you can do save/restore of columns.
https://www.infragistics.com/products/ignite-ui-angular/angular/components/grid/grid.html#state-persistence
The example is based on localStorage, but exactly the same logic will apply if your user preferences are saved by your service somewhere.
@kdinev above example shows like grid attributes are created in component.ts file and its looped inside component.html for each column. But i have created grid columns in component.html. Because i have used custom template for header and some of columns. is there a way to implement with component.html file
@prabakaran1311 How are you saving the user preferences and where do you get them from? They cannot be available in the template, unless you pass them in a property in your component. You can still have templates for the headers based on a condition coming from your user preferences column definitions.
@kdinev really i want to use the template because i am showing images in header and also showing images in grid cells. Also i am using custom filtering template. so i have to integrate the user preference in same grid. or else is there a way to show images using property in component?
@prabakaran1311 What I'm saying is you still can have conditional templates, even when your columns are objects coming from the component, like this:
public ngOnInit() {
const columnsFromState = this.state.getColumnsForGrid(this.gridId);
this.columns = this.state.columns && columnsFromState ?
columnsFromState : this.initialColumns;
}
<igx-column *ngFor="let c of columns" [sortable]="true" [movable]="true" [filterable]="true"
[field]="c.field" [header]="c.header" [dataType]="c.dataType" [pinned]="c.pinned" [hidden]="c.hidden">
<ng-template *ngIf="c.certainUserPreferenceFlag" igxCellEditor let-cell>
<!-- custom editor template that would be rendered only if certainUserPreferenceFlag is present -->
</ng-template>
<ng-template *ngIf="c.otherUserPreferenceFlag" igxHeader let-column>
<!-- custom header template that would be rendered only if otherUserPreferenceFlag is present -->
</ng-template>
</igx-column>
@kdinev thanks for your solution one more question i am using custom filtering template how to include custom template in grid. I have attached my sample grid with all the funtions like custom filtering template, custom column, custom cell templates i have used.

Same way as in the example above, with an additional igxFilterCellTemplate, which is conditionally rendered.
@kdinev i am getting below error while implementing state-persistence below is the screen shot.
In igxstate directive grid id is not calling it shows null value
@prabakaran1311 I would not be able to tell you where the problem is, based on the screenshot alone. Inspect the first exception that you get, as the following are probably a result of the first one.
@kdinev thanks for your reply. Actually grid objects are not capturing in directive while i console.log(this.grid) it shows as null so grid id also shows null. it causes the issue i have updated the screenshot.
The problem is you're trying to load the state onInit. Move the login in ngAfterViewInit.
@kdinev sorry its my mistake i am using igxtreegrid instead of i am calling igxgridcomponent method thats why grid obects are not loaded. thanks for your help
@kdinev i need to reset the grid to its original state(if user already set any preference) while user wants to reset the grid to original initial stage on button click. i tried to load the initial values to grid but its not resetting the grid. Is there a way to reload the grid without reload the whole page.
@prabakaran1311 Setting the initial state should be exactly the same as setting a user preferences state. Can you show an example of how you reset the grid state?