Vue UI Components Documentation
To enable the filtering feature, you need to set the 'filterable' property to true. When the value of the filterable property is true, the Grid displays a filtering panel in the columns popup menus. jqxGrid has several methods that allow you to handle the data filtering – addfilter, removefilter, applyfilters and clearfilters. The first method adds a filter to a grid column. The 'removefilter' method removes a filter from a grid column. The 'applyfilters' method applies all filters to the grid and refreshes its contents. The last method clears the filtering.
Let's see how to add a filter:
let filtergroup = new jqx.filter();2. The next step is to create the filters. Each filter must have a filter value – this is the value we compare each cell value with. The filter condition specifies how the filter will compare each cell value with the filter value. The filter condition value depends on the filter's type (jqxGrid supports string, numeric and date filters). If you want to get the list of the supported grid filtering conditions, you can use the 'getoperatorsbyfiltertype' method of filter group.The createfilter method is used to create the filter.
let filtervalue = 'Beate';
let filtercondition = 'contains';
// possible conditions for string filter: 'EMPTY', 'NOT_EMPTY', 'CONTAINS', 'CONTAINS_CASE_SENSITIVE',
// 'DOES_NOT_CONTAIN', 'DOES_NOT_CONTAIN_CASE_SENSITIVE', 'STARTS_WITH', 'STARTS_WITH_CASE_SENSITIVE',
// 'ENDS_WITH', 'ENDS_WITH_CASE_SENSITIVE', 'EQUAL', 'EQUAL_CASE_SENSITIVE', 'NULL', 'NOT_NULL'
// possible conditions for numeric filter: 'EQUAL', 'NOT_EQUAL', 'LESS_THAN', 'LESS_THAN_OR_EQUAL', 'GREATER_THAN', 'GREATER_THAN_OR_EQUAL', 'NULL', 'NOT_NULL'
// possible conditions for date filter: 'EQUAL', 'NOT_EQUAL', 'LESS_THAN', 'LESS_THAN_OR_EQUAL', 'GREATER_THAN', 'GREATER_THAN_OR_EQUAL', 'NULL', 'NOT_NULL'
let filter1 = filtergroup.createfilter('stringfilter', filtervalue, filtercondition);
filtervalue = 'Andrew';
filtercondition = 'starts_with';
let filter2 = filtergroup.createfilter('stringfilter', filtervalue, filtercondition);3. The third step is to add the filters to the filter group. In the code example below, we added two filters in the filter group with operator 'or'. This means that each cell value will be evaluated by filter1 and filter2 and the evaluation result will be true, if the filter1's returned value is true or filter2's returned value is true.
let filter_or_operator = 1;
filtergroup.addfilter(filter_or_operator, filter1);
filtergroup.addfilter(filter_or_operator, filter2);4. In the final step, we add the filter group to the first column and apply the filters by calling the 'applyfilters' method.
// add the filters
myGrid.value.addfilter('firstname', filtergroup, true);
// apply the filters
myGrid.value.applyfilters();If you want to remove the filter, call the 'removefilter' method and then the 'applyfilters' method.
myGrid.value.removefilter('firstname');
myGrid.value.applyfilters();If you want to clear all filters, use the 'clearfilters' method.
myGrid.value.clearfilters();Custom Filter Conditions
To customize the filter conditions, you need to do the following:
1. Update the Localization Strings. The filter conditions displayed in the filter dropdowns are loaded from the jqxGrid's localization object.
*Use the Grid's "ready" callback or bind to the "bindingcomplete" event before the Grid's initialization.
ready: function () {
let localizationObject = {
filterstringcomparisonoperators: ['contains', 'does not contain'],
// filter numeric comparison operators.
filternumericcomparisonoperators: ['less than', 'greater than'],
// filter date comparison operators.
filterdatecomparisonoperators: ['less than', 'greater than'],
// filter bool comparison operators.
filterbooleancomparisonoperators: ['equal', 'not equal']
}
//apply filters
myGrid.value.localizestrings(localizationObject);
}2. The second step is to update the conditions within the Filter object. In order to achieve that, you need to add a function called "updatefilterconditions". That function is invoked by jqxGrid with 2 parameters - the type of the filter and the default filter conditions. As a result, the function should return an array of strings depending on the filter's type. That array will be used by the filter object.
<JqxGrid ref="myGrid" :width="getWidth" :source="dataAdapter" :columns="columns" :ready="ready"
:filterable="true" :autoshowfiltericon="true" :updatefilterconditions="updatefilterconditions">
</JqxGrid>updatefilterconditions: function (type, defaultconditions) {
let stringcomparisonoperators = ['CONTAINS', 'DOES_NOT_CONTAIN'];
let numericcomparisonoperators = ['LESS_THAN', 'GREATER_THAN'];
let datecomparisonoperators = ['LESS_THAN', 'GREATER_THAN'];
let booleancomparisonoperators = ['EQUAL', 'NOT_EQUAL'];
switch (type) {
case 'stringfilter':
return stringcomparisonoperators;
case 'numericfilter':
return numericcomparisonoperators;
case 'datefilter':
return datecomparisonoperators;
case 'booleanfilter':
return booleancomparisonoperators;
}
}Add filters to multiple columns through the API
<template>
<JqxGrid
ref="myGrid"
:width="width"
:source="dataAdapter"
:columns="columns"
:sortable="true"
:filterable="true"
:autoshowfiltericon="true"
:ready="ready"
>
</JqxGrid>
</template>
<script setup>
import { ref } from 'vue';
import JqxGrid from 'jqwidgets-scripts/jqwidgets-vue3/vue_jqxgrid.vue';
const myGrid = ref(null);
let source = { localdata: generatedata(500, false), datatype: 'array' };
const width = ref(800);
const dataAdapter = ref(new jqx.dataAdapter(source));
const columns = ref([
{ text: 'First Name', datafield: 'firstname', width: 160 },
{ text: 'Last Name', datafield: 'lastname', width: 160 },
{ text: 'Product', datafield: 'productname', width: 170 },
{
text: 'Order Date',
datafield: 'date',
filtertype: 'date',
width: 160,
cellsformat: 'dd-MMMM-yyyy'
},
{ text: 'Quantity', datafield: 'quantity', width: 80, cellsalign: 'right' },
{ text: 'Unit Price', datafield: 'price', cellsalign: 'right', cellsformat: 'c2' }
]);
function addfilter() {
// create a filter group for the FirstName column.
let fNameFiltergroup = new jqx.filter();
// operator between the filters in the filter group. 1 is for OR. 0 is for AND.
let filter_or_operator = 1;
// create a string filter with `contains` condition.
let filtervalue = 'Beate';
let filtercondition = 'contains';
let fNameFilter1 = fNameFiltergroup.createfilter('stringfilter', filtervalue, filtercondition);
// create second filter.
filtervalue = 'Andrew';
filtercondition = 'starts_with';
let fNameFilter2 = fNameFiltergroup.createfilter('stringfilter', filtervalue, filtercondition);
// add the filters to the filter group.
fNameFiltergroup.addfilter(filter_or_operator, fNameFilter1);
fNameFiltergroup.addfilter(filter_or_operator, fNameFilter2);
// add the filter group to the `firstname` column in the Grid.
myGrid.value.addfilter('firstname', fNameFiltergroup);
// create a filter group for the Quantity column.
let quantityFilterGroup = new jqx.filter();
// create a filter.
filter_or_operator = 1;
filtervalue = 3;
filtercondition = 'less_than';
let quantityFilter1 = quantityFilterGroup.createfilter(
'numericfilter',
filtervalue,
filtercondition
);
quantityFilterGroup.addfilter(filter_or_operator, quantityFilter1);
// add the filter group to the `quantity` column in the Grid.
myGrid.value.addfilter('quantity', quantityFilterGroup);
// apply the filters.
myGrid.value.applyfilters();
}
function ready() {
this.addfilter();
}
</script>*generatedata function is implemented in the generatedata.js. The file is included in the download package and can be found in the installation_folder/demos/jqxgrid folder.
By default the filter groups are combined with 'AND' operator. That means that the Grid will display records if they meet the criteria of the filter group applied to column 1 and the filter group applied to column 2, etc. You can create any combination of filter groups using 'AND' and 'OR'.
The example below illustrates how:
<template>
<JqxGrid
ref="myGrid"
:width="width"
:source="dataAdapter"
:columns="columns"
:sortable="true"
:filterable="true"
:autoshowfiltericon="true"
:ready="ready"
>
</JqxGrid>
</template>
<script setup>
import { ref } from 'vue';
import JqxGrid from 'jqwidgets-scripts/jqwidgets-vue3/vue_jqxgrid.vue';
const myGrid = ref(null);
let source = { localdata: generatedata(500, false), datatype: 'array' };
const width = ref(800);
const dataAdapter = ref(new jqx.dataAdapter(source));
const columns = ref([
{ text: 'First Name', datafield: 'firstname', width: 160 },
{ text: 'Last Name', datafield: 'lastname', width: 160 },
{ text: 'Product', datafield: 'productname', width: 170 },
{
text: 'Order Date',
datafield: 'date',
filtertype: 'date',
width: 160,
cellsformat: 'dd-MMMM-yyyy'
},
{ text: 'Quantity', datafield: 'quantity', width: 80, cellsalign: 'right' },
{ text: 'Unit Price', datafield: 'price', cellsalign: 'right', cellsformat: 'c2' }
]);
function addfilter() {
// create a filter group for the FirstName column.
let fNameFiltergroup = new jqx.filter();
fNameFiltergroup.operator = 'or';
// operator between the filters in the filter group. 1 is for OR. 0 is for AND.
let filter_or_operator = 1;
// create a string filter with `contains` condition.
let filtervalue = 'Beate';
let filtercondition = 'contains';
let fNameFilter1 = fNameFiltergroup.createfilter('stringfilter', filtervalue, filtercondition);
// create second filter.
filtervalue = 'Andrew';
filtercondition = 'starts_with';
let fNameFilter2 = fNameFiltergroup.createfilter('stringfilter', filtervalue, filtercondition);
// add the filters to the filter group.
fNameFiltergroup.addfilter(filter_or_operator, fNameFilter1);
fNameFiltergroup.addfilter(filter_or_operator, fNameFilter2);
// add the filter group to the `firstname` column in the Grid.
myGrid.value.addfilter('firstname', fNameFiltergroup);
// create a filter group for the Quantity column.
let quantityFilterGroup = new jqx.filter();
quantityFilterGroup.operator = 'or';
// create a filter.
filter_or_operator = 1;
filtervalue = 3;
filtercondition = 'less_than';
let quantityFilter1 = quantityFilterGroup.createfilter(
'numericfilter',
filtervalue,
filtercondition
);
quantityFilterGroup.addfilter(filter_or_operator, quantityFilter1);
// add the filter group to the `quantity` column in the Grid.
myGrid.value.addfilter('quantity', quantityFilterGroup);
// apply the filters.
myGrid.value.applyfilters();
}
function ready() {
this.addfilter();
}
</script>Using the above code, jqxGrid will display any record which meets at least one of the filtering criteria i.e a record is displayed, if it meets the condition of the 'FirstName' column's filter group or the one of the 'Quantity' column's filter group.