Group:


Event Log:
Expanded Group:
Collapsed Group:

jQuery Grid: Grouping with Pager

The sample illustrates the Grid's Grouping feature, when Paging is enabled.

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, jqxmenu.js, jqxgrid.js, jqxgrid.grouping.js, jqxgrid.selection.js, jqxgrid.pager.js, jqxgrid.sort.js, jqxlistbox.js, jqxdropdownlist.js.

It also loads helper scripts of this demo site, which are not part of the library: demos.js (getWidth() and other layout helpers).

script.js

$(document).ready(function () {
    var url = "../sampledata/customers.xml";

    // prepare the data
    var source =
    {
        datatype: "xml",
        datafields: [
            { name: 'CompanyName', map: 'm\\:properties>d\\:CompanyName', type: 'string' },
            { name: 'ContactName', map: 'm\\:properties>d\\:ContactName', type: 'string' },
            { name: 'ContactTitle', map: 'm\\:properties>d\\:ContactTitle', type: 'string' },
            { name: 'City', map: 'm\\:properties>d\\:City', type: 'string' },
            { name: 'PostalCode', map: 'm\\:properties>d\\:PostalCode', type: 'string' },
            { name: 'Country', map: 'm\\:properties>d\\:Country', type: 'string' }
        ],
        root: "entry",
        record: "content",
        id: 'm\\:properties>d\\:CustomerID',
        url: url
    };
    var dataAdapter = new $.jqx.dataAdapter(source);

    // Create jqxGrid
    $("#grid").jqxGrid(
    {
        width: getWidth('Grid'),
        source: dataAdapter,
        groupable: true,
        pageable: true,
        height: 280,
        columns: [
          { text: 'Company Name', datafield: 'CompanyName', width: 250 },
          { text: 'City', datafield: 'City', width: 120 },
          { text: 'Country', datafield: 'Country' }
        ],
        groups: ['City']
    });

    $("#expand").jqxButton({ theme: theme });
    $("#collapse").jqxButton({ theme: theme });
    $("#expandall").jqxButton({ theme: theme });
    $("#collapseall").jqxButton({ theme: theme });

    // expand group.
    $("#expand").on('click', function () {      
        var groupnum = parseInt($("#groupnum").val());
        if (!isNaN(groupnum)) {
            $("#grid").jqxGrid('expandgroup', groupnum);
        }
    });

    // collapse group.
    $("#collapse").on('click', function () {
        var groupnum = parseInt($("#groupnum").val());
        if (!isNaN(groupnum)) {
            $("#grid").jqxGrid('collapsegroup', groupnum);
        }
    });

    // expand all groups.
    $("#expandall").on('click', function () {
        $("#grid").jqxGrid('expandallgroups');
    });

    // collapse all groups.
    $("#collapseall").on('click', function () {
        $("#grid").jqxGrid('collapseallgroups');
    });

    // trigger expand and collapse events.
    $("#grid").on('groupexpand', function (event) {
        var args = event.args;
        $("#expandedgroup").text("Group: " + args.group + ", Level: " + args.level);
    });

    $("#grid").on('groupcollapse', function (event) {
        var args = event.args;
        $("#collapsedgroup").text("Group: " + args.group + ", Level: " + args.level);
    });
});

body.html

<div id="grid">
</div>
<div style="margin-top: 30px;">
    <div style="float: left; margin-left: 20px;">
        <input value="Expand Group" type="button" id="expand">
        <br>
        <input style="margin-top: 10px;" value="Collapse Group" type="button" id="collapse">
        <br>
        <span style="margin-top: 10px;">Group:</span>
        <input value="1" id="groupnum" style="margin-top: 10px; width: 20px;" type="text">
    </div>
    <div style="float: left; margin-left: 20px;">
        <input value="Expand All Groups" type="button" id="expandall">
        <br>
        <input style="margin-top: 10px; margin-bottom: 10px;" value="Collapse All Groups" type="button" id="collapseall">
        <br>
    </div>
    <div style="float: left; margin-left: 20px;">
        <div style="font-weight: bold;">
            <span>Event Log:</span>
        </div>
        <div style="margin-top: 10px;">
            <span>Expanded Group:</span> <span id="expandedgroup"></span>
        </div>
        <div style="margin-top: 10px;">
            <span>Collapsed Group:</span> <span id="collapsedgroup"></span>
        </div>
    </div>
</div>

Learn more

More jQuery Grid demos