Edit Mode:
Editable Columns:
First Name
Last Name
Quantity
Product
Available

jQuery Grid: Edit Modes

In order to enter in edit mode, 'click' on a Grid cell. The sample illustrates how to enable/disable the editing and the jqxGrid edit modes.

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, jqxcalendar.js, jqxnumberinput.js, jqxdatetimeinput.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 = generatedata(200);

    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 failed.
            commit(true);
        },
        datafields:
        [
            { name: 'firstname', type: 'string' },
            { name: 'lastname', type: 'string' },
            { name: 'productname', type: 'string' },
            { name: 'available', type: 'bool' },
            { name: 'quantity', type: 'number' },
            { name: 'price', type: 'number' }
        ]
    };

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

    // initialize jqxGrid
    $("#grid").jqxGrid(
    {
        width: getWidth('Grid'),
        source: dataAdapter,
        editable: true,
        selectionmode: 'singlecell',
        editmode: 'click',
        columns: [
          { text: 'First Name', columntype: 'textbox', datafield: 'firstname', width: 90 },
          { text: 'Last Name', datafield: 'lastname', width: 90 },
          { text: 'Product', datafield: 'productname' },
          { text: 'Quantity', datafield: 'quantity', width: 70, cellsalign: 'right' },
          { text: 'Available', datafield: 'available', columntype: 'checkbox', width: 67 }
       ]
    });

    var editModes = ['Click', 'Double-Click', 'Selected Cell Click'];
    $("#editmodes").jqxDropDownList({  autoDropDownHeight: true, dropDownWidth: 150, width: 150, height: 25, selectedIndex: 0, source: editModes });
    $("#firstname").jqxCheckBox({  checked: true });
    $("#lastname").jqxCheckBox({  checked: true });
    $("#productname").jqxCheckBox({  checked: true });
    $("#available").jqxCheckBox({  checked: true });
    $("#quantity").jqxCheckBox({  checked: true });

    // change the Edit mode.
    $("#editmodes").on('select', function (event) {
        var index = event.args.index;
        var editMode = index == 0 ? 'click' : index == 1 ? 'dblclick' : 'selectedcell';
        $("#grid").jqxGrid({ editmode: editMode });
    });

    // change the Columns Editable state.
    $("#firstname, #lastname, #productname, #available, #quantity").on('change', function (event) {
        var datafield = event.target.id;
        $("#grid").jqxGrid('setcolumnproperty', datafield, 'editable', event.args.checked);
    });
});

body.html

<div id="grid">
</div>
<div style="margin-top: 20px;">
    <div style="float: left; margin-top: 10px;">
        <span>Edit Mode:</span>
        <div style="margin-top: 5px;" id="editmodes">
        </div>
    </div>
    <div style="float: left; margin-left: 20px; margin-top: 10px;">
        <span>Editable Columns:</span>
        <div>
            <div style="float: left;">
                <div style="margin-top: 5px;" id="firstname">
                    First Name</div>
                <div style="margin-top: 5px;" id="lastname">
                    Last Name</div>
                <div style="margin-top: 5px;" id="quantity">
                    Quantity</div>
            </div>
            <div style="float: left; margin-left: 20px;">
                <div style="margin-top: 5px;" id="productname">
                    Product</div>
                <div style="margin-top: 5px;" id="available">
                    Available</div>
            </div>
        </div>
    </div>
</div>

Learn more

More jQuery Grid demos