jQuery Grid: Columns Reorder

The Columns drag and drop operation in 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.

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.filter.js, jqxgrid.selection.js, jqxgrid.columnsresize.js, jqxgrid.columnsreorder.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/beverages.txt";

    // prepare the data
    var source =
    {
        datatype: "json",
        datafields: [
            { name: 'name' },
            { name: 'type' },
            { name: 'calories', type: 'int' },
            { name: 'totalfat' },
            { name: 'protein' }
        ],
        id: 'id',
        url: url
    };
    var dataAdapter = new $.jqx.dataAdapter(source);

    $("#grid").jqxGrid(
    {
        width: getWidth('Grid'),
        source: dataAdapter,
        columnsresize: true,
        columnsreorder: true,
        columns: [
          { text: 'Name', datafield: 'name', width: 200 },
          { text: 'Beverage Type', datafield: 'type', width: 200 },
          { text: 'Calories', datafield: 'calories', width: 100 },
          { text: 'Total Fat', datafield: 'totalfat'}
        ]
    });

    // trigger the columnreordered event.
    $("#grid").on('columnreordered', function (event) {
        var column = event.args.columntext;
        var newindex = event.args.newindex
        var oldindex = event.args.oldindex;
        $("#eventlog").text("Column: " + column + ", " + "New Index: " + newindex + ", Old Index: " + oldindex);
    });
});

body.html

<div id="jqxWidget">
    <div id="grid"></div>
    <div style="margin-top: 30px;" id="eventlog"></div> 
</div>

Learn more

More jQuery Grid demos