jqGrid自定义onCellSelect不会在beforeSaveCell之前触发

我在onCellSelect中写了一段代码,执行得很好

onCellSelect: function (rowid, iCol, cellcontent) { if (iCol > 0) { $("#gridMain_d").jqGrid("resetSelection"); $("#gridMain_d").setSelection(rowid, true); } } 

但问题是因为此代码之前的事件没有被触发。 我知道这个,因为我在之前删除此代码然后才开始工作。 我试过使用return语句,但没有任何作用。

UPDATE
我评论了上面写的代码并添加了这段代码

 beforeSelectRow: function (rowid, e) { var $self = $(this), iCol, cm, $td = $(e.target).closest("tr.jqgrow>td"), $tr = $td.closest("tr.jqgrow"), p = $self.jqGrid("getGridParam"); if ($(e.target).is("input[type=checkbox]") && $td.length > 0) { $self.jqGrid("setSelection", $tr.attr("id"), true, e); } else { $self.jqGrid('resetSelection'); $self.jqGrid("setSelection", $tr.attr("id"), true, e); } return true; }, 

但仍然在之前的事件没有被解雇。

更新2
这个jsFiddle复制了这个问题。 http://jsfiddle.net/eranjali08/CzVVK/1175/

有许多回调相互依赖。 此外,它可能是jqGrid的不同版本中的这种依赖性的差异。 我建议你使用onCellSelect而不是onCellSelect因为它将是第一个在点击jqGrid单元格时调用的回调。 您可能需要的所有信息都可以从beforeSelectRow的第二个参数(下面的代码中的e )获得:

 beforeSelectRow: function (rowid, e) { var $self = $(this), $td = $(e.target).closest("tr.jqgrow>td"); iCol = $.jgrid.getCellIndex($(e.target).closest($td[0]), colModel = $self.jqGrid("getGridParam", "colModel"), columnName = colModel[i].name; //... // one can use here $td.html(), $td.text() to access the content of the cell // columnName is the name of the column which cell was clicked // iCol - the index of the column which cell was clicked return true; // or false to suppress the selection } 

你只需要忘记beforeSelectRow应该返回告知jqGrid是否选择被点击的行的值。 从beforeSelectRow返回的值false"stop"禁止选择单击的行。 所有其他值都允许选择。

更新 :我再次分析了你的代码,我希望我找到了你的问题的原因。 您使用resetSelection ,这在使用单元格编辑时是邪恶的。 查看resetSelection 的最后一行 。

 tpsavedRow = []; 

会销毁包含当前编辑单元格信息的数组。 因此无法保存或恢复单元格。

要解决此问题,您必须从代码中删除resetSelection 。 如果你真的需要使用resetSelection你应该将它替换为例如调用setSelection的循环。 相应的代码可能看起来接近下面的代码:

 beforeSelectRow: function (rowid, e) { var $self = $(this), iCol, cm, i, idsOfSelectedRows, $td = $(e.target).closest("tr.jqgrow>td"), $tr = $td.closest("tr.jqgrow"), p = $self.jqGrid("getGridParam"); if ($(e.target).is("input[type=checkbox]") && $td.length > 0) { $self.jqGrid("setSelection", $tr.attr("id"), true, e); } else { //$self.jqGrid('resetSelection'); idsOfSelectedRows = p.selarrrow.slice(0); // make copy of the array for (i = 0; i < idsOfSelectedRows.length; i++) { $self.jqGrid("setSelection", idsOfSelectedRows[i], false, e); } $self.jqGrid("setSelection", rowid, false, e); } return false; },