DataTables:无法读取未定义的属性“长度”

[英]DataTables: Cannot read property 'length' of undefined


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).

我理解这是一个受欢迎的问题,我已经在Stack Overflow和其他网站(包括datatables网站)上阅读了所有类似的问题。

To clarify, I am using

为了澄清,我正在使用

  • PHP Codeigniter
  • PHP Codeigniter
  • Materliazecss
  • Materliazecss

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

我还确保我正确地收到了JSON数组:

[{"name_en":"hello","phone":"55555555","email":"a.shouman","facebook":"https:\/\/www.facebook.com"},{"name_en":"Mahfouz","phone":"00000000","email":"m.mahfouz","facebook":null},{"name_en":"Abdelrahman","phone":"55555555","email":"a.shouman","facebook":"https:\/\/www.facebook.com"},{"name_en":"ayman test","phone":"55554444","email":"a.shouman","facebook":"https:\/\/www.facebook.com"},{"name_en":"Abdelrahman Mossad Shouman Bootstrap","phone":"12345678","email":"a.shouman@mail.com","facebook":"https:\/\/www.facebook.com"},{"name_en":"Abdelrahman Mossad Shouman Bootstrap","phone":"12345678","email":"a.shouman@mail.com","facebook":"https:\/\/www.facebook.co"},{"name_en":"a.sharara","phone":"00000000","email":"a.sharara","facebook":""}]

My HTML table looks like this:

我的HTML表格如下所示:

<table id="customer_table">
     <thead>
         <tr>
            <th>Name</th>
            <th>Contact</th>
            <th>Email</th>
            <th>Facebook</th>
         </tr>
     </thead>
</table>

And here is my document.ready function:

这是我的document.ready功能:

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

The error I am getting is

我得到的错误是

Uncaught TypeError: Cannot read property 'length' of undefined

未捕获的TypeError:无法读取未定义的属性“长度”

5 个解决方案

#1


33  

It's even simpler: juste use dataSrc:'' option in the ajax defintion so dataTable don't expect an object but an array:

它甚至更简单:juste在ajax定义中使用dataSrc:''选项,因此dataTable不期望一个对象而是一个数组:

    $('#pos-table2').DataTable({
                "processing": true,
                "serverSide": true,
                "ajax":{"url":"pos.json","dataSrc":""}
            }
    );

See ajax options

请参阅ajax选项

#2


23  

OK, thanks all for the help.

好的,谢谢大家的帮助。

However the problem was much easier than that.

然而问题要比那容易得多。

All I need to do is to fix my JSON to assign the array, to an attribute called data, as following.

我需要做的就是修复我的JSON以将数组分配给名为data的属性,如下所示。

{
  "data": [{
    "name_en": "hello",
    "phone": "55555555",
    "email": "a.shouman",
    "facebook": "https:\/\/www.facebook.com"
  }, ...]
}

#3


3  

Try as follows the return must be d, not d.data

尝试如下返回必须是d,而不是d.data

 ajax: {
      "url": "xx/xxx/xxx",
      "type": "GET",
      "error": function (e) {
      },
      "dataSrc": function (d) {
         return d
      }
      },

#4


1  

When you have JSON data then the following error appears enter image description here

当您有JSON数据时,会出现以下错误

A better solution is to assign a var data for the local json array object, details see: https://datatables.net/manual/tech-notes/4

更好的解决方案是为本地json数组对象分配var数据,详细信息请参见:https://datatables.net/manual/tech-notes/4

This is helps you to display table contents.

这有助于您显示表格内容。

 $(document).ready(function(){   

        $('#customer_table').DataTable( {
         "aaData": data,

           "aoColumns": [{
                            "mDataProp": "name_en"
                        }, {
                            "mDataProp": "phone"
                        }, {
                            "mDataProp": "email"
                        }, {
                            "mDataProp": "facebook"
                        }]
            });
        });

#5


0  

In my case, i had to assign my json to an attribute called aaData just like in Datatables ajax example which data looked like this.

在我的情况下,我必须将我的json分配给一个名为aaData的属性,就像在Datatables ajax中一样,数据看起来像这样。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2015/12/15/24cd578bee902cf65ec71b5e3d52c5a4.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告