数据表:无法读取未定义的属性“长度" [英] DataTables: Cannot read property 'length' of undefined

查看:148
本文介绍了数据表:无法读取未定义的属性“长度"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解这是一个很普遍的问题,并且我已经在Stack Overflow和其他网站(包括datatables网站)上阅读了所有类似的问题.

I understand this a popular issue, and I have read all the similar questions here on Stack Overflow and other sites (including the datatables website).

为了澄清,我正在使用

  • PHP Codeigniter
  • 材料学

我还确保我正确接收了JSON数组:

I have also made sure that I received the JSON array correctly:

[{"name_en":"hello","phone":"55555555"},{"name_en":"hi","phone":"00000000"}]

我的HTML表如下:

<table id="customer_table">
     <thead>
         <tr>
            <th>Name</th>
            <th>Phone</th>
         </tr>
     </thead>
</table>

这是我的document.ready函数:

  $(document).ready(function(){
            //$('#customer_table').DataTable();
            $('#customer_table').DataTable( {
                "ajax": 'json',
                "dataSrc": "",
                 "columns": [
                    { "data": "email" },
                    { "data": "name_en" }
                ]
            });
  });

我得到的错误是

未捕获的TypeError:无法读取未定义的属性'length'

Uncaught TypeError: Cannot read property 'length' of undefined

推荐答案

原因

此错误TypeError: Cannot read property 'length' of undefined通常意味着jQuery DataTables无法在对Ajax请求的响应中找到数据.

CAUSE

This errors TypeError: Cannot read property 'length' of undefined usually means that jQuery DataTables cannot find the data in the response to the Ajax request.

默认情况下,jQuery DataTables期望数据采用以下所示格式之一.发生错误是因为以默认格式以外的其他格式返回了数据.

By default jQuery DataTables expects the data to be in one of the formats shown below. Error occurs because data is returned in the format other than default.

数组数组

{ 
   "data": [
      [
         "Tiger Nixon",
         "System Architect",
         "$320,800",
         "2011/04/25",
         "Edinburgh",
         "5421"
      ]
   ]
}

对象数组

{ 
   "data": [
      {
         "name": "Tiger Nixon",
         "position": "System Architect",
         "salary": "$320,800",
         "start_date": "2011/04/25",
         "office": "Edinburgh",
         "extn": "5421"
      }
   ]
}

解决方案

使用默认格式或使用 ajax.dataSrc 选项来定义包含表数据的数据属性在Ajax响应中(默认为data).

有关更多信息,请参见数据数组位置.

See Data array location for more information.

请参见 jQuery数据表:常见的JavaScript控制台错误,以获取更多详细信息.

See jQuery DataTables: Common JavaScript console errors for more details.

这篇关于数据表:无法读取未定义的属性“长度"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆