jQuery Grid: Customized Editors

Customize the Grid editors.

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.selection.js, jqxgrid.pager.js, jqxlistbox.js, jqxgrid.edit.js, jqxdropdownlist.js, jqxcombobox.js, jqxpanel.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 () {           
    // prepare the data
    var ordersSource =
    {
        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: 'm\\:properties>d\\:OrderID',
        url: "../sampledata/orders.xml",
        pager: function (pagenum, pagesize, oldpagenum) {
            // callback called when a page or page size is changed.
        },
        updaterow: function (rowid, rowdata, result) {
            // synchronize with the server - send update command
            // call result with parameter true if the synchronization with the server is successful 
            // and with parameter false if the synchronization failder.
            result(true);
        }
    };
    var ordersAdapter = new $.jqx.dataAdapter(ordersSource);

    $("#grid").jqxGrid(
    {
        width: getWidth('Grid'),
        source: ordersAdapter,
        selectionmode: 'singlecell',
        editable: true,
        pageable: true,
        autoheight: true,
        columns: [
            {
                text: 'Ship City', datafield: 'ShipCity', width: 150, columntype: 'combobox',
                createeditor: function (row, column, editor) {
                    // assign a new data source to the combobox.
                    var list = ['Stuttgart', 'Rio de Janeiro', 'Strasbourg'];
                    editor.jqxComboBox({ autoDropDownHeight: true, source: list, promptText: "Please Choose:" });
                },
                // update the editor's value before saving it.
                cellvaluechanging: function (row, column, columntype, oldvalue, newvalue) {
                    // return the old value, if the new value is empty.
                    if (newvalue == "") return oldvalue;
                }
            },
            {
                text: 'Ship Country', datafield: 'ShipCountry', width: 150, columntype: 'dropdownlist',
                createeditor: function (row, column, editor) {
                    // assign a new data source to the dropdownlist.
                    var list = ['Germany', 'Brazil', 'France'];
                    editor.jqxDropDownList({ autoDropDownHeight: true, source: list });
                },
                // update the editor's value before saving it.
                cellvaluechanging: function (row, column, columntype, oldvalue, newvalue) {
                    // return the old value, if the new value is empty.
                    if (newvalue == "") return oldvalue;
                }
            },
            { text: 'Ship Name', datafield: 'ShipName', columntype: 'combobox' }
        ]
    });
});

body.html

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

Learn more

More jQuery Grid demos