Filter Background
Show All Filter Icons
Event Log:

jQuery Grid: Filtering

The Grid filtering feature. Move the mouse cursor over a column header and click the dropdown button to open the filtering menu.

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.

jqxGrid API used in this demo

Source code

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, jqxcalendar.js, jqxdatetimeinput.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).

script.js

$(document).ready(function () {
    var data = generatedata(500);
    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 addfilter = function () {
        var filtergroup = new $.jqx.filter();

        var filter_or_operator = 1;
        var filtervalue = 'Beate';
        var filtercondition = 'contains';
        var filter1 = filtergroup.createfilter('stringfilter', filtervalue, filtercondition);

        filtervalue = 'Andrew';
        filtercondition = 'starts_with';
        var filter2 = filtergroup.createfilter('stringfilter', filtervalue, filtercondition);

        filtergroup.addfilter(filter_or_operator, filter1);
        filtergroup.addfilter(filter_or_operator, filter2);
        // add the filters.
        $("#grid").jqxGrid('addfilter', 'firstname', filtergroup);
        // apply the filters.
        $("#grid").jqxGrid('applyfilters');
    }
    var adapter = new $.jqx.dataAdapter(source);

    $("#grid").jqxGrid(
    {
        width: getWidth('Grid'),
        source: adapter,
        filterable: true,
        sortable: true,
        ready: function () {
            addfilter();
        },
        autoshowfiltericon: true,
        columns: [
          { 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' }
        ]
    });

    $('#events').jqxPanel({ width: 300, height: 80});

    $("#grid").on("filter", function (event) {
        $("#events").jqxPanel('clearcontent');
        var filterinfo = $("#grid").jqxGrid('getfilterinformation');

        var eventData = "Triggered 'filter' event";
        for (i = 0; i < filterinfo.length; i++) {
            var eventData = "Filter Column: " + filterinfo[i].filtercolumntext;
            $('#events').jqxPanel('prepend', '<div style="margin-top: 5px;">' + eventData + '</div>');
        }
    });

    $('#clearfilteringbutton').jqxButton({ height: 25});
    $('#filterbackground').jqxCheckBox({ checked: true, height: 25});
    $('#filtericons').jqxCheckBox({ checked: false, height: 25});
    // clear the filtering.
    $('#clearfilteringbutton').click(function () {
        $("#grid").jqxGrid('clearfilters');
    });
    // show/hide filter background
    $('#filterbackground').on('change', function (event) {
        $("#grid").jqxGrid({ showfiltercolumnbackground: event.args.checked });
    });
    // show/hide filter icons
    $('#filtericons').on('change', function (event) {
        $("#grid").jqxGrid({ autoshowfiltericon: !event.args.checked });
    });
});

body.html

<div id="grid">
</div>
<div id="eventslog" style="margin-top: 30px;">
    <div style="width: 200px; float: left; margin-right: 10px;">
    <input value="Remove Filter" id="clearfilteringbutton" type="button">
    <div style="margin-top: 10px;" id="filterbackground">Filter Background</div>
    <div style="margin-top: 10px;" id="filtericons">Show All Filter Icons</div>
    </div>
    <div style="float: left;">
        Event Log:
        <div style="border: none;" id="events">
        </div>
    </div>
</div>

Learn more

More jQuery Grid demos