javascript - DataTables 问题 : VM9075 dataTables. min.js:24Uncaught TypeError: 无法设置未定义的属性 '_DT_CellIndex'

标签 javascript datatables

我刚开始使用 DataTables,创建表格时一切正常。

当我在表中显示 5、24、47 行时,DataTables 的行为符合我的预期。

但我有这个大约有 700 行的表,我在 Google Chrome 中收到错误,

"VM9075 dataTables.min.js:24Uncaught TypeError: Cannot set property '_DT_CellIndex' of undefined "

在 IE 9 中,

"SCRIPT5007: Unable to set value of the property '_DT_CellIndex': object is null or undefined 
jquery-1.10.2.min.js, line 4 character 2367"

顺便说一句,我没有两次包含 jQuery。

我不确定如何从这里开始。

我尝试使用未缩小版本的 .js 文件自己调试它,但我一直收到“ext”方法或属性未定义,也无法修复。

感谢任何帮助!

最佳答案

我想通了

最大的问题是不知道这个错误到底意味着什么。 在我的例子中,它意味着“表中作为 <td> 元素子元素的每个 <tr> 元素的数量与作为 <th> 元素子元素的 <thead> 元素的数量不匹配。”

我的表是由服务器生成的,一些 <tr>元素有 27 <td>子元素(它填满了表格的整个宽度,但一些 <tr> 元素只有 3、4 或 5 个,... <td> 子元素不是有效的表格。

我通过添加空 <td> 解决了这个问题我表中的元素为 <tr>缺少正确数量的元素 <td>元素

var makeTableValidObject = {
    thisWasCalled: 0,
    makeTableValid: function() {
        var tableToWorkOn = document.getElementById("table1");      
        //check the number of columns in the <thead> tag
                                                   //thead     //tr        //th      elements
        var numberOfColumnsInHeadTag = tableToWorkOn.children[1].children[0].children.length;
        var numberOf_trElementsToValidate = tableToWorkOn.children[2].children.length;      
        //now go through each <tr> in the <tbody> and see if they all match the length of the thead columns
                       //tbody     //all trs//all tds   elements
         //tableToWorkOn.children[2].children.children);        
        for(var i = 0; i < numberOf_trElementsToValidate; i++) {
            //row my row make sure the columns have the correct number of elements
            var tdColumnArray =  tableToWorkOn.children[2].children[i].children
            var trElementToAppendToIfNeeded = tableToWorkOn.children[2].children[i];
            if(tdColumnArray.length != numberOfColumnsInHeadTag) {
                //since they don't match up, make them valid                
                if(tdColumnArray.length < numberOfColumnsInHeadTag) {
                //add the necessary number of blank <td> tags to the <tr> element to make this <tr> valid
                    var tdColumnArrayLength = tdColumnArray.length;
                    for(var j = 0; j < (numberOfColumnsInHeadTag - tdColumnArrayLength); j++) {
                        var blank_tdElement = document.createElement("td");
                        blank_tdElement.id = "validating_tdId" + i + "_" + j;
                    trElementToAppendToIfNeeded.appendChild(blank_tdElement);           
                    }
                }
                else {
                    //TODO: remove <td> tags to make this <tr> valid if necessary                   
                }
            }
        }
    }
};

编辑 1:

已经有一段时间了,这个问题仍然有很多观点。我已经更新了代码。

为了更通用,我用第二行替换了第一行代码

var numberOfColumnsInHeadTag = tableToWorkOn.children[1].children[0].children.length;

var numberOfColumnsInHeadTag = tableToWorkOn.querySelectorAll('thead')[0].querySelectorAll('th');

在之前的代码中几乎所有你看到 children.children 的地方,我用 querySelectorAll(...) 函数替换了它。

它使用 css 选择器,这使得它非常强大。

保持幸福

关于javascript - DataTables 问题 : VM9075 dataTables. min.js:24Uncaught TypeError: 无法设置未定义的属性 '_DT_CellIndex',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35904708/

相关文章:

javascript - 填充路径取决于绘制顺序

jquery - 如何根据 jQuery 数据表中的列值设置表行的颜色

javascript - jQuery DataTables - 按隐藏列排序日期

javascript - 将工具提示添加到数据表的特定列

javascript - 如何使用 ngx-export-as 中的选项

javascript - 在另一个函数的 if 中调用匿名函数

javascript - 评估错误,黑莓 5.2,Javascript

JavaScript 根据复选框的值打开页面

javascript - HTML javascript 动态调整表格大小

javascript - 如何在点击 meteor 时显示图标?