jQuery Grid: Custom Row Editor

The example shows how to assign custom editors to cells depending on the row's index.

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.edit.js, jqxgrid.selection.js, jqxlistbox.js, jqxdropdownlist.js, jqxcheckbox.js, jqxnumberinput.js, jqxslider.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 () {
    

    // prepare the data
    var data = new Array();
    data.push({ "Name": "Population", "Berlin": "3560154", "Boston": "3406829", "London": "8174100" });
    data.push({ "Name": "Country", "Berlin": "Germany", "Boston": "United States", "London": "United Kingdom" });
    data.push({ "Name": "Capital", "Berlin": "true", "Boston": "false", "London": "true" });

    var source =
    {
        localdata: data,
        datatype: "array",
        updaterow: function (rowid, rowdata, commit) {
            // synchronize with the server - send update command
            // call commit with parameter true if the synchronization with the server is successful 
            // and with parameter false if the synchronization failder.
            commit(true);
        },
        datafields:
        [
            { name: 'Name', type: 'string' },
            { name: 'Berlin', type: 'string' },
            { name: 'Boston', type: 'string' },
            { name: 'London', type: 'string' }
        ]
    };

    var dataAdapter = new $.jqx.dataAdapter(source);

    var createGridEditor = function(row, cellValue, editor, cellText, width, height)
    {
        // construct the editor.
        if (row == 0) {
            editor.jqxNumberInput({ decimalDigits: 0, inputMode: 'simple',  width: width, height: height, spinButtons: true });
        }
        else if (row == 1) {
            editor.jqxDropDownList({autoDropDownHeight: true,  width: width, height: height, source: ['United States', 'Germany', 'United Kingdom']});
        }
        else if (row == 2) {
            var element = $('<div tabIndex=0 style="position: absolute; top: 50%; left: 50%; margin-top: -7px; margin-left: -10px;"></div>');
            editor.append(element);
            element.jqxCheckBox({ animationShowDelay: 0, animationHideDelay: 0, width: 18, height: 18 });
        }
    }

    var initGridEditor = function (row, cellValue, editor, cellText, width, height) {
        // set the editor's current value. The callback is called each time the editor is displayed.
        if (row == 0) {
            editor.jqxNumberInput({ decimal: parseInt(cellValue)});
        }
        else if (row == 1) {
            editor.jqxDropDownList('selectItem', cellValue);
        }
        else if (row == 2) {
            var checkBoxHTMLElement = editor.find('div:first');
            checkBoxHTMLElement.jqxCheckBox({ checked: cellValue.toString() == "true" });
        }
    }

    var gridEditorValue = function (row, cellValue, editor) {
        if (row == 2) {
            var checkBoxHTMLElement = editor.find('div:first');
            return checkBoxHTMLElement.val();
        }

        return editor.val();
    }

    // initialize jqxGrid
    $("#grid").jqxGrid(
    {
        width: getWidth('Grid'),
        autoheight: true,
        source: dataAdapter,
        editable: true,
        
        selectionmode: 'singlecell',
        columns: [
          {
              text: 'Name', pinned: true, editable: false,  datafield: 'Name', width: 150
          },
          {
              text: 'Boston', columntype: 'custom', datafield: 'Boston', width: 150,
              createeditor: createGridEditor, initeditor: initGridEditor, geteditorvalue: gridEditorValue
          },
          {
              text: 'Berlin', columntype: 'custom', datafield: 'Berlin', width: 150,
              createeditor: createGridEditor, initeditor: initGridEditor, geteditorvalue: gridEditorValue
              },
          {
              text: 'London', columntype: 'custom', datafield: 'London',
              createeditor: createGridEditor, initeditor: initGridEditor, geteditorvalue: gridEditorValue
          }
        ]
    });
});

body.html

<div id="grid"></div>

Learn more

More jQuery Grid demos