Create and apply a custom filter to jqxGrid.
A live JavaScript/jQuery example of jqxGrid, the jQWidgets Grid component, with its complete source code below. It is one of 139 jqxGrid demos for jQuery.
autoshowfiltericon property – Displays the filter icon only when the column is filtered.theme property – Sets the widget's theme.height property – Sets or gets the Grid's height.width property – Sets or gets the Grid's width.columns property – Sets the Grid columns.filterable property – Enables or disables the Grid Filtering feature.sortable property – The sortable property enables or disables the sorting feature.source property – Sets and gets the source.addfilter method – Adds a filter to the Grid.applyfilters method – Applies all filters to the Grid.clearfilters method – Clears all filters from the Grid.To run it in your own page, load jqx.base.css, a theme and the library files below from npm (npm install jqwidgets-scripts) or the jsDelivr CDN, then add the markup and the script. jQuery is not required: jqxcore.js provides $. Getting started with jqxGrid shows the setup step by step.
The page loads jqx.base.css, a theme and these library files: jqxcore.js, jqxdata.js, jqxbuttons.js, jqxscrollbar.js, jqxlistbox.js, jqxdropdownlist.js, jqxmenu.js, jqxgrid.js, jqxgrid.filter.js, jqxgrid.sort.js, jqxgrid.selection.js, jqxpanel.js, jqxcheckbox.js.
It also loads helper scripts of this demo site, which are not part of the library: demos.js (getWidth() and other layout helpers), generatedata.js (generatedata(), random sample rows).
$(document).ready(function () {
var data = generatedata(200);
var source =
{
localdata: data,
datafields:
[
{ name: 'firstname', type: 'string' },
{ name: 'lastname', type: 'string' },
{ name: 'productname', type: 'string' },
{ name: 'date', type: 'date' },
{ name: 'quantity', type: 'number' },
{ name: 'price', type: 'number' }
],
datatype: "array"
};
var dataAdapter = new $.jqx.dataAdapter(source);
// create grid.
$("#grid").jqxGrid(
{
width: 600,
source: dataAdapter,
filterable: false,
sortable: true,
autoshowfiltericon: false,
columns: [
{ text: 'First Name', datafield: 'firstname', width: 100 },
{ text: 'Last Name', datafield: 'lastname', width: 100 },
{ text: 'Product', datafield: 'productname', width: 180 },
{ text: 'Order Date', datafield: 'date', width: 100, cellsformat: 'd' },
{ text: 'Quantity', datafield: 'quantity', width: 80, cellsalign: 'right' },
{ text: 'Unit Price', datafield: 'price', width: 100, cellsalign: 'right', cellsformat: 'c2' }
]
});
// create buttons, listbox and the columns chooser dropdownlist.
$("#applyfilter").jqxButton({ theme: theme });
$("#clearfilter").jqxButton({ theme: theme });
$("#filterbox").jqxListBox({ checkboxes: true, width: 200, height: 250 });
$("#columnchooser").jqxDropDownList({ autoDropDownHeight: true, selectedIndex: 0, width: 200, height: 25,
source: [{ label: 'First Name', value: 'firstname' },
{ label: 'Last Name', value: 'lastname' },
{ label: 'Product', value: 'productname' },
{ label: 'Order Date', value: 'date' },
{ label: 'Quantity', value: 'quantity' },
{ label: 'Unit Price', value: 'price' }
]
});
// updates the listbox with unique records depending on the selected column.
var updateFilterBox = function (datafield) {
var filterBoxAdapter = new $.jqx.dataAdapter(source,
{
uniqueDataFields: [datafield],
autoBind: true
});
var uniqueRecords = filterBoxAdapter.records;
uniqueRecords.splice(0, 0, '(Select All)');
$("#filterbox").jqxListBox({ source: uniqueRecords, displayMember: datafield });
$("#filterbox").jqxListBox('checkAll');
}
updateFilterBox('firstname');
// handle select all item.
var handleCheckChange = true;
$("#filterbox").on('checkChange', function (event) {
if (!handleCheckChange)
return;
if (event.args.label != '(Select All)') {
handleCheckChange = false;
$("#filterbox").jqxListBox('checkIndex', 0);
var checkedItems = $("#filterbox").jqxListBox('getCheckedItems');
var items = $("#filterbox").jqxListBox('getItems');
if (checkedItems.length == 1) {
$("#filterbox").jqxListBox('uncheckIndex', 0);
}
else if (items.length != checkedItems.length) {
$("#filterbox").jqxListBox('indeterminateIndex', 0);
}
handleCheckChange = true;
}
else {
handleCheckChange = false;
if (event.args.checked) {
$("#filterbox").jqxListBox('checkAll');
}
else {
$("#filterbox").jqxListBox('uncheckAll');
}
handleCheckChange = true;
}
});
// handle columns selection.
$("#columnchooser").on('select', function (event) {
updateFilterBox(event.args.item.value);
});
// builds and applies the filter.
var applyFilter = function (datafield) {
$("#grid").jqxGrid('clearfilters');
var filtertype = 'stringfilter';
if (datafield == 'date') filtertype = 'datefilter';
if (datafield == 'price' || datafield == 'quantity') filtertype = 'numericfilter';
var filtergroup = new $.jqx.filter();
var checkedItems = $("#filterbox").jqxListBox('getCheckedItems');
if (checkedItems.length == 0) {
var filter_or_operator = 1;
var filtervalue = "Empty";
var filtercondition = 'equal';
var filter = filtergroup.createfilter(filtertype, filtervalue, filtercondition);
filtergroup.addfilter(filter_or_operator, filter);
}
else {
for (var i = 0; i < checkedItems.length; i++) {
var filter_or_operator = 1;
var filtervalue = checkedItems[i].label;
var filtercondition = 'equal';
var filter = filtergroup.createfilter(filtertype, filtervalue, filtercondition);
filtergroup.addfilter(filter_or_operator, filter);
}
}
// add the filters.
$("#grid").jqxGrid('addfilter', datafield, filtergroup);
// apply the filters.
$("#grid").jqxGrid('applyfilters');
}
// clears the filter.
$("#clearfilter").click(function () {
$("#grid").jqxGrid('clearfilters');
});
// applies the filter.
$("#applyfilter").click(function () {
var dataField = $("#columnchooser").jqxDropDownList('getSelectedItem').value;
applyFilter(dataField);
});
});
<div id="grid">
</div>
<div style="margin-top: 20px;">
<div>Choose Column:</div>
<div id="columnchooser"></div>
<div style="margin-top: 10px;" id="filterbox"></div>
<div style="margin-top: 10px;">
<input type="button" id="applyfilter" value="Apply Filter">
<input type="button" id="clearfilter" value="Clear Filter">
</div>
</div>