I am using jqGrid 4.4.0 with inline editing. For the sake of this question, my grid has four columns: an identifier column (SomeGridRowId), a jQuery autocomplete text column, a single-character column (SingleChar) and a boolean
hidden column (CanEditSingleChar), I need to enable or disable editing a single-character column on based on the value of the CanEditSingleChar
column. This works for me on existing rows using onSelectRow
and setColProp
, but for some reason I can't get it to behave correctly on newly inserted rows. If I add a new row and select a value from autocomplete, the SingleChar
column will always be unavailable for editing. I went through Javascript using the Chrome and IE developer tools; column and property values ​​are set correctly, but the SingleChar
column editable
property does not reflect this.
I apologize for the gigantic piece of code, but I do not want to leave anything.
$("#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 tried a whole bunch of things strung on a bunch of other questions similar to SO, and looked like crazy ... all to no avail. I resorted to pulling hair. How can I manage the editable
property of one column after autocomplete select
in another column, all in a new row?
EDIT
Finally, I got a working example here . If you add a row and then select any of the "Atypical *" in the Autocomplete
column, you can reproduce this behavior.