如何使用DataTables重新加载表数据,Ajax call& Json的回应?

时间:2018-04-26 09:55:46

标签: javascript json ajax datatables reload

我有一张通过Thymeleaf生成的表格。我想使用jQuery刷新表的内容。

    <table class="table table-hover" id="main-table">
        <thead class="thead-inverse">
            <tr>
                <th class="col-md-2 text-center">Network Id</th>
                <th class="col-md-2 text-center">Rep date</th>
                <th class="col-md-2 text-center">Hashrate [KH/s]</th>
            </tr>
        </thead>
        <tbody>
            <tr th:each="networkHashrate : ${networkHashrates}" th:onclick="'javascript:openPoolModal(\''+ ${networkHashrate.id} + '\');'">
                <td class="text-center" id="hashrateId" th:text="${networkHashrate.id}"> Sample id</td>
                <td class="text-center" id="repDate" th:text="${@findAndDisplayDataService.formatDate(networkHashrate.repDate)}">Sample rep-date</td>
                <td class="text-center" id="hashrate" th:text="${@findAndDisplayDataService.formatHashrate(networkHashrate.hashrate)}">Sample hashrate</td>
            </tr>
        </tbody>
    </table>

我已经提出了这样的功能来每8秒更新一次表格内容:

$(document).ready(function() {
var table = $('#main-table').DataTable({
           ajax: {
                url: '/refresh',
                dataSrc:''

            },
           paging: true,
           lengthChange: false,
           pageLength: 20,
           stateSave: true,
           info: true,
           searching: false,
           "aoColumns": [
             { "orderSequence": [ "asc", "desc" ] },
             { "orderSequence": [ "asc", "desc" ] },
             { "orderSequence": [ "desc", "asc" ] }
           ],
           "order": [[ 0, "asc" ]]
});


setInterval(function(){
table.ajax.reload();
}, 8000);
});

这是JSON的回应:

[{  
  "id":1,
  "repDate":{  
     "offset":{  },
     "nano":880042000,
     "year":2018,
     "monthValue":4,
     "dayOfMonth":25,
     "hour":12,
     "minute":58,
     "second":53,
     "month":"APRIL",
     "dayOfWeek":"WEDNESDAY",
     "dayOfYear":115
  },
  "hashrate":5114926.0
},...more entries
]

打印一张空表,我不断收到错误:

Uncaught TypeError: Cannot read property 'reload' of undefined

还有一个警告弹出窗口说:

Data Tables warning: table id=main-table - Requestem unknown parameter '0' for row 0 column 0. For more information about this error, please see: http://datatables.net/tn/4

编辑

我将表声明移到了函数之外。现在我一直在收到警告。

编辑2

我按照您的说法做了,数据一直在刷新,但仍然存在一些问题。

首先,我的stateSave: true属性停止工作,所以当重新加载表格时,它总会返回到第一页。
其次,我丢失了原本位于class="text:center"文件中的所有样式(例如on:click)和html属性。

1 个答案:

答案 0 :(得分:1)

尝试在$(document).ready:

之前声明表
var table;
$(document).ready(function() {
  table = $('#main-table').DataTable({"serverSide": true, ...})
  setInterval(function(){
     table.ajax.reload();
   }, 8000);
})

错误与列定义有关,请尝试这种方式来定义列:

 "columnDefs": [
                    {
                        "targets": 0,
                        "data": "id",
                    },
                    {
                        "targets": 1,
                        "data": "repDate",
                    },
                    {
                        "targets": 2,
                        "data": "repDate",
                    }
                ]