I am using jQgrid Free (release 4.15.2) and I need to add the ability to edit rows inline which is not a problem at all because it's pretty easy to setup. Here is the code I am using:
$.jgrid = $.jgrid || {};
$.jgrid.no_legacy_api = true;
$.jgrid.useJSON = true;
$(function () {
"use strict";
var $grid = $("#list"),
pagerSelector = "#pager",
customAddButton = function (options) {
$grid.jqGrid('navButtonAdd', pagerSelector, options);
$grid.jqGrid('navButtonAdd', '#' + $grid[0].id + "_toppager", options);
};
$.fn.fmatter.customActionsFormatter = function (cellValue, options, rowData) {
return '<a href="#" title="Delete selected row"><span class="fa fa-fw fa-trash-o delete_row" data-id="' + rowData.Id + '"></span></a>';
};
$grid.jqGrid({
url: '/ajax/plans_to_forms/get_all',
datatype: "json",
colNames: ["", "Id", "Form #", "Form", "Plan", "Class", "Drug"],
colModel: [
{name: "act", formatter: "customActionsFormatter", width: 20, search: false},
{name: "Id", jsonmap: "Id", key: true, hidden: true},
{name: "FormId", align: 'center', fixed: true, frozen: true, resizable: false, width: 100},
{name: "FormName", width: 300},
{name: "PlanName", width: 300},
{name: "DrugGroupName", width: 300},
{name: "DrugName", width: 300}
],
cmTemplate: {autoResizable: true, editable: true},
iconSet: "fontAwesome",
rowNum: 25,
guiStyle: "bootstrap",
autoResizing: {compact: true},
rowList: [25, 50, 100, "10000:All"],
viewrecords: true,
autoencode: true,
sortable: true,
pager: pagerSelector,
toppager: true,
cloneToTop: true,
hoverrows: true,
multiselect: true,
multiPageSelection: true,
rownumbers: true,
sortname: "Id",
sortorder: "desc",
loadonce: true,
autowidth: true,
autoresizeOnLoad: true,
forceClientSorting: true,
shrinkToFit: true,
navOptions: {
edit: false,
add: false,
del: false,
search: false
},
inlineEditing: {keys: true, defaultFocusField: "DrugGroupName", focusField: "DrugGroupName"},
onSelectRow: function (rowid, status, e) {
var $self = $(this), savedRow = $self.jqGrid("getGridParam", "savedRow");
if (savedRow.length > 0 && savedRow[0].id !== rowid) {
$self.jqGrid("restoreRow", savedRow[0].id);
}
$self.jqGrid("editRow", rowid, {focusField: e.target});
}
}).jqGrid('navGrid', pagerSelector, {
search: false,
edit: false,
add: false,
del: false,
refresh: true,
cloneToTop: true
}).jqGrid("filterToolbar", {
stringResult: true, searchOnEnter: false, defaultSearch: 'cn'
}).jqGrid("gridResize").jqGrid('setFrozenColumns');
customAddButton({
caption: 'Delete selected',
buttonicon: 'fa-trash-o',
title: "Delete all selected rows",
onClickButton: function () {
var rowIds = $("#list").jqGrid('getGridParam', 'selarrrow');
if (rowIds.length > 0) {
delete_all_link_modal.modal();
delete_all_link_modal.attr('data-link-ids', rowIds);
} else {
alert('You must select at least one item.');
}
}
});
});
The following line enables the inline editing:
inlineEditing: {keys: true, defaultFocusField: "DrugGroupName", focusField: "DrugGroupName"}
Where is my problem? I need to edit only the column DrugGroupName
and the line above make the entire row editable which leads me to the following questions:
It's possible to edit only a given set of columns instead of all of them? - I was checking docs here but I could not find anything helpfulIt's possible to send the data to the server as soon as I click in any other place or by hitting the ENTER key? - I want to avoid the extra click on the save icon.
UPDATE: I have found the answer for my first question already. I just need to make the column not editable while defining the colModel
. Ex:
colModel: [
{name: "act", formatter: "customActionsFormatter", width: 20, search: false},
{name: "Id", jsonmap: "Id", key: true, hidden: true},
{name: "FormId", align: 'center', fixed: true, frozen: true, resizable: false, width: 100, editable: false},
{name: "FormName", width: 300, editable: false},
{name: "PlanName", width: 300, editable: false},
{
name: "DrugGroupName",
width: 300,
edittype: "select",
editoptions: {
generateValue: true,
selectFilled: function (options) {
setTimeout(function () {
$(options.elem).select2({
width: "100%"
});
}, 0);
}
},
stype: "select", searchoptions: {
sopt: ["eq", "ne"],
generateValue: true,
noFilterText: "Any",
selectFilled: function (options) {
$(options.elem).select2({
width: "100%"
});
}
}
},
{name: "DrugName", width: 300, editable: false}
]
That way I am forcing DrugGroupName
to be the only one editable.