Column chooser in Vue Grid component
26 Mar 202524 minutes to read
The column chooser feature in the Syncfusion Vue 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 ColumnChooser in the provide section
<template>
<div id="app">
<ejs-grid :dataSource="data" :showColumnChooser='true' :toolbar='toolbarOptions' height='272px'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' width='120' textAlign="Right"></e-column>
<e-column field='CustomerID' headerText='Customer Name' width='150'></e-column>
<e-column field='Freight' headerText='Freight' width='120' format='C2' textAlign="Right"></e-column>
<e-column field='ShipCountry' headerText='Ship Country' :visible='true' width='150'></e-column>
<e-column field='ShipCity' headerText='Ship City' :visible='false' width='150'></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script setup>
import { provide } from "vue";
import { GridComponent as EjsGrid, ColumnDirective as EColumn, ColumnsDirective as EColumns, ColumnChooser, Toolbar } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
const toolbarOptions = ['ColumnChooser'];
provide('grid', [ColumnChooser, Toolbar]);
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";
</style>
<template>
<div id="app">
<ejs-grid :dataSource="data" :showColumnChooser='true' :toolbar='toolbarOptions' height='272px'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' width='120' textAlign="Right"></e-column>
<e-column field='CustomerID' headerText='Customer Name' width='150'></e-column>
<e-column field='Freight' headerText='Freight' width='120' format='C2' textAlign="Right"></e-column>
<e-column field='ShipCountry' headerText='Ship Country' :visible='true' width='150'></e-column>
<e-column field='ShipCity' headerText='Ship City' :visible='false' width='150'></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import { GridComponent, ColumnsDirective, ColumnDirective, ColumnChooser, Toolbar } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
export default {
name: "App",
components: {
"ejs-grid": GridComponent,
"e-columns": ColumnsDirective,
"e-column": ColumnDirective
},
data() {
return {
data: data,
toolbarOptions: ['ColumnChooser']
};
},
provide: {
grid: [ColumnChooser, Toolbar]
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";
</style>
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.
<template>
<div id="app">
<ejs-grid :dataSource="data" :showColumnChooser='true' :toolbar='toolbarOptions' height='272px'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' width='120' textAlign="Right"
:showInColumnChooser='false'></e-column>
<e-column field='CustomerID' headerText='Customer Name' width='150'></e-column>
<e-column field='Freight' headerText='Freight' width='120' format='C2' textAlign="Right"></e-column>
<e-column field='ShipCountry' headerText='Ship Country' :visible='true' width='150'></e-column>
<e-column field='ShipCity' headerText='Ship City' :visible='false' width='150'></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script setup>
import { provide } from "vue";
import { GridComponent as EjsGrid, ColumnDirective as EColumn, ColumnsDirective as EColumns, ColumnChooser, Toolbar } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
const toolbarOptions = ['ColumnChooser'];
provide('grid', [ColumnChooser, Toolbar]);
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";
</style>
<template>
<div id="app">
<ejs-grid :dataSource="data" :showColumnChooser='true' :toolbar='toolbarOptions' height='272px'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' width='120' textAlign="Right"
:showInColumnChooser='false'></e-column>
<e-column field='CustomerID' headerText='Customer Name' width='150'></e-column>
<e-column field='Freight' headerText='Freight' width='120' format='C2' textAlign="Right"></e-column>
<e-column field='ShipCountry' headerText='Ship Country' :visible='true' width='150'></e-column>
<e-column field='ShipCity' headerText='Ship City' :visible='false' width='150'></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import { GridComponent, ColumnsDirective, ColumnDirective, ColumnChooser, Toolbar } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
export default {
name: "App",
components: {
"ejs-grid": GridComponent,
"e-columns": ColumnsDirective,
"e-column": ColumnDirective
},
data() {
return {
data: data,
toolbarOptions: ['ColumnChooser']
};
},
provide: {
grid: [ColumnChooser, Toolbar]
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";
</style>
The
columns.showInColumnChooser
property is applied to eachelement individually. By setting it to false, you can hide specific columns from the column chooser dialog.
Open column chooser by externally
The Syncfusion Vue 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:
<template>
<div id="app">
<ejs-button class='e-primary' @click='show' style='margin-bottom:4px;'>open Column Chooser </ejs-button>
<ejs-grid ref='grid' :dataSource="data" :showColumnChooser='true' height='272px'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' width='120' textAlign="Right"></e-column>
<e-column field='CustomerID' headerText='Customer Name' width='150'
:showInColumnChooser='false'></e-column>
<e-column field='OrderDate' headerText='Order Date' width='130' format="yMd" textAlign="Right"
type='date'></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>
</div>
</template>
<script setup>
import { provide, ref } from "vue";
import { GridComponent as EjsGrid, ColumnDirective as EColumn, ColumnsDirective as EColumns, ColumnChooser } from "@syncfusion/ej2-vue-grids";
import { ButtonComponent as EjsButton } from "@syncfusion/ej2-vue-buttons";
import { data } from './datasource.js';
const grid = ref(null);
const show = function () {
grid.value.ej2Instances.columnChooserModule.openColumnChooser(100, 40); // give X and Y axis
}
provide('grid', [ColumnChooser]);
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";
</style>
<template>
<div id="app">
<ejs-button class='e-primary' @click='show' style='margin-bottom:4px;'>open Column Chooser </ejs-button>
<ejs-grid ref='grid' :dataSource="data" :showColumnChooser='true' height='272px'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' width='120' textAlign="Right"></e-column>
<e-column field='CustomerID' headerText='Customer Name' width='150' :showInColumnChooser='false'></e-column>
<e-column field='OrderDate' headerText='Order Date' width='130' format="yMd" textAlign="Right"
type='date'></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>
</div>
</template>
<script>
import { GridComponent, ColumnsDirective, ColumnDirective, ColumnChooser } from "@syncfusion/ej2-vue-grids";
import { ButtonComponent } from "@syncfusion/ej2-vue-buttons";
import { data } from './datasource.js';
export default {
name: "App",
components: {
"ejs-button": ButtonComponent,
"ejs-grid": GridComponent,
"e-columns": ColumnsDirective,
"e-column": ColumnDirective
},
data() {
return {
data: data
};
},
methods: {
show: function () {
this.$refs.grid.ej2Instances.columnChooserModule.openColumnChooser(100, 40); // give X and Y axis
}
},
provide: {
grid: [ColumnChooser]
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";
</style>
Customize column chooser dialog size
The column chooser dialog in Syncfusion Vue 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;
}
<template>
<div id="app">
<ejs-grid :dataSource="data" :showColumnChooser='true' :toolbar='toolbarOptions' height='272px'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' width='120' textAlign="Right"></e-column>
<e-column field='CustomerID' headerText='Customer Name' width='150'></e-column>
<e-column field='Freight' headerText='Freight' width='120' format='C2' textAlign="Right"></e-column>
<e-column field='ShipCountry' headerText='Ship Country' :visible='true' width='150'></e-column>
<e-column field='ShipCity' headerText='Ship City' :visible='false' width='150'></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script setup>
import { provide } from "vue";
import { GridComponent as EjsGrid, ColumnDirective as EColumn, ColumnsDirective as EColumns, ColumnChooser, Toolbar } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
const toolbarOptions = ['ColumnChooser'];
provide('grid', [ColumnChooser, Toolbar]);
</script>
<style>
.e-grid .e-dialog.e-ccdlg {
height: 500px;
width: 200px;
}
.e-grid .e-ccdlg .e-cc-contentdiv {
height: 200px;
width: 230px;
}
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";</style>
<template>
<div id="app">
<ejs-grid :dataSource="data" :showColumnChooser='true' :toolbar='toolbarOptions' height='272px'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' width='120' textAlign="Right"></e-column>
<e-column field='CustomerID' headerText='Customer Name' width='150'></e-column>
<e-column field='Freight' headerText='Freight' width='120' format='C2' textAlign="Right"></e-column>
<e-column field='ShipCountry' headerText='Ship Country' :visible='true' width='150'></e-column>
<e-column field='ShipCity' headerText='Ship City' :visible='false' width='150'></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import { GridComponent, ColumnsDirective, ColumnDirective, ColumnChooser, Toolbar } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
export default {
name: "App",
components: {
"ejs-grid":GridComponent,
"e-columns":ColumnsDirective,
"e-column":ColumnDirective
},
data() {
return {
data: data,
toolbarOptions: ['ColumnChooser']
};
},
provide: {
grid: [ColumnChooser, Toolbar]
}
}
</script>
<style>
.e-grid .e-dialog.e-ccdlg {
height: 500px;
width: 200px;
}
.e-grid .e-ccdlg .e-cc-contentdiv {
height: 200px;
width: 230px;
}
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";
</style>
Change default search operator of the column chooser
The column chooser dialog in the Syncfusion Vue 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 columnChooserSettings.
Here’s an example of how to change the default search operator of the column chooser to contains in the Vue Grid:
<template>
<div id="app">
<ejs-grid :dataSource="data" :showColumnChooser='true' :toolbar='toolbarOptions'
:columnChooserSettings='columnChooserSettings' height='272px'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' width='120' textAlign="Right"></e-column>
<e-column field='CustomerID' headerText='Customer Name' width='150'></e-column>
<e-column field='Freight' headerText='Freight' width='120' format='C2' textAlign="Right"></e-column>
<e-column field='ShipCountry' headerText='Ship Country' :visible='true' width='150'></e-column>
<e-column field='ShipCity' headerText='Ship City' :visible='false' width='150'></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script setup>
import { provide } from "vue";
import { GridComponent as EjsGrid, ColumnDirective as EColumn, ColumnsDirective as EColumns, ColumnChooser, Toolbar } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
const toolbarOptions = ['ColumnChooser'];
const columnChooserSettings = { operator: 'contains' };
provide('grid', [ColumnChooser, Toolbar]);
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";
</style>
<template>
<div id="app">
<ejs-grid :dataSource="data" :showColumnChooser='true' :toolbar='toolbarOptions'
:columnChooserSettings='columnChooserSettings' height='272px'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' width='120' textAlign="Right"></e-column>
<e-column field='CustomerID' headerText='Customer Name' width='150'></e-column>
<e-column field='Freight' headerText='Freight' width='120' format='C2' textAlign="Right"></e-column>
<e-column field='ShipCountry' headerText='Ship Country' :visible='true' width='150'></e-column>
<e-column field='ShipCity' headerText='Ship City' :visible='false' width='150'></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import { GridComponent, ColumnsDirective, ColumnDirective, ColumnChooser, Toolbar } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
export default {
name: "App",
components: {
"ejs-grid": GridComponent,
"e-columns": ColumnsDirective,
"e-column": ColumnDirective
},
data() {
return {
data: data,
toolbarOptions: ['ColumnChooser'],
columnChooserSettings: { operator: 'contains' }
};
},
provide: {
grid: [ColumnChooser, Toolbar]
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";
</style>
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 columnchoosersettings.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:
<template>
<div id="app">
<ejs-grid :dataSource="data" :showColumnChooser='true' :toolbar='toolbarOptions'
:columnChooserSettings='columnChooserSettings' height='272px'>
<e-columns>
<e-column field='ÒrderID̂' headerText='Òrder ID̂' width='120' textAlign="Right"></e-column>
<e-column field='CustomerID' headerText='Customer Name' width='150'></e-column>
<e-column field='F̂reight' headerText='F̂reight' width='120' format='C2' textAlign="Right"></e-column>
<e-column field='OrderDate' headerText='Order Date' type='date' format='yMd' width='150'></e-column>
<e-column field='ShipCountry' headerText='Ship Country' :visible='true' width='150'></e-column>
<e-column field='ShipCity' headerText='Ship City' :visible='false' width='150'></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script setup>
import { provide } from "vue";
import { GridComponent as EjsGrid, ColumnDirective as EColumn, ColumnsDirective as EColumns, ColumnChooser, Toolbar } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
const toolbarOptions = ['ColumnChooser'];
const columnChooserSettings = { ignoreAccent: true };
provide('grid', [ColumnChooser, Toolbar]);
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";
</style>
<template>
<div id="app">
<ejs-grid :dataSource="data" :showColumnChooser='true' :toolbar='toolbarOptions'
:columnChooserSettings='columnChooserSettings' height='272px'>
<e-columns>
<e-column field='ÒrderID̂' headerText='Òrder ID̂' width='120' textAlign="Right"></e-column>
<e-column field='CustomerID' headerText='Customer Name' width='150'></e-column>
<e-column field='F̂reight' headerText='F̂reight' width='120' format='C2' textAlign="Right"></e-column>
<e-column field='OrderDate' headerText='Order Date' type='date' format='yMd' width='150'></e-column>
<e-column field='ShipCountry' headerText='Ship Country' :visible='true' width='150'></e-column>
<e-column field='ShipCity' headerText='Ship City' :visible='false' width='150'></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import { GridComponent, ColumnsDirective, ColumnDirective, ColumnChooser, Toolbar } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js';
export default {
name: "App",
components: {
"ejs-grid": GridComponent,
"e-columns": ColumnsDirective,
"e-column": ColumnDirective
},
data() {
return {
data: data,
toolbarOptions: ['ColumnChooser'],
columnChooserSettings: { ignoreAccent: true }
};
},
provide: {
grid: [ColumnChooser, Toolbar]
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";
</style>
Column Chooser Template in Syncfusion Vue Grid
The Column Chooser Template feature allows full customization of the column chooser’s header, content, and footer, making it easier to manage column visibility. To enable the column chooser, set showColumnChooser to true and add ColumnChooser to the toolbar property.
To implement a custom column chooser template in the Grid, use the following properties:
-
columnChooserSettings.headerTemplate - Defines the header template of the column chooser.
-
columnChooserSettings.template- Defines the content template.
-
columnChooserSettings.footerTemplate - Defines the footer template.
In this example, a Syncfusion TreeView component is rendered inside the column chooser. To use the TreeView component, install the Syncfusion TreeView package as described in the documentation. The columnChooserSettings.template
property is assigned a template using the v-slot directive to render the TreeView component with checkboxes for selecting columns.Checkbox selection is handled using the nodeClicked and keyPress events, which organize columns into Order Details, Shipping Details, and Delivery Status.
The column chooser footer is customized using columnChooserSettings.footerTemplate
property, which is assigned a template via the v-slot directive, replacing the default buttons with customized Apply and Close buttons. The Apply button updates column visibility based on selection, while the Close button closes the column chooser via the onClick
event. Additionally, the header is customized using columnChooserSettings.headerTemplate
property, which is assigned a template via the v-slot directive to include a title and an icon.
<template>
<div id='app'>
<div>
<ejs-grid ref='gridInstance' id='gridInstance' :dataSource="data" :allowPaging='true' :showColumnChooser='true' :columnChooserSettings='columnChooserSettings' :toolbar='toolbar'>
<e-columns>
<e-column field='CustomerID' headerText='Customer ID' width='160' textAlign='Right' :showInColumnChooser="false" :isPrimaryKey='true'></e-column>
<e-column field='CustomerName' headerText='Name' width='100' minWidth='100'></e-column>
<e-column headerText='Order Details' :columns='orderColumns' textAlign='Center'></e-column>
<e-column headerText='Shipping Details' :columns='shipColumns' textAlign='Center'></e-column>
<e-column headerText='Delivery Status' :columns='deliveryColumns' textAlign='Center'></e-column>
</e-columns>
<template v-slot:template="{data}">
<div v-if="data && data.columns && data.columns.length">
<ejs-treeview id='treeview' :fields='dataProcess(data)' cssClass="no-border" :enableRtl="enableRTL" :showCheckBox='true' :keyPress='nodeCheck' :nodeClicked='nodeCheck'></ejs-treeview>
</div>
<div v-else class="no-record-text">No Matches Found</div>
</template>
<template v-slot:headerTemplate>
<div>
<span class="e-icons e-columns" id="column-chooser-icon"></span>
<span id="column-chooser-text">Column Options</span>
</div>
</template>
<template v-slot:footerTemplate>
<ejs-button v-on:click='columnChooserSubmit'>Apply</ejs-button>
<ejs-button v-on:click='columnChooserClose'>Close</ejs-button>
</template>
</ejs-grid>
</div>
</div>
</template>
<script setup>
import { ref } from "vue";
import { provide,onMounted } from "vue";
import { GridComponent as EjsGrid, ColumnDirective as EColumn, ColumnsDirective as EColumns,ColumnChooser, Toolbar,Page } from "@syncfusion/ej2-vue-grids";
import { TreeViewComponent as EjsTreeview } from "@syncfusion/ej2-vue-navigations";
import { ButtonComponent as EjsButton } from "@syncfusion/ej2-vue-buttons";
import stackedHeaderData from './datasource';
const gridInstance = ref(null);
const enableRTL = ref(false);
const data = stackedHeaderData;
const toolbar = ["ColumnChooser"];
const columnChooserSettings = {
enableSearching: true,
template: "template",
headerTemplate: "headerTemplate",
footerTemplate: "footerTemplate",
};
const orderColumns = [
{ field: "OrderID", headerText: "ID", textAlign: "Right", width: 90 },
{ field: "OrderDate", headerText: "Date", textAlign: "Right", width: 110,format: "yMd"},
];
const shipColumns = [
{ field: "ShipCountry", headerText: "Country", textAlign: "Left", width: 115,},
{ field: "Freight", headerText: "Charges",textAlign: "Right",width: 130,format: "C2"},
];
const deliveryColumns = [
{ field: "Status", headerText: "Status", textAlign: "Center", width: 110 },
];
const dataProcess = (args) => {
const parentNodes = [
{ id: 1, name: "Order Details", hasChild: true, expanded: true },
{ id: 2, name: "Shipping Details", hasChild: true, expanded: true },
{ id: 3, name: "Delivery Status", hasChild: true, expanded: true },
];
let treeData = [];
if (args.columns && args.columns.length) {
treeData = args.columns.map((column) => {
let parentId;
switch (column.field) {
case "OrderID":
case "OrderDate":
parentId = 1;
break;
case "ShipCountry":
case "Freight":
parentId = 2;
break;
case "Status":
parentId = 3;
break;
default:
break;
}
return {
id: column.uid,
name: column.headerText,
pid: parentId,
isChecked: column.visible,
};
});
const uniquePids = [];
treeData.forEach((item) => {
if (uniquePids.indexOf(item.pid) === -1) {
uniquePids.push(item.pid);
}
});
const filteredParents = parentNodes.filter(
(parent) => uniquePids.indexOf(parent.id) !== -1
);
treeData.push(...filteredParents);
} else {
treeData = [];
}
const fields = {
dataSource: treeData,
id: "id",
parentID: "pid",
text: "name",
hasChildren: "hasChild",
};
return fields;
};
const nodeCheck = (args) => {
let checkedNode = [args.node];
var treeObj = document.getElementById("treeview").ej2_instances[0];
if (
args.event.target.classList.contains("e-fullrow") ||
args.event.key == "Enter"
) {
let getNodeDetails = treeObj.getNode(args.node);
if (getNodeDetails.isChecked == "true") {
treeObj.uncheckAll(checkedNode);
} else {
treeObj.checkAll(checkedNode);
}
}
};
const columnChooserClose = () => {
gridInstance.value.ej2Instances.columnChooserModule.hideDialog();
};
const columnChooserSubmit = () => {
const checkedElements = [];
const uncheckedElements = [];
const treeObj = document.getElementById("treeview").ej2_instances[0];
document.querySelectorAll(".e-list-item").forEach((item) => {
const itemDetails = treeObj.getNode(item);
if (!itemDetails.hasChildren) {
if (item.getAttribute("aria-checked") === "true") {
checkedElements.push(itemDetails.text);
} else {
uncheckedElements.push(itemDetails.text);
}
}
});
const showColumns = gridInstance.value
.getVisibleColumns()
.filter((col) => col.showInColumnChooser)
.map((col) => col.headerText)
.filter((col) => !uncheckedElements.includes(col));
checkedElements.forEach((item) => {
if (!showColumns.includes(item)) {
showColumns.push(item);
}
});
gridInstance.value.ej2Instances.columnChooserModule.changeColumnVisibility({
visibleColumns: showColumns,
hiddenColumns: uncheckedElements,
});
};
onMounted(() => {
if (gridInstance.value?.ej2Instances) {
enableRTL.value = gridInstance.value.ej2Instances.enableRtl;
}
});
provide("grid", [ColumnChooser, Toolbar, Page]);
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";
</style>
<template>
<div id='app'>
<div>
<ejs-grid ref='gridInstance' id='gridInstance' :dataSource="data" :allowPaging='true' :showColumnChooser='true' :columnChooserSettings='columnChooserSettings' :toolbar='toolbar'>
<e-columns>
<e-column field='CustomerID' headerText='Customer ID' width='160' textAlign='Right' :showInColumnChooser="false" :isPrimaryKey='true'></e-column>
<e-column field='CustomerName' headerText='Name' width='100' minWidth='100'></e-column>
<e-column headerText='Order Details' :columns='orderColumns' textAlign='Center'></e-column>
<e-column headerText='Shipping Details' :columns='shipColumns' textAlign='Center'></e-column>
<e-column headerText='Delivery Status' :columns='deliveryColumns' textAlign='Center'></e-column>
</e-columns>
<template v-slot:template="{data}">
<div v-if="data && data.columns && data.columns.length">
<ejs-treeview id='treeview' :fields='dataProcess(data)' cssClass="no-border" :enableRtl="enableRTL" :showCheckBox='true' :keyPress='nodeCheck' :nodeClicked='nodeCheck'></ejs-treeview>
</div>
<div v-else class="no-record-text">No Matches Found</div>
</template>
<template v-slot:headerTemplate>
<div>
<span class="e-icons e-columns" id="column-chooser-icon"></span>
<span id="column-chooser-text">Column Options</span>
</div>
</template>
<template v-slot:footerTemplate>
<ejs-button v-on:click='columnChooserSubmit'>Apply</ejs-button>
<ejs-button v-on:click='columnChooserClose'>Close</ejs-button>
</template>
</ejs-grid>
</div>
</div>
</template>
<script>
import { TreeViewComponent } from "@syncfusion/ej2-vue-navigations";
import { ButtonComponent } from '@syncfusion/ej2-vue-buttons';
import { GridComponent, ColumnDirective, ColumnsDirective, Page, Toolbar, ColumnChooser} from '@syncfusion/ej2-vue-grids';
import { stackedHeaderData } from './datasource';
export default {
components: {
'ejs-grid': GridComponent,
'e-column': ColumnDirective,
'e-columns': ColumnsDirective,
'ejs-button': ButtonComponent,
"ejs-treeview": TreeViewComponent,
},
data: () => {
return {
data: stackedHeaderData,
toolbar: ['ColumnChooser'],
columnChooserSettings: { enableSearching: true, template: 'template', headerTemplate: 'headerTemplate', footerTemplate: 'footerTemplate' },
enableRTL: false,
orderColumns: [
{ field: 'OrderID', headerText: 'ID', textAlign: 'Right', width: 90, minWidth: 90 },
{ field: 'OrderDate', headerText: 'Date', textAlign: 'Right', width: 110, minWidth: 100, format: 'yMd' }
],
shipColumns: [
{ field: 'ShipCountry', headerText: 'Country', textAlign: 'Left', width: 115 },
{ field: 'Freight', headerText: 'Charges', textAlign: 'Right', width: 130 },
],
deliveryColumns: [
{ field: 'Status', headerText: 'Status', textAlign: 'Center', width: 110 }
],
}
},
methods: {
nodeCheck: function (args) {
let checkedNode = [args.node];
var treeObj = document.getElementById('treeview').ej2_instances[0];
if (args.event.target.classList.contains('e-fullrow') || args.event.key == "Enter") {
let getNodeDetails = treeObj.getNode(args.node);
if (getNodeDetails.isChecked == 'true') {
treeObj.uncheckAll(checkedNode);
} else {
treeObj.checkAll(checkedNode);
}
}
},
dataProcess: function (args) {
const parentNodes = [
{ id: 1, name: 'Order Details', hasChild: true, expanded: true },
{ id: 2, name: 'Shipping Details', hasChild: true, expanded: true },
{ id: 3, name: 'Delivery Status', hasChild: true, expanded: true },
];
let treeData = [];
if (args.columns && args.columns.length) {
treeData = args.columns.map((column) => {
let parentId;
switch (column.field) {
case 'OrderID':
case 'OrderDate':
parentId = 1;
break;
case 'ShipCountry':
case 'Freight':
parentId = 2;
break;
case 'Status':
parentId = 3;
break;
default :
break;
}
return {
id: column.uid,
name: column.headerText,
pid: parentId,
isChecked: column.visible
};
});
const uniquePids = [];
treeData.forEach((item) => {
if (uniquePids.indexOf(item.pid) === -1) {
uniquePids.push(item.pid);
}
});
const filteredParents = parentNodes.filter((parent) => uniquePids.indexOf(parent.id) !== -1);
treeData.push(...filteredParents);
} else {
treeData = [];
}
const fields = { dataSource: treeData, id: 'id', parentID: 'pid', text: 'name', hasChildren: 'hasChild' };
return fields;
},
columnChooserClose: function() {
var gridInstance = document.getElementById('gridInstance').ej2_instances[0];
gridInstance.columnChooserModule.hideDialog();
},
columnChooserSubmit: function (){
const checkedElements = [];
const uncheckedElements = [];
var gridInstance = document.getElementById('gridInstance').ej2_instances[0];
var treeObj = document.getElementById('treeview').ej2_instances[0];
var showColumns = gridInstance.getVisibleColumns().filter(function (column) { return (column.showInColumnChooser === true); });
showColumns = showColumns.map(function (col) { return col.headerText; });
const treeItems = document.querySelectorAll('.e-list-item');
treeItems.forEach(item => {
const itemDetails = treeObj.getNode(item);
if (!itemDetails.hasChildren) {
if (item.getAttribute('aria-checked') === 'true') {
checkedElements.push(itemDetails.text);
} else {
uncheckedElements.push(itemDetails.text);
}
}
});
showColumns = showColumns.filter((col) => uncheckedElements.indexOf(col) === -1);
checkedElements.forEach(item => {
if (!showColumns.includes(item)) {
showColumns.push(item);
}
});
var columnsToUpdate = { visibleColumns: showColumns, hiddenColumns: uncheckedElements };
gridInstance.columnChooserModule.changeColumnVisibility(columnsToUpdate);
},
},
computed: {
enableRTL() {
const gridInstance = document.getElementById('gridInstance').ej2_instances[0];
return gridInstance.enableRtl;
}
},
provide: {
grid: [Page, Toolbar, ColumnChooser]
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";
</style>
Enable and disable search option
By default, the search option in the column chooser allows filtering specific columns from the Grid’s column list.
The search option is enabled by default in the column chooser. However, you can disable it by setting the columnChooserSettings.enableSearching
property to false.
The following example demonstrates how to enable or disable the search option dynamically using a Switch and its change event in the Grid.
<template>
<div id="app">
<div style="padding: 20px 0px 20px 0px;display:flex">
<label style="padding: 0px 20px 0px 0px;font-weight: bold">Enable and disable search option</label>
<ejs-switch ref="switch" id="switch" :checked="true" :change="change"></ejs-switch>
</div>
<ejs-grid :dataSource="data" :showColumnChooser='true' :toolbar='toolbarOptions' height='272px'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' width='120' textAlign="Right"></e-column>
<e-column field='CustomerID' headerText='Customer Name' width='150'></e-column>
<e-column field='Freight' headerText='Freight' width='120' format='C2' textAlign="Right"></e-column>
<e-column field='ShipCountry' headerText='Ship Country' :visible='true' width='150'></e-column>
<e-column field='ShipCity' headerText='Ship City' :visible='false' width='150'></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script setup>
import { provide } from "vue";
import { ref } from "vue";
import { GridComponent as EjsGrid, ColumnDirective as EColumn, ColumnsDirective as EColumns, ColumnChooser, Toolbar } from "@syncfusion/ej2-vue-grids";
import { SwitchComponent as EjsSwitch } from "@syncfusion/ej2-vue-buttons";
import { data } from './datasource.js';
const toolbarOptions = ['ColumnChooser'];
const grid = ref(null);
const change = function(args) {
if (grid.value && grid.value.ej2Instances) {
grid.value.ej2Instances.columnChooserSettings.enableSearching = args.checked;
}
}
provide('grid', [ColumnChooser, Toolbar]);
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";
</style>
<template>
<div id="app">
<div style="padding: 20px 0px 20px 0px;display:flex">
<label style="padding: 0px 20px 0px 0px;font-weight: bold">Enable and disable search option</label>
<ejs-switch ref="switch" id="switch" :checked="true" :change="change"></ejs-switch>
</div>
<ejs-grid :dataSource="data" :showColumnChooser='true' :toolbar='toolbarOptions' height='272px'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' width='120' textAlign="Right"></e-column>
<e-column field='CustomerID' headerText='Customer Name' width='150'></e-column>
<e-column field='Freight' headerText='Freight' width='120' format='C2' textAlign="Right"></e-column>
<e-column field='ShipCountry' headerText='Ship Country' :visible='true' width='150'></e-column>
<e-column field='ShipCity' headerText='Ship City' :visible='false' width='150'></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import { GridComponent, ColumnsDirective, ColumnDirective, ColumnChooser, Toolbar } from "@syncfusion/ej2-vue-grids";
import { SwitchComponent } from "@syncfusion/ej2-vue-buttons";
import { data } from './datasource.js';
export default {
name: "App",
components: {
"ejs-grid": GridComponent,
"e-columns": ColumnsDirective,
"e-column": ColumnDirective,
"ejs-switch": SwitchComponent
},
data() {
return {
data: data,
toolbarOptions: ['ColumnChooser']
};
},
methods: {
change(args){
this.$refs.grid.ej2Instances.columnChooserSettings.enableSearching = args.checked;
}
},
provide: {
grid: [ColumnChooser, Toolbar]
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-calendars/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind.css";
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/tailwind.css";
</style>