Angular 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:
1. The first step is to create a filter group. The filter group is a group of one or more filtering criteria.
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
this.myGrid().addfilter('firstname', filtergroup, true);
// apply the filters
this.myGrid().applyfilters();If you want to remove the filter, call the 'removefilter' method and then the 'applyfilters' method.
this.myGrid().removefilter('firstname');
this.myGrid().applyfilters();If you want to clear all filters, use the 'clearfilters' method.
this.myGrid().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 = (): void => {
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']
}
this.myGrid().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 #myGrid [theme]="'material'" [width]="'100%'" [source]="dataAdapter"
[autoheight]="true" [columns]="columns" [filterable]="true"
[autoshowfiltericon]="true" [ready]="ready" [updatefilterconditions]="updatefilterconditions">
</jqxGrid>updatefilterconditions = (type: string, defaultconditions: any): string[] => {
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
<jqxGrid #myGrid [theme]="'material'" [width]="'100%'" [source]="dataAdapter"
[autoheight]="true" [columns]="columns" [sortable]="true"
[filterable]="true" [autoshowfiltericon]="true" [ready]="ready">
</jqxGrid>import { Component, viewChild } from '@angular/core';
import { jqxGridComponent, jqxGridModule } from 'jqwidgets-ng/jqxgrid';
@Component({ selector: 'app-root', imports: [jqxGridModule], templateUrl: './app.html' })
export class App {
readonly myGrid = viewChild.required<jqxGridComponent>('myGrid');
source: any = {
localdata: [
{
firstname: 'Andrew',
lastname: 'Burke',
productname: 'White Chocolate Mocha',
quantity: 5,
price: 3.8
},
{
firstname: 'Andrew',
lastname: 'Wilson',
productname: 'Espresso con Panna',
quantity: 1,
price: 5
},
{
firstname: 'Nancy',
lastname: 'Fuller',
productname: 'Caffe Latte',
quantity: 1,
price: 3.5
},
{
firstname: 'Regina',
lastname: 'Wilson',
productname: 'Doubleshot Espresso',
quantity: 7,
price: 4.2
},
{
firstname: 'Mayumi',
lastname: 'Davolio',
productname: 'Caffe Espresso',
quantity: 1,
price: 3.6
},
{
firstname: 'Beate',
lastname: 'Saavedra',
productname: 'Caffe Latte',
quantity: 2,
price: 3.5
},
{
firstname: 'Beate',
lastname: 'Nodier',
productname: 'White Chocolate Mocha',
quantity: 6,
price: 3.8
},
{
firstname: 'Petra',
lastname: 'Winkler',
productname: 'Doubleshot Espresso',
quantity: 5,
price: 4.6
},
{
firstname: 'Andrew',
lastname: 'Rossi',
productname: 'Caffe Latte',
quantity: 2,
price: 3.5
},
{
firstname: 'Nancy',
lastname: 'Saavedra',
productname: 'Cappuccino',
quantity: 1,
price: 3.8
},
{
firstname: 'Regina',
lastname: 'Wilson',
productname: 'Doubleshot Espresso',
quantity: 7,
price: 4.6
},
{
firstname: 'Mayumi',
lastname: 'Bjorn',
productname: 'Black Tea',
quantity: 1,
price: 3.8
},
{
firstname: 'Beate',
lastname: 'Petersen',
productname: 'Caffe Latte',
quantity: 2,
price: 3.5
},
{
firstname: 'Saavedra',
lastname: 'Fuller',
productname: 'Black Tea',
quantity: 6,
price: 3.8
}
],
datatype: 'array'
};
dataAdapter: any = new jqx.dataAdapter(this.source);
columns: any[] = [
{ 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' }
];
addfilter = (): void => {
// 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.
this.myGrid().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.
this.myGrid().addfilter('quantity', quantityFilterGroup);
// apply the filters.
this.myGrid().applyfilters();
};
ready = (): void => {
this.addfilter();
};
}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:
<jqxGrid #myGrid [theme]="'material'" [width]="'100%'" [source]="dataAdapter"
[autoheight]="true" [columns]="columns" [sortable]="true"
[filterable]="true" [autoshowfiltericon]="true" [ready]="ready">
</jqxGrid>import { Component, viewChild } from '@angular/core';
import { jqxGridComponent, jqxGridModule } from 'jqwidgets-ng/jqxgrid';
@Component({ selector: 'app-root', imports: [jqxGridModule], templateUrl: './app.html' })
export class App {
readonly myGrid = viewChild.required<jqxGridComponent>('myGrid');
source: any = {
localdata: [
{
firstname: 'Andrew',
lastname: 'Burke',
productname: 'White Chocolate Mocha',
quantity: 5,
price: 3.8
},
{
firstname: 'Andrew',
lastname: 'Wilson',
productname: 'Espresso con Panna',
quantity: 1,
price: 5
},
{
firstname: 'Nancy',
lastname: 'Fuller',
productname: 'Caffe Latte',
quantity: 1,
price: 3.5
},
{
firstname: 'Regina',
lastname: 'Wilson',
productname: 'Doubleshot Espresso',
quantity: 7,
price: 4.2
},
{
firstname: 'Mayumi',
lastname: 'Davolio',
productname: 'Caffe Espresso',
quantity: 1,
price: 3.6
},
{
firstname: 'Beate',
lastname: 'Saavedra',
productname: 'Caffe Latte',
quantity: 2,
price: 3.5
},
{
firstname: 'Beate',
lastname: 'Nodier',
productname: 'White Chocolate Mocha',
quantity: 6,
price: 3.8
},
{
firstname: 'Petra',
lastname: 'Winkler',
productname: 'Doubleshot Espresso',
quantity: 5,
price: 4.6
},
{
firstname: 'Andrew',
lastname: 'Rossi',
productname: 'Caffe Latte',
quantity: 2,
price: 3.5
},
{
firstname: 'Nancy',
lastname: 'Saavedra',
productname: 'Cappuccino',
quantity: 1,
price: 3.8
},
{
firstname: 'Regina',
lastname: 'Wilson',
productname: 'Doubleshot Espresso',
quantity: 7,
price: 4.6
},
{
firstname: 'Mayumi',
lastname: 'Bjorn',
productname: 'Black Tea',
quantity: 1,
price: 3.8
},
{
firstname: 'Beate',
lastname: 'Petersen',
productname: 'Caffe Latte',
quantity: 2,
price: 3.5
},
{
firstname: 'Saavedra',
lastname: 'Fuller',
productname: 'Black Tea',
quantity: 6,
price: 3.8
}
],
datatype: 'array'
};
dataAdapter: any = new jqx.dataAdapter(this.source);
columns: any[] = [
{ 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' }
];
addfilter = (): void => {
// 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.
this.myGrid().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.
this.myGrid().addfilter('quantity', quantityFilterGroup);
// apply the filters.
this.myGrid().applyfilters();
};
ready = (): void => {
this.addfilter();
};
}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.