I am using jqGrid 4.4.0 with inline editing. For the sake of this question, my grid has four columns: an ID column (SomeGridRowId), a text column with a jQuery autocomplete (Autocomplete), a single character text column (SingleChar), and a hidden boolean
column (CanEditSingleChar). I need to enable or disable editing of the single character column based on the value of the CanEditSingleChar
column. I've got this working on existing rows using onSelectRow
and setColProp
, but for some reason I cannot get it to behave correctly on newly inserted rows. If I add a new row and select a value from the autocomplete, the SingleChar
column is always not editable. I've stepped through the Javascript using the Chrome and IE developer tools; the column values and properties are getting set properly, but the SingleChar
column's editable
property does not reflect this.
I apologize for the gigantic code snippet, but I don't want to leave anything out.
$("#coolGrid").jqGrid({
url: '@Url.Action("GetCoolGridData")',
postData: {
someId: function () { return $("#someId").val(); },
otherStaticArg: function () { return 1; }
},
datatype: 'json',
mtype: 'POST',
loadonce: true,
cellsubmit: 'clientArray',
editurl: 'clientArray',
scroll: true,
pager: $("#coolGridPager"),
rowNum: 200,
sortname: 'SomeGridRowId',
sortorder: 'asc',
viewrecords: true,
height: '100%',
colNames: ['SomeGridRowId', 'CanEditSingleChar', 'Autocomplete', 'SingleChar'],
colModel: [
{ name: 'SomeGridRowId', index: 'SomeGridRowId', hidden: true },
{ name: 'CanEditSingleChar', index: 'CanEditSingleChar', hidden: true },
{
name: 'Autocomplete',
index: 'Autocomplete',
width: 220,
editable: true,
edittype: 'text',
editoptions: {
dataInit: function (elem) {
$(elem).autocomplete({
source: '@Url.Action("GetSomeGridAutocomplete")',
minLength: 2,
select: function (event, ui) {
var rowId = getRowId($(this));
if (ui.item) {
$("#coolGrid").jqGrid('setCell', rowId, 'CanEditSingleChar', ui.item.CanEditSingleChar, '', '');
$("#coolGrid").jqGrid('setCell', rowId, 'Autocomplete', ui.item.label, '', '');
setSingleCharEditMode(rowId);
}
}
});
}
}
},
{
name: 'SingleChar',
index: 'SingleChar',
width: 10,
editable: true, //default to true, most are editable
edittype: 'text'
}
],
onSelectRow: function (clickedRow) {
if (clickedRow && clickedRow != $.coolGridLastSelectedRow) {
$("#coolGrid").jqGrid('saveRow', $.coolGridSelectedRow, false, 'clientArray');
$.coolGridLastSelectedRow = clickedRow;
}
setSingleCharEditMode($.coolGridLastSelectedRow);
$("#coolGrid").jqGrid('editRow', $.coolGridLastSelectedRow, true);
},
afterInsertRow: function (rowid, rowdata, rowelem) {
if (rowid == 'new_row') { //shown solely for completeness, pretty sure this is not the problem
var newRowIndex = $("#coolGridNewRowIndex").val();
if (!newRowIndex)
newRowIndex = 1;
var newRowId = rowid + "_" + newRowIndex;
$("#new_row").attr('id', newRowId);
newRowIndex++;
$("#coolGrid").val(newRowIndex);
$("#coolGrid").jqGrid('setSelection', newRowId, true);
}
}
});
$("#coolGrid").jqGrid('navGrid', '#coolGridPager',
{
add: false,
del: false,
edit: false,
search: false,
beforeRefresh: function () {
$("#coolGrid").jqGrid('setGridParam', { datatype: 'json' });
}
});
$("#coolGrid").jqGrid('inlineNav', '#coolGridPager',
{
edit: false,
add: true,
addtext: "Add",
save: false,
cancel: false,
restoreAfterSelect: false,
addParams: {
position: 'last',
addRowParams: {
keys: true
}
}
});
And the setSingleCharEditMode
function:
function setSingleCharEditMode(rowid) {
var rowData = $("#coolGrid").jqGrid('getRowData', rowid);
if (rowData.CanEditSingleChar === "false") {
$("#coolGrid").jqGrid('setColProp', 'SingleChar', { editable: false });
} else {
$("#coolGrid").jqGrid('setColProp', 'SingleChar', { editable: true });
}
}
I've tried a whole slew of stuff, rifled through a pile of other SO questions, googled like mad....all to no avail. I've resorted to pulling my hair out. How can I control the editable
property of one column after an autocomplete select
on another column, all on a new row?
EDIT
I've got a working example up here, finally. If you add a row and then select either of the "Atypical *" in the Autocomplete
column, you can reproduce this behavior.