【问题标题】:sorting not working datatable plugin [closed]排序不工作的数据表插件[关闭]
【发布时间】:2016-06-29 08:34:07
【问题描述】:

DataTables Cannot read property 'length' of undefined

我有与上面链接相同的问题。我有一个指向 json 数据的链接。

我的查询:

$('#card-table').DataTable({ "ajax": "http://project-maria-1350.appspot.com/getPeople?key=CX1231466035200" });

我的 HTML 代码:

`<div id="passenger" class="tab-pane fade in active">
  <table id="card-table" class="display" cellspacing="0" width="100%">
  <thead>
        <tr>
            <th>Last Name</th>
            <th>First Name</th>
            <th>Gender</th>
            <th>Nationality</th>
            <th>Age</th>
            <th>Class</th>
            <th>Boarding No.</th>
            <th>Seat No.</th>
            <th>Group Code</th>
        </tr>
    </thead>
  </table>
</div>`

我收到Uncaught TypeError: Cannot read property 'length' of undefined.数据显示。但是排序不起作用。 请帮助我。谢谢。

【问题讨论】:

  • 您来自后端的列数是否与 HTML 中指定的相同?请检查它,因为大多数时候这会导致未定义的长度问题。
  • 您是否在根据任何特定字段进行排序时收到此错误?根据你的代码和数据检查这个 plunker plnkr.co/edit/9ug2unk2kc4NuGLbvW2v?p=preview ,我没有看到任何问题。
  • 不,这根本不是同一个问题。首先你的数据不是 JSON,它是一个字符串数组;此外,您的服务器脚本没有Access-Control-Allow-Origin,因此在基于浏览器的数据表上下文中无用。

标签: javascript jquery datatable


【解决方案1】:

数据表要求 JSON 对象的格式为 {"data": [{..,..},{...,...}]}

检查这个例子https://datatables.net/examples/data_sources/ajax.html (单击“AJAX”选项卡并查看格式)。

你可以改变它。

【讨论】:

  • 谢谢。现在没有错误。但是,仍然排序不起作用。我需要更改我的 JSON 结构。将我的数据转换为 JSON 的任何想法?
  • 好的。您必须向我展示您的后端代码。是带 SQL 的 PHP 吗?
  • 我解决了。谢谢。
猜你喜欢
  • 2013-02-24
  • 2017-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-18
  • 2018-07-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多