DataTables Ajax请求-无法读取未定义的属性“长度”

时间:2019-05-01 20:51:29

标签: javascript ajax datatables

我正在将DataTables与AJAX端点一起使用,当我在站点上初始化数据表时,我不断收到错误消息,提示“无法读取未定义的属性'length'”。我试图使此代码有效,但没有效果

请求的json结果

{
    "data": [
        {
            "id": "28",
            "id_transakcji": "xxxxxxxxxxxxxxxxx",
            "kod": "xxxxxxxxxxxxxxxxxxxxx",
            "id_kodu": "xxx",
            "email": "xxxxxxxxxx@o2.pl",
            "status": "W Toku",
            "xxyy": "10",
            "xxyya": "xxxxxxx",
            "xxaa": "xxxxxxx",
            "xxx": "xxxxxxxxx",
            "data_utworzenia": "2019-05-01 20:51:31"
        },
        {
            ...

该数据表的配置

var mytable = jQuery('#Transactions').dataTable( {
                ajax: {
                    url: '<?php echo admin_url( 'admin-ajax.php' ) ?>',
                    type: 'POST',
                    data: {action:'get_transactions'},
                    dataSrc: ''
                }
                columns: [
                    {"data": "id"},
                    {"data": "id_transakcji"},
                    {"data": "kod"},
                    {"data": "id_kodu"},
                    {"data": "email"},
                    {"data": "status"},
                    {"data": "xxyy"},
                    {"data": "xxyya"},
                    {"data": "xxaa"},
                    {"data": "xxx"},
                    {"data": "data_utworzenia"}
                ]
            } );

表的HTML

 <table id="Transactions" class="display" style="width:100%">  
    <thead>
      <th>ID</th>
      <th>ID Transakcji</th>
      <th>Kod</th>
      <th>ID Kodu</th>
      <th>Email</th>
      <th>status</th>
      <th>xxyy</th>
      <th>xxyya</th>
      <th>xxaa</th>
      <th>xxx</th>
      <th>data_utworzenia</th>

    </thead>
    <tbody>

  </tbody>
  <tfoot>
      <th>ID</th>
      <th>ID Transakcji</th>
      <th>Kod</th>
      <th>ID Kodu</th>
      <th>Email</th>
      <th>status</th>
      <th>xxyy</th>
      <th>xxyya</th>
      <th>xxaa</th>
      <th>xxx</th>
      <th>data_utworzenia</th>

   </tfoot>
</table>

有人知道为什么会这样吗?

谢谢

威克多

1 个答案:

答案 0 :(得分:0)

"pageLength": 10,添加datatables()

var mytable = jQuery('#Transactions').dataTable( {
               "pageLength": 10, //10 just an example
                ajax: {
                    url: '<?php echo admin_url( 'admin-ajax.php' ) ?>',
                    type: 'POST',
                    data: {action:'get_transactions'},
                    dataSrc: ''
                }
                columns: [
                    {"data": "id"},
                    {"data": "id_transakcji"},
                    {"data": "kod"},
                    {"data": "id_kodu"},
                    {"data": "email"},
                    {"data": "status"},
                    {"data": "xxyy"},
                    {"data": "xxyya"},
                    {"data": "xxaa"},
                    {"data": "xxx"},
                    {"data": "data_utworzenia"}
                ]
} );