如何修复 Cannot read 属性 'cellIndex' of undefined dragging from an jqgrid column to other in free jqgrid

How to fix Cannot read property 'cellIndex' of undefined dragging from one jqgrid column to other in free jqgrid

免费的 jqgrid 将 beforeSelectRow 事件处理程序定义为

        beforeSelectRow: function (rowid, e) {
            var 
               colName = $.jgrid.getCellIndex($(e.target).closest('td')[0]),
               .... 

如果在一个 jqgrid 列中按住鼠标按钮,鼠标光标将移动到同一行中的其他列并释放鼠标按钮,异常

无法读取未定义的第 566 行 属性 'cellIndex'

出现在第 808 行

               colName = $.jgrid.getCellIndex($(e.target).closest('td')[0]),

下面有堆栈跟踪(今天的行号 jquery.jqgrid.src.js)

如果鼠标光标移动到其他行按住按钮,则不会发生此异常。

如何修复或诊断此问题? 在同一行的列之间拖动时,似乎 $(e.target).closest('td')[0] 未定义。

    Uncaught TypeError: Cannot read property 'cellIndex' of undefined Line 566 Column 12TypeError: Cannot read property 'cellIndex' of undefined  
  at Object.$.extend.getCellIndex (http://localhost:52216/admin/Scripts/jqgrid-4.8.0/js/jquery.jqgrid.src.js:566:12)  
  at HTMLTableElement.$grid.jqGrid.beforeSelectRow (http://localhost:52216/admin/Grid/Index/DoklstlG?_user=admin&_company=1:808:38)    
  at HTMLTableElement.$.extend.fullBoolFeedback (http://localhost:52216/admin/Scripts/jqgrid-4.8.0/js/jquery.jqgrid.src.js:1508:35)  
  at HTMLTableElement.$.extend.feedback (http://localhost:52216/admin/Scripts/jqgrid-4.8.0/js/jquery.jqgrid.src.js:1533:34)  
  at HTMLTableElement.feedback (http://localhost:52216/admin/Scripts/jqgrid-4.8.0/js/jquery.jqgrid.src.js:1618:26)  
  at HTMLTableElement.<anonymous> (http://localhost:52216/admin/Scripts/jqgrid-4.8.0/js/jquery.jqgrid.src.js:4283:55)  
  at HTMLTableElement.jQuery.event.dispatch (http://localhost:52216/admin/Scripts/jquery-1.11.2.js:4665:9) 
   at HTMLTableElement.elemData.handle (http://localhost:52216/admin/Scripts/jquery-1.11.2.js:4333:46)

更新

添加有问题的行后,razor 解析器抛出语法错误。 我尝试使用它,但仍然出现错误。

我使用

解决了这个问题
        beforeSelectRow: function (rowid, e) {
            var iCol, td=$(e.target).closest('td')[0];
            if ( td  == undefined ) {
                return true;
            }
            iCol = $.jgrid.getCellIndex(td);

在我看来,您必须稍微更改一下代码。您应该始终测试您使用 <td> 元素的 DOM 调用 $.jgrid.getCellIndex 而不是使用它的其他一些子元素。 $.jgrid.getCellIndex典型用法的代码片段应该是这样的:

var $td = $(e.target).closest("tr.jqgrow>td");
if ($td.length > 0) {
    var iCol = $.jgrid.getCellIndex($td);
    ...
}