Sort Background
Event Log:

jQuery Grid: Sorting

Enable sorting and sort by a column.

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.sort.js, jqxgrid.filter.js, jqxgrid.selection.js, jqxpanel.js, jqxcheckbox.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/orders.xml";

    // prepare the data
    var source =
    {
        datatype: "xml",
        datafields: [
            { name: 'ShippedDate', map: 'm\\:properties>d\\:ShippedDate', type: 'date' },
            { name: 'Freight', map: 'm\\:properties>d\\:Freight', type: 'float' },
            { name: 'ShipName', map: 'm\\:properties>d\\:ShipName', type: 'string' },
            { name: 'ShipAddress', map: 'm\\:properties>d\\:ShipAddress', type: 'string' },
            { name: 'ShipCity', map: 'm\\:properties>d\\:ShipCity', type: 'string' },
            { name: 'ShipCountry', map: 'm\\:properties>d\\:ShipCountry', type: 'string' }
        ],
        root: "entry",
        record: "content",
        id: { name: "OrderID", map: "m\\:properties>d\\:OrderID" },
        url: url,
        sortcolumn: 'ShipName',
        sortdirection: 'asc'
    };
    var dataAdapter = new $.jqx.dataAdapter(source);

    // create grid.
    $("#grid").jqxGrid(
    {
        width: getWidth('Grid'),
        height: 450,
        source: dataAdapter,
        sortable: true,
        filterable: true,
        altrows: true,
        columns: [
          { text: 'Ship Name', datafield: 'ShipName', width: 250 },
          { text: 'Shipped Date', datafield: 'ShippedDate', width: 100, cellsformat: 'yyyy-MM-dd' },
          { text: 'Freight', datafield: 'Freight', width: 80, cellsformat: 'F2', cellsalign: 'right' },
          { text: 'Ship Address', datafield: 'ShipAddress', width: 350 },
          { text: 'Ship City', datafield: 'ShipCity', width: 100 },
          { text: 'Ship Country', datafield: 'ShipCountry', width: 101 }
        ]
    });

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

    $("#grid").on("sort", function (event) {
        $("#events").jqxPanel('clearcontent');

        var sortinformation = event.args.sortinformation;
        var sortdirection = sortinformation.sortdirection.ascending ? "ascending" : "descending";
        if (!sortinformation.sortdirection.ascending && !sortinformation.sortdirection.descending) {
            sortdirection = "null";
        }

        var eventData = "Triggered 'sort' event <div>Column:" + sortinformation.sortcolumn + ", Direction: " + sortdirection + "</div>";
        $('#events').jqxPanel('prepend', '<div style="margin-top: 5px;">' + eventData + '</div>');
    });

    $('#clearsortingbutton').jqxButton({ height: 25});
    $('#sortbackground').jqxCheckBox({checked: true, height: 25});
    // clear the sorting.
    $('#clearsortingbutton').click(function () {
        $("#grid").jqxGrid('removesort');
    });
    // show/hide sort background
    $('#sortbackground').on('change', function (event) {
        $("#grid").jqxGrid({ showsortcolumnbackground: event.args.checked });
    });
});

body.html

<div id="grid">
</div>
<div id="eventslog" style="margin-top: 30px;">
    <div style="float: left; margin-right: 10px;">
    <input value="Remove Sort" id="clearsortingbutton" type="button">
    <div style="margin-top: 10px;" id="sortbackground">Sort Background</div>
    </div>
    <div style="margin-left: 100px; float: left;">
        Event Log:
        <div style="border: none;" id="events">
        </div>
    </div>
</div>

Learn more

More jQuery Grid demos