javascript 数据表“未捕获的类型错误:无法读取未定义的属性‘nTr’”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21762872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 21:38:10  来源:igfitidea点击:

dataTables "Uncaught TypeError: Cannot read property 'nTr' of undefined"

javascriptjqueryjquery-datatables

提问by Clement Smith

So, this is the code I have in a post-submit callback from a form using ajaxForm:

所以,这是我在使用 ajaxForm 的表单提交后回调中的代码:

function UpdateFormFinished(response, statusText, xhr, $form) {

    if (response['bResult'] == true) {

        var sTestType = $("#sTestType").val();
        var sTableId = "";

        for (index in response) {

            if (typeof response[index] !== "function" && index != "bResult" && index != "sResult") {

                aRowIdPieces = index.split("_");
                sChar = aRowIdPieces[1];

                iColumn = $("#" + index).prevUntil(index).length;
                sTableId = $("#" + sTestType + "_" + sChar).parent().parent().attr("id");

                oTable = $("#" + sTableId).dataTable();
                oTable.fnUpdate(response[index], $("#ixDataTable").val(), iColumn);
            }
        }
    }

    alert(response['sResult']);

    $("#dialog_test_details").dialog("close");
}

The first line after the if retrieves a value from a hidden input in the form itself. The variable "index" will have a value of "someWord_X", where "X" is an integer. iColumn stores the index of the column/cell I want to update (I am aware the numbering starts at zero, which means that prevUntil returns the index of the column/cell I want to update). Finally, "ixDataTable" is another hidden input on the form and returns an integer (I have verified this using console.log to output that value).

if 之后的第一行从表单本身的隐藏输入中检索值。变量“index”的值为“someWord_X”,其中“X”是一个整数。iColumn 存储我要更新的列/单元格的索引(我知道编号从零开始,这意味着 prevUntil 返回我要更新的列/单元格的索引)。最后,“ixDataTable”是表单上的另一个隐藏输入并返回一个整数(我已经使用 console.log 验证了这一点以输出该值)。

So, from everything I can see, I am passing good values in to fnUpdate, but I am getting the error "Cannot read property 'nTr' of undefined". I have looked at the stack trace and the problem starts at line 6188 in dataTables (v1.9.4), which is this line:

所以,从我能看到的一切来看,我正在将好的值传递给 fnUpdate,但我收到错误“无法读取未定义的属性 'nTr'”。我查看了堆栈跟踪,问题从 dataTables (v1.9.4) 的第 6188 行开始,即这一行:

_fnGetTdNodes( oSettings, iRow )[iColumn].innerHTML = sDisplay;

The stack trace proceeds to line 4661 and I have copied lines 4660 to 4689 here:

堆栈跟踪继续到第 4661 行,我在这里复制了第 4660 行到 4689 行:

            oData = oSettings.aoData[iRow];
            if ( oData.nTr !== null )
            {
                /* get the TD child nodes - taking into account text etc nodes */
                anTds = [];
                nTd = oData.nTr.firstChild;
                while ( nTd )
                {
                    sNodeName = nTd.nodeName.toLowerCase();
                    if ( sNodeName == 'td' || sNodeName == 'th' )
                    {
                        anTds.push( nTd );
                    }
                    nTd = nTd.nextSibling;
                }

                iCorrector = 0;
                for ( iColumn=0, iColumns=oSettings.aoColumns.length ; iColumn<iColumns ; iColumn++ )
                {
                    if ( oSettings.aoColumns[iColumn].bVisible )
                    {
                        anReturn.push( anTds[iColumn-iCorrector] );
                    }
                    else
                    {
                        anReturn.push( oData._anHidden[iColumn] );
                        iCorrector++;
                    }
                }
            }

Any suggestions for how to fix this are greatly appreciated and will be attempted. I have verified the table id is correct and I have also tried incrementing the column index by 1 and neither of these things has had a positive impact. I have also tried passing in:

非常感谢任何有关如何解决此问题的建议,并将予以尝试。我已经验证表 id 是正确的,我还尝试将列索引增加 1,但这些都没有产生积极的影响。我也试过传入:

ixDataTable = sTestType + "_" + sChar;

as the second argument for fnUpdate, but it did not like receiving the id of the row to be updated, either.

作为 fnUpdate 的第二个参数,但它也不喜欢接收要更新的行的 id。

Thank you in advance and hopefully it is a very simple fix.

预先感谢您,希望这是一个非常简单的修复。

采纳答案by Clement Smith

I discovered the problem and it is here:

我发现了问题,它在这里:

oTable.fnUpdate(response[index], $("#ixDataTable").val(), iColumn);

Specifically, it is with the value of $("#ixDataTable").val()because this evaluates to a string. So, I actually have to write the code like this:

具体来说,它的值为 ,$("#ixDataTable").val()因为它的计算结果是一个字符串。所以,我实际上必须像这样编写代码:

oTable.fnUpdate(response[index], parseInt($("#ixDataTable").val()), iColumn);

Hope this helps others who might be encountering this and cannot understand why.

希望这可以帮助可能遇到此问题但无法理解原因的其他人。

回答by David Otto

Since this recited code seems to be outdated, I have another possible bugfix for anyone experiencing this problem. This behaviour simply occurs whenever you define within the options:

由于此引用的代码似乎已过时,因此对于遇到此问题的任何人,我都有另一个可能的错误修复。只要您在选项中定义,就会发生这种行为:

"pageLength": "30" //bad

instead of

代替

"pageLength": 30 //good

DataTables interprets this as 030 for its internal counters, which leads to this error.

DataTables 将其解释为内部计数器的 030,从而导致此错误。