javascript - 数据表-未捕获的类型错误 : Cannot read property 'length' of undefined

标签 javascript json datatables

我已经看到了这个问题的几个例子,但仍然无法找到解决方案。

错误表明它在 jquery.dataTables.js(版本 1.10.4)的第 3287 行中断,如下所示

// Got the data - add it to the table
    for ( i=0 ; i<aData.length ; i++ ) {
        _fnAddData( settings, aData[i] );
    }

这是我的 Controller 。 Controller 是这样的,因为现在缺少数据库连接,但将以与 $data 相同的格式返回 JSON。我已经尝试了几种方法来解决错误,但仍然遇到其他问题。 JSON 有效。

public function test()
{
  $data = '{"persons": [{"branch": "CORP","phone_numbers": [{"desk": "5223422117"},{"mobile": "5022319224"},{"branch": "422-922-2291"}],"email": "twilliams@test.com","preferred_name": "Thomas","person_id": 368,"department": "IT","first_name": "Thomas","title": "Programming Manager","last_name": "Williams"}]}';

  $data = json_encode($data);
  echo $data;

}

我的javascript

$(document).ready(function() {
     $('#directory_table').dataTable( {
         "ajax": {
             "url": "test",
             "type": "JSON"
         },
         "aoColumns": [
             { "persons": "preferred_name" },
             { "persons": "last_name" },
             { "persons": "phone_numbers.0" },
             { "persons": "phone_numbers.1" },
             { "persons": "phone_numbers.2" },
             { "persons": "email" },
             { "persons": "department" },
             { "persons": "title" }
         ]
     } );
 } );

我的 HTML

<table id='directory_table' class="display">
    <thead>
        <tr style='background: #186A9F; color: white;'>
            <th>First Name </th>
            <th>Last Name</th>
            <th>Desk Number</th>
            <th>Mobile</th>
            <th>Branch</th>
            <th>Email</th>
            <th>Department</th>
            <th>Title</th>
        </tr>
    <thead>
</table>

最佳答案

原因

默认情况下,DataTables 期望 JSON 响应具有特定结构,请参阅 documentation .

数组的数组:

{
    "data": [
        [ "value1", "value2" ],
        ...
    ]
}

对象数组:

{
    "data": [
        {
           "attr1": "value1",
           "attr2": "value2"
        },
        ...
    ]
}

发生错误是因为您的响应中包含对象数组的数据属性名称 (persons) 与默认值 (data) 不同。

解决方案

使用ajax.dataSrc选项来定义要从数据源对象(即 Ajax 请求返回的对象)读取的属性。

$('#directory_table').dataTable( {
   "ajax": {
      "url": "test",
      "dataSrc": "persons"
   },
   "columns": [
      { "data": "preferred_name" },
      { "data": "last_name" },
      { "data": "phone_numbers.0.desk" },
      { "data": "phone_numbers.1.mobile" },
      { "data": "phone_numbers.2.branch" },
      { "data": "email" },
      { "data": "department" },
      { "data": "title" }
   ]
});

或者,如果您可以将 JSON 响应中的数据属性名称从 persons 更改为 data,则添加 "dataSrc": "persons" 不会'被需要。

演示

参见 this jsFiddle用于代码和演示。

链接

参见 jQuery DataTables: Common JavaScript console errors有关此错误和其他常见控制台错误的更多信息。

关于javascript - 数据表-未捕获的类型错误 : Cannot read property 'length' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29568016/

相关文章:

javascript - 将 Canvas dataURL 导出为 PDF——如何? DataURL 太长,无法传递到服务器

jQuery 访问 JSON 元素

php - 在wordpress中使用现有html表的数据表(使用数据库连接)

javascript - 数据表:如何在顶部获得排序箭头

javascript - select2 通过 jquery 或 js 从下拉列表中触发一个元素

javascript - jQuery 替换/删除没有 id 或标签的属性

javascript - Hammer.js 在点击叠加层并将其移除时将焦点设置在叠加层下方的输入上

java - 如何在 Scala 中从 JSON 解析通用类参数?

javascript - 使用 JavaScript 对象数组中的键值之和进行分组

jquery - 替换行时无法使 jQuery DataTable 中的数据无效