Add New Row to Top
Add New Row to Bottom

jQuery Grid: Ever Present Row with Custom Widgets

The sample shows how to customize fields in the ever present row. The first Text Field in the ever present row is disabled.

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, jqxlistbox.js, jqxdropdownlist.js, jqxmenu.js, jqxinput.js, jqxgrid.js, jqxgrid.filter.js, jqxgrid.sort.js, jqxgrid.selection.js, jqxgrid.edit.js, jqxgrid.columnsresize.js, jqxpanel.js, jqxcalendar.js, jqxradiobutton.js, jqxnumberinput.js, jqxdatetimeinput.js, jqxcheckbox.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 () {
    var data = generatedata(20);
    var source =
    {
        localdata: data,
        datafields:
        [
            { name: 'name', type: 'string' },
            { name: 'productname', type: 'string' },
            { name: 'available', type: 'bool' },
            { name: 'date', type: 'date' },
            { name: 'quantity', type: 'number' }
        ],
        datatype: "array"
    };

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

    var getSourceAdapter = function (name) {
        var source =
        {
            localdata: data,
            datafields:
            [
                { name: 'name', type: 'string' },
                { name: 'productname', type: 'string' }
            ],
            datatype: "array"
        };
        var fields = new Array();
        fields.push(name);
        var dataAdapter = new $.jqx.dataAdapter(source, { autoBind: true, autoSort: true, uniqueDataFields: fields, autoSortField: name });
        return dataAdapter.records;
    }

    $("#grid").jqxGrid(
    {
        width: getWidth('Grid'),
        filterable: true,
        source: dataAdapter,
        showeverpresentrow: true,
        everpresentrowposition: "top",
        editable: true,
        everpresentrowactionsmode: "columns",
        columns: [
          {
              text: 'Name', columntype: 'textbox', filtertype: 'input', datafield: 'name', width: 215,
              createEverPresentRowWidget: function (datafield, htmlElement, popup, addCallback) {
                  var inputTag = $("<input style='box-sizing: border-box; border: none;'/>").appendTo(htmlElement);
                  inputTag.jqxInput({ popupZIndex: 99999999, placeHolder: "Enter Name: ", source: getSourceAdapter("name"), displayMember: 'name', width: '100%', height: 30 });
                  $(document).on('keydown.name', function (event) {
                      if (event.keyCode == 13) {
                          if (event.target === inputTag[0]) {
                              addCallback();
                          }
                      }
                  });

                  var input = inputTag.parent()[0].className.indexOf('jqx-input-group') >= 0 ? inputTag.parent() : inputTag;

                  return input;
              },
              initEverPresentRowWidget: function (datafield, htmlElement) {
              },
              validateEverPresentRowWidgetValue: function (datafield, value, rowValues) {
                  if (!value || (value && value.length < 5)) {
                      return { message: "Entered value should be more than 5 characters", result: false };
                  }
                  return true;
              },
              getEverPresentRowWidgetValue: function (datafield, htmlElement, validate) {
                  var value = htmlElement.val();
                  return value;
              },
              resetEverPresentRowWidgetValue: function (datafield, htmlElement) {
                  htmlElement.val("");
              }
          },
          {
              text: 'Product', filtertype: 'checkedlist', datafield: 'productname', width: 220,
              createEverPresentRowWidget: function (datafield, htmlElement, popup, addCallback) {
                  var inputTag = $("<div style='border: none;'></div>").appendTo(htmlElement);
                  inputTag.jqxDropDownList({ popupZIndex: 99999999, placeHolder: "Enter Product: ", source: getSourceAdapter("productname"), displayMember: 'productname', width: '100%', height: 30 });
                  $(document).on('keydown.productname', function (event) {
                      if (event.keyCode == 13) {
                          if (event.target === inputTag[0]) {
                              addCallback();
                          }
                          else if ($(event.target).ischildof(inputTag)) {
                              addCallback();
                          }
                      }
                  });
                  return inputTag;
              },
              getEverPresentRowWidgetValue: function (datafield, htmlElement, validate) {
                  var selectedItem = htmlElement.jqxDropDownList('getSelectedItem');
                  if (!selectedItem)
                      return "";

                  var value = selectedItem.label;
                  return value;
              },
              resetEverPresentRowWidgetValue: function (datafield, htmlElement) {
                  htmlElement.jqxDropDownList('clearSelection');
              }
          },
          {
              text: 'Ship Date', datafield: 'date', filtertype: 'range', width: 210, cellsalign: 'right', cellsformat: 'd',
              createEverPresentRowWidget: function (datafield, htmlElement, popup, addCallback) {
                  var inputTag = $("<div style='border: none;'></div>").appendTo(htmlElement);
                  inputTag.jqxDateTimeInput({ value: null, popupZIndex: 99999999, placeHolder: "Enter Date: ", width: '100%', height: 30 });
                  $(document).on('keydown.date', function (event) {
                      if (event.keyCode == 13) {
                          if (event.target === inputTag[0]) {
                              addCallback();
                          }
                          else if ($(event.target).ischildof(inputTag)) {
                              addCallback();
                          }
                      }
                  });
                  return inputTag;
              },
              initEverPresentRowWidget: function (datafield, htmlElement) {
              },
              getEverPresentRowWidgetValue: function (datafield, htmlElement, validate) {
                  var value = htmlElement.val();
                  return value;
              },
              resetEverPresentRowWidgetValue: function (datafield, htmlElement) {
                  htmlElement.val(null);
              }
          },
          {
              text: 'Qty.', datafield: 'quantity', filtertype: 'number', cellsalign: 'right',
              createEverPresentRowWidget: function (datafield, htmlElement, popup, addCallback) {
                  var inputTag = $("<div style='border: none;'></div>").appendTo(htmlElement);
                  inputTag.jqxNumberInput({ inputMode: 'simple', decimalDigits: 0, width: '100%', height: 30 });
                  $(document).on('keydown.qty', function (event) {
                      if (event.keyCode == 13) {
                          if (event.target === inputTag[0]) {
                              addCallback();
                          }
                          else if ($(event.target).ischildof(inputTag)) {
                              addCallback();
                          }
                      }
                  });
                  return inputTag;
              },
              initEverPresentRowWidget: function (datafield, htmlElement) {
              },
              validateEverPresentRowWidgetValue: function (datafield, value, rowValues) {
                  if (parseInt(value) < 0) {
                      return { message: "Entered value should be positive number", result: false };
                  }
                  return true;
              },
              getEverPresentRowWidgetValue: function (datafield, htmlElement, validate) {
                  var value = htmlElement.val();
                  if (value == "") value = 0;
                  return parseInt(value);
              },
              resetEverPresentRowWidgetValue: function (datafield, htmlElement) {
                  htmlElement.val("");
              }
          },
    	  { text: '', datafield: 'addButtonColumn', width: 50 },
          { text: '', datafield: 'resetButtonColumn', width: 50 }
        ]
    });

    $("#top").jqxRadioButton({ checked: true });
    $("#bottom").jqxRadioButton();
    $("#top").on('checked', function () {
        $("#grid").jqxGrid('everpresentrowactions', 'add reset');
    });
    $("#bottom").on('checked', function () {
        $("#grid").jqxGrid('everpresentrowactions', 'addBottom reset');
    });
});

body.html

<div id="grid">
</div>
<br>
<div id="top">Add New Row to Top</div>
<div id="bottom">Add New Row to Bottom</div>

Learn more

More jQuery Grid demos