Filter bar in Vue Grid component
29 Mar 20239 minutes to read
By defining the allowFiltering
to true, then filter bar row will be rendered next to header which allows you to filter data. You can filter the records with different expressions depending upon the column type.
Filter bar Expressions:
You can enter the following filter expressions(operators) manually in the filter bar.
Expression | Example | Description | Column Type |
---|---|---|---|
= | =value | equal | Number |
!= | !=value | notequal | Number |
> | >value | greaterthan | Number |
< | <value | lessthan | Number |
>= | >=value | greaterthanorequal | Number |
<= | <=value | lessthanorequal | Number |
* | *value | startswith | String |
% | %value | endswith | String |
N/A | N/A | Always equal operator will be used for Date filter | Date |
N/A | N/A | Always equal operator will be used for Boolean filter | Boolean |
<template>
<div id="app">
<ejs-grid :dataSource='data' :allowFiltering='true' height='273px'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' width=100></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=120></e-column>
<e-column field='ShipCity' headerText='Ship City' width=100></e-column>
<e-column field='ShipName' headerText='Ship Name' width=100></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import Vue from "vue";
import { GridPlugin, Filter } from "@syncfusion/ej2-vue-grids";
import { data } from './datasource.js'
Vue.use(GridPlugin);
export default {
data() {
return {
data: data
};
},
provide: {
grid: [Filter]
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/material.css";
</style>
Filter bar template with custom component
The filterBarTemplate
is used to add a custom component for a particular column and this contains the following functions.
- create – It is used for creating custom components.
- write - It is used to wire events for custom components.
In the following sample dropdown is used as custom component in EmployeeID column.
<template>
<div id="app">
<ejs-grid ref='grid' :dataSource='data' :allowFiltering='true' height='273px'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' width=100></e-column>
<e-column field='EmployeeID' headerText='Employee ID' width=120 :filterBarTemplate='templateOptions'></e-column>
<e-column field='ShipCity' headerText='Ship City' width=100></e-column>
<e-column field='ShipName' headerText='Ship Name' width=100></e-column>
</e-columns>
</ejs-grid>
</div>
</template>
<script>
import Vue from "vue";
import { GridPlugin, Filter } from "@syncfusion/ej2-vue-grids";
import { DropDownList } from '@syncfusion/ej2-dropdowns';
import { data } from './datasource.js'
Vue.use(GridPlugin);
export default {
data() {
return {
data: data,
templateOptions: {
create: function (args) {
const dd = document.createElement('input');
dd.id = 'EmployeeID';
return dd;
},
write: function (args) {
let DropDownListObj: DropDownList = new DropDownList({
dataSource: ['All','1','3','4','5','6','8','9'],
fields: { text: 'EmployeeID', value: 'EmployeeID' },
placeholder: 'Select a value',
popupHeight: '200px',
change: function(e) {
var gridObj = (document.getElementsByClassName('e-grid')[0] as any).ej2_instances[0];
if(e.value =='All') {
gridObj.removeFilteredColsByField('EmployeeID');
} else {
gridObj.filterByColumn('EmployeeID','equal',parseInt(e.value as any));
}
}
});
DropDownListObj.appendTo('#EmployeeID');
}
}
};
},
provide: {
grid: [Filter]
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-grids/styles/material.css";
</style>