【问题标题】:DataTables: Cannot read property style of undefinedDataTables:无法读取未定义的属性样式
【发布时间】:2017-01-15 13:02:44
【问题描述】:

我收到以下错误:

jquery.dataTables.js:4089 Uncaught TypeError: Cannot read property 'style' of undefined(…)
_fnCalculateColumnWidths @ jquery.dataTables.js:4089
_fnInitialise @ jquery.dataTables.js:3216
(anonymous function) @ jquery.dataTables.js:6457
each @ jquery-2.0.2.min.js:4
each @ jquery-2.0.2.min.js:4
DataTable @ jquery.dataTables.js:5993
$.fn.DataTable @ jquery.dataTables.js:14595
(anonymous function) @ VM3329:1
(anonymous function) @ VM3156:180
l @ jquery-2.0.2.min.js:4
fireWith @ jquery-2.0.2.min.js:4
k @ jquery-2.0.2.min.js:6
(anonymous function) @ jquery-2.0.2.min.js:6

上面提到(匿名函数)@VM3156:180 的那一行是:

                TASKLISTGRID = $("#TASK_LIST_GRID").DataTable({
                    data : response,
                    columns : columns.AdoptionTaskInfo.columns,
                    paging: true
                });

所以我猜这是失败的地方。

HTML ID 元素存在:

  <table id="TASK_LIST_GRID" class="table table-striped table-bordered table-hover dataTable no-footer" width="100%" role="grid" aria-describedby="TASK_LIST_GRID_info">
  <thead>
    <tr role="row">
      <th class="sorting" tabindex="0" aria-controls="TASK_LIST_GRID" rowspan="1" colspan="1">Solution</th>
      <th class="sorting" tabindex="0" aria-controls="TASK_LIST_GRID" rowspan="1" colspan="1">Status</th>
      <th class="sorting" tabindex="0" aria-controls="TASK_LIST_GRID" rowspan="1" colspan="1">Category</th>
      <th class="sorting" tabindex="0" aria-controls="TASK_LIST_GRID" rowspan="1" colspan="1">Type</th>
      <th class="sorting" tabindex="0" aria-controls="TASK_LIST_GRID" rowspan="1" colspan="1">Due Date</th>
      <th class="sorting" tabindex="0" aria-controls="TASK_LIST_GRID" rowspan="1" colspan="1">Create Date</th>
      <th class="sorting" tabindex="0" aria-controls="TASK_LIST_GRID" rowspan="1" colspan="1">Owner</th>
      <th class="sorting" tabindex="0" aria-controls="TASK_LIST_GRID" rowspan="1" colspan="1">Comments</th>
      <th class="sorting" tabindex="0" aria-controls="TASK_LIST_GRID" rowspan="1" colspan="1">Mnemonic</th>
      <th class="sorting" tabindex="0" aria-controls="TASK_LIST_GRID" rowspan="1" colspan="1">Domain</th>
      <th class="sorting" tabindex="0" aria-controls="TASK_LIST_GRID" rowspan="1" colspan="1">Approve</th>
      <th class="sorting" tabindex="0" aria-controls="TASK_LIST_GRID" rowspan="1" colspan="1">Dismiss</th>
    </tr>
  </thead>
  <tbody></tbody>
</table>

此外,还存在 columns.AdoptionTaskInfo.columns 和响应对象数组。不知道如何调试出了什么问题..任何建议都会有所帮助..

【问题讨论】:

  • 在你的代码中寻找.style。您正在尝试访问未定义变量的该属性。您可以从那里进行调试。
  • 检查您尝试获取的列数是否与您创建的 的数量相同。
  • 列数与 javascripts 中定义的列数不匹配导致此问题。

标签: javascript jquery node.js datatables


【解决方案1】:

问题在于

标签的数量需要与配置中的列数(键为“columns”的数组)相匹配。如果 标记比指定的列少,您会收到这条略显神秘的错误消息。

(正确答案已作为评论出现,但我将其作为答案重复,以便更容易找到 - 我没有看到 cmets)

【讨论】:

  • 我想补充一点,在某些情况下人们可能会觉得它有帮助,如果你没有定义 dataSrc 那么在你的 json 中使用“data”,如果你使用另一个名字你会得到这个错误。
  • 此外,如果您的列不可见,但包含用于搜索或编辑器等内容,则它们必须位于列的末尾:[ ] 列表。
  • 非常感谢,我想知道 javascripts 中的问题..
  • 谢谢,我还发现我获取的数据有误,而且文件数比列数少,所以这让我发现了不止一个错误
  • 你需要获得奖励。节省了大量时间。谢谢@ehrencrona
【解决方案2】:

可能的原因

  • 表格页眉或页脚中的th 元素数与表格正文中的列数不同或使用columns 选项定义。
  • 属性 colspan 用于表头中的th 元素。
  • columnDefs.targets 选项中指定的列索引不正确。

解决方案

  • 确保表格页眉或页脚中的th 元素数与columns 选项中定义的列数相匹配。
  • 如果您在表格标题中使用colspan 属性,请确保您至少有两个标题行和每列一个唯一的th 元素。请参阅Complex header 了解更多信息。
  • 如果您使用 columnDefs.targets 选项,请确保从零开始的列索引引用现有列。

链接

更多信息请参见jQuery DataTables: Common JavaScript console errors - TypeError: Cannot read property ‘style’ of undefined。

【讨论】:

  • 非常感谢,列索引的最后解决方案对我有帮助
【解决方案3】:

您说任何建议都会有帮助,所以目前我解决了我的 DataTables“无​​法读取未定义的属性'样式'”的问题,但我的问题基本上是在数据表启动阶段的 columnDefs 部分使用了错误的索引。我有 9 列,索引是 0、1、2、..、8,但我使用的是 9 和 10 的索引,所以在修复错误的索引问题后,故障消失了。我希望这会有所帮助。

简而言之,你必须注意你的列数量和索引是否一致。

错误代码:

    jQuery('#table').DataTable({
        "ajax": {
            url: "something_url",
            type: 'POST'
        },
        "processing": true,
        "serverSide": true,
        "bPaginate": true,
        "sPaginationType": "full_numbers",
        "columns": [
            { "data": "cl1" },
            { "data": "cl2" },
            { "data": "cl3" },
            { "data": "cl4" },
            { "data": "cl5" },
            { "data": "cl6" },
            { "data": "cl7" },
            { "data": "cl8" },
            { "data": "cl9" }
        ],
        columnDefs: [
            { orderable: false, targets: [ 7, 9, 10 ] } //This part was wrong
        ]
    });

固定代码:

    jQuery('#table').DataTable({
        "ajax": {
            url: "something_url",
            type: 'POST'
        },
        "processing": true,
        "serverSide": true,
        "bPaginate": true,
        "sPaginationType": "full_numbers",
        "columns": [
            { "data": "cl1" },
            { "data": "cl2" },
            { "data": "cl3" },
            { "data": "cl4" },
            { "data": "cl5" },
            { "data": "cl6" },
            { "data": "cl7" },
            { "data": "cl8" },
            { "data": "cl9" }
        ],
        columnDefs: [
            { orderable: false, targets: [ 5, 7, 8 ] } //This part is ok now
        ]
    });

【讨论】:

  • 这救了我,谢谢。我使用“aTargets”:[7],所以它会出错,因为不存在索引为 7 的列。legacy.datatables.net/usage/columns
  • 太好了,我很高兴我的回答解决了您的问题,请+投票
【解决方案4】:

大部分情况是表头数和数据单元数不匹配

【讨论】:

    【解决方案5】:

    当我在表头中设置colspan 时遇到了这个问题。所以我的桌子是:

            <thead>
                <tr>
                    <th colspan="2">Expenses</th>
    
                    <th colspan="2">Income</th>
    
                    <th>Profit/Loss</th>
                </tr>
            </thead>
    

    然后一旦我将其更改为:

            <thead>
                <tr>
                    <th>Expenses</th>
                    <th></th>
                    <th>Income</th>
                    <th></th>
                    <th>Profit/Loss</th>
                </tr>
            </thead>
    

    一切正常。

    【讨论】:

      【解决方案6】:

      对我来说,{data: columnName} 的数据列有更多列 比表头&lt;th&gt;Column Name&lt;/th&gt;

      【讨论】:

      • 优秀的我的兄弟!...我的头是 26 和网格列 27
      【解决方案7】:

      确保输入数据中的response[i] 和response[i][j] 不是undefined/null。

      如果是这样,请将它们替换为“”。

      【讨论】:

        【解决方案8】:

        绘制新(其他)表时也会发生这种情况。我通过首先删除上一个表解决了这个问题:

        $("#prod_tabel_ph").remove();

        【讨论】:

          【解决方案9】:

          解决方案非常简单。

            <table id="TASK_LIST_GRID" class="table table-striped table-bordered table-hover dataTable no-footer" width="100%" role="grid" aria-describedby="TASK_LIST_GRID_info">
            <thead>
              <tr role="row">
                <th class="sorting" tabindex="0" aria-controls="TASK_LIST_GRID" rowspan="1" colspan="1">Solution</th>
                <th class="sorting" tabindex="0" aria-controls="TASK_LIST_GRID" rowspan="1" colspan="1">Status</th>
                <th class="sorting" tabindex="0" aria-controls="TASK_LIST_GRID" rowspan="1" colspan="1">Category</th>
                <th class="sorting" tabindex="0" aria-controls="TASK_LIST_GRID" rowspan="1" colspan="1">Type</th>
                <th class="sorting" tabindex="0" aria-controls="TASK_LIST_GRID" rowspan="1" colspan="1">Due Date</th>
                <th class="sorting" tabindex="0" aria-controls="TASK_LIST_GRID" rowspan="1" colspan="1">Create Date</th>
                <th class="sorting" tabindex="0" aria-controls="TASK_LIST_GRID" rowspan="1" colspan="1">Owner</th>
                <th class="sorting" tabindex="0" aria-controls="TASK_LIST_GRID" rowspan="1" colspan="1">Comments</th>
                <th class="sorting" tabindex="0" aria-controls="TASK_LIST_GRID" rowspan="1" colspan="1">Mnemonic</th>
                <th class="sorting" tabindex="0" aria-controls="TASK_LIST_GRID" rowspan="1" colspan="1">Domain</th>
                <th class="sorting" tabindex="0" aria-controls="TASK_LIST_GRID" rowspan="1" colspan="1">Approve</th>
                <th class="sorting" tabindex="0" aria-controls="TASK_LIST_GRID" rowspan="1" colspan="1">Dismiss</th>
              </tr>
            </thead>
            <tbody></tbody>
          </table>

                          TASKLISTGRID = $("#TASK_LIST_GRID").DataTable({
                              data : response,
                              columns : columns.AdoptionTaskInfo.columns,
                              paging: true
                          });
                          
                          //Note: columns : columns.AdoptionTaskInfo.columns has at least a column not definded in the <thead>

          注意:columns : columns.AdoptionTaskInfo.columns 至少有一列未在表头中定义

          【讨论】:

            【解决方案10】:

            有趣的是,我收到以下错误,因为第 1 对/第 1 对太多,但谷歌仍然把我指向这里。我会把它写下来,以便人们找到它。

            jquery.dataTables.min.js:27 Uncaught TypeError: Cannot read property 'className' of undefined
            at ua (jquery.dataTables.min.js:27)
            at HTMLTableElement.<anonymous> (jquery.dataTables.min.js:127)
            at Function.each (jquery.min.js:2)
            at n.fn.init.each (jquery.min.js:2)
            at n.fn.init.j (jquery.dataTables.min.js:116)
            at HTMLDocument.<anonymous> (history:619)
            at i (jquery.min.js:2)
            at Object.fireWith [as resolveWith] (jquery.min.js:2)
            at Function.ready (jquery.min.js:2)
            at HTMLDocument.K (jquery.min.js:2)
            

            【讨论】:

              【解决方案11】:

              这里的答案让我走上了正确的道路,但没有一个给出我需要的完整答案。我使用 destroy: true 作为选项,但在 DataTable 的第二次迭代中,我不断收到原始问题中的错误。

              相反,我必须直接调用destroy() 方法,并清除 HTML 的子实体以强制 DataTables 在每次迭代时正确重绘 DataTable,而不会在 @987654323 之间发生冲突@ 标头和在 columns: 中传递的标头是这样的:

              if ($.fn.DataTable.isDataTable('#results_table')) {
                  $('#results_table').DataTable().destroy();
              }
              $('#results_table thead').empty();
              $('#results_table tbody').empty();
              
              $('#results_table').DataTable({
                  data: data,
                  columns: columns,
                  ...etc...
              });
              

              【讨论】:

              • 纠正我的兄弟!但是 THead 怎么不见了?
              【解决方案12】:

              在我的例子中,我更新了服务器端数据表两次,它给了我这个错误。希望它可以帮助某人。

              【讨论】:

                【解决方案13】:

                我解决了这个错误,通过将src属性替换为https://code.jquery.com/jquery-3.5.1.min.js,问题是由JQuery的slim版本引起的。

                【讨论】:

                  【解决方案14】:

                  我在使用 Jquery Datatable 时遇到了同样的问题。表头中表头(标签)的数量与表体中的标签数量相同。我通过删除名为“scrollX”的 Datatable 属性之一解决了这个问题:true”。然后一切对我来说都很好。

                  【讨论】:

                  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。
                  猜你喜欢
                  相关资源
                  最近更新 更多
                  热门标签