【问题标题】:DataTables - Uncaught TypeError: Cannot read property 'length' of undefinedDataTables - 未捕获的类型错误:无法读取未定义的属性“长度”
【发布时间】:2015-04-10 18:06:33
【问题描述】:

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

错误表明它在 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] );
    }

这是我的控制器。控制器是这样的,因为现在缺少 db 连接,但是会以与 $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>

【问题讨论】:

    标签: javascript json datatables


    【解决方案1】:

    原因

    默认情况下,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

    【讨论】:

    • 是的,我这边的 URL 是正确的,我能够运行 ajax,点击我的控制器,并且我的 json 正在通过 DataTables javascript。我一有机会就试试这个。感谢您的反馈。
    猜你喜欢
    • 1970-01-01
    • 2013-11-24
    • 1970-01-01
    • 2021-05-03
    相关资源
    最近更新 更多