Column chooser in Angular Grid component
13 Jun 202418 minutes to read
The column chooser feature in the Syncfusion Angular Grid component allows you to dynamically show or hide columns. This feature can be enabled by defining the showColumnChooser property as true.
To use the column chooser, you need to inject the ColumnChooserService in the provider section of AppModule.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { GridModule, ToolbarService, ColumnChooserService } from '@syncfusion/ej2-angular-grids'
import { Component, OnInit } from '@angular/core';
import { data } from './datasource';
import { ToolbarItems } from '@syncfusion/ej2-angular-grids';
@Component({
imports: [
GridModule
],
providers: [ToolbarService, ColumnChooserService],
standalone: true,
selector: 'app-root',
template: `<ejs-grid [dataSource]='data' [toolbar]='toolbarOptions' height='272px' [showColumnChooser]= 'true'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' width='120' textAlign="Right"></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='ShipCountry' headerText='Ship Country' width='150'></e-column>
<e-column field='ShipCity' headerText='Ship City' [visible]='false' width='150'></e-column>
</e-columns>
</ejs-grid>`
})
export class AppComponent implements OnInit {
public data?: object[];
public toolbarOptions?: ToolbarItems[];
ngOnInit(): void {
this.data = data;
this.toolbarOptions = ['ColumnChooser'];
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
The column chooser dialog displays the header text of each column by default. If the header text is not defined for a column, the corresponding column field name is displayed instead.
Hide column in column chooser dialog
You can hide the column names in column chooser by defining the columns.showInColumnChooser as false. This feature is useful when working with a large number of columns or when you want to limit the number of columns that are available for selection in the column chooser dialog.
In this example, the columns.showInColumnChooser
property is set to false for the Order ID column. As a result, the Order ID column will not be displayed in the column chooser dialog.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { GridModule, ToolbarService, ColumnChooserService } from '@syncfusion/ej2-angular-grids'
import { Component, OnInit } from '@angular/core';
import { data } from './datasource';
import { ToolbarItems } from '@syncfusion/ej2-angular-grids';
@Component({
imports: [
GridModule
],
providers: [ToolbarService, ColumnChooserService],
standalone: true,
selector: 'app-root',
template: `<ejs-grid [dataSource]='data' [toolbar]='toolbarOptions' height='272px' [showColumnChooser]= 'true'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' width='120' textAlign="Right" [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='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>`
})
export class AppComponent implements OnInit {
public data?: object[];
public toolbarOptions?: ToolbarItems[];
ngOnInit(): void {
this.data = data;
this.toolbarOptions = ['ColumnChooser'];
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
- The
columns.showInColumnChooser
property is applied to eachelement individually. By setting it to false, you can hide specific columns from the column chooser dialog. - To work with showing and hiding columns, it is necessary to have at least one column of the grid in a visible state
Open column chooser by externally
The Syncfusion Angular Grid provides the flexibility to open the column chooser dialog on a web page using an external button. By default, the column chooser button is displayed in the right corner of the grid component, and clicking the button opens the column chooser dialog below it. However, you can programmatically open the column chooser dialog at specific X and Y axis positions by using the openColumnChooser method.
Here’s an example of how to open the column chooser in the Grid using an external button:
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { GridModule, ToolbarService, ColumnChooserService } from '@syncfusion/ej2-angular-grids'
import { Component, OnInit, ViewChild } from '@angular/core';
import { data } from './datasource';
import { GridComponent } from '@syncfusion/ej2-angular-grids';
@Component({
imports: [
GridModule
],
providers: [ToolbarService, ColumnChooserService],
standalone: true,
selector: 'app-root',
template: ` <button id='show' ejs-button class='e-primary' (click)='show()'> open Column Chooser </button>
<ejs-grid #grid [dataSource]='data' [height]='280' [showColumnChooser]= 'true'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' width='120' textAlign="Right" [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='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>`
})
export class AppComponent implements OnInit {
public data?: Object[];
@ViewChild('grid')
public grid?: GridComponent;
ngOnInit(): void {
this.data = data;
}
show() {
this.grid?.columnChooserModule.openColumnChooser(100, 40); // give X and Y axis
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Customize column chooser dialog size
The column chooser dialog in Syncfusion Angular Grid comes with default size, but you can modify its height and width as per your specific needs using CSS styles.
To customize the column chooser dialog size, you can use the following CSS styles:
.e-grid .e-dialog.e-ccdlg {
height: 500px;
width: 200px;
}
.e-grid .e-ccdlg .e-cc-contentdiv {
height: 200px;
width: 230px;
}
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { GridModule, ToolbarService, ColumnChooserService } from '@syncfusion/ej2-angular-grids'
import { Component, OnInit } from '@angular/core';
import { data } from './datasource';
import { ToolbarItems } from '@syncfusion/ej2-angular-grids';
@Component({
imports: [
GridModule
],
providers: [ToolbarService, ColumnChooserService],
standalone: true,
selector: 'app-root',
template: `<ejs-grid id='grid' [dataSource]='data' [toolbar]='toolbarOptions' height='272px' [showColumnChooser]= 'true'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' width='120' textAlign="Right" [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='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>`,
})
export class AppComponent implements OnInit {
public data?: object[];
public toolbarOptions?: ToolbarItems[];
ngOnInit(): void {
this.data = data;
this.toolbarOptions = ['ColumnChooser'];
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Change default search operator of the column chooser
The column chooser dialog in the Syncfusion Angular Grid provides a search box that allows you to search for column names. By default, the search functionality uses the “startsWith” operator to match columns and display the results in the column chooser dialog. However, there might be cases where you need to change the default search operator to achieve more precise data matching.
To change the default search operator of the column chooser in Syncfusion Grid, you need to use the operator property of the column chooser.
Here’s an example of how to change the default search operator of the column chooser to contains in the Angular Grid:
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { GridModule, ToolbarService, ColumnChooserService } from '@syncfusion/ej2-angular-grids'
import { Component, OnInit, ViewChild } from '@angular/core';
import { data } from './datasource';
import { ToolbarItems, GridComponent, ColumnChooserSettingsModel } from '@syncfusion/ej2-angular-grids';
@Component({
imports: [
GridModule
],
providers: [ToolbarService, ColumnChooserService],
standalone: true,
selector: 'app-root',
template: `<ejs-grid [dataSource]='data' [toolbar]='toolbarOptions' height='272px' [showColumnChooser]= 'true' [columnChooserSettings]='columnChooserSettings'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' width='120' textAlign="Right"></e-column>
<e-column field='OrderDate' headerText='Order Date' width='120' format="yMd" textAlign="Right"></e-column>
<e-column field='Freight' headerText='Freight' width='120' format='C2' textAlign="Right"></e-column>
<e-column field='ShipCountry' headerText='Ship Country' width='130'></e-column>
<e-column field='ShipCity' headerText='Ship City' [visible]='false' width='130'></e-column>
</e-columns>
</ejs-grid>`
})
export class AppComponent implements OnInit {
@ViewChild('grid') public grid?: GridComponent;
public data?: object[];
public toolbarOptions?: ToolbarItems[];
public columnChooserSettings?:ColumnChooserSettingsModel;
ngOnInit(): void {
this.data = data;
this.toolbarOptions = ['ColumnChooser'];
this.columnChooserSettings = { operator: 'contains' };
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Diacritics searching in column chooser
By default, the grid ignores diacritic characters when performing a search in the column chooser. However, in some cases, you may want to include diacritic characters in the search. To enable this behavior, you can set the ignoreAccent property to true.
Here is an example that demonstrates the usage of the ignoreAccent
property to include diacritic characters for searching in the column chooser:
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { GridModule, ToolbarService, ColumnChooserService } from '@syncfusion/ej2-angular-grids'
import { Component, OnInit, ViewChild } from '@angular/core';
import { data } from './datasource';
import { ToolbarItems, GridComponent, ColumnChooserSettingsModel } from '@syncfusion/ej2-angular-grids';
@Component({
imports: [
GridModule
],
providers: [ToolbarService, ColumnChooserService],
standalone: true,
selector: 'app-root',
template: `<ejs-grid [dataSource]='data' [toolbar]='toolbarOptions' height='272px' [showColumnChooser]= 'true' [columnChooserSettings]='columnChooserSettings'>
<e-columns>
<e-column field='ÒrderID̂' headerText='Òrder ID̂' width='120' textAlign="Right"></e-column>
<e-column field='OrderDate' headerText='Order Date' width='120' format="yMd" textAlign="Right"></e-column>
<e-column field='F̂reight' headerText='F̂reight' width='120' format='C2' textAlign="Right"></e-column>
<e-column field='ShipCountry' headerText='Ship Country' width='130'></e-column>
<e-column field='ShipCity' headerText='Ship City' [visible]='false' width='130'></e-column>
</e-columns>
</ejs-grid>`
})
export class AppComponent implements OnInit {
@ViewChild('grid') public grid?: GridComponent;
public data?: object[];
public toolbarOptions?: ToolbarItems[];
public columnChooserSettings?:ColumnChooserSettingsModel;
ngOnInit(): void {
this.data = data;
this.toolbarOptions = ['ColumnChooser'];
this.columnChooserSettings = { ignoreAccent: true };
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));