[英]Jquery datatable add rows
I have a datatable in my page initiated like below:我的页面中有一个数据表,如下所示:
var dataSet = [
{ "id": 1, "name": "Bennett Weimann", "email": "jtremblay@example.com" },
{ "id": 2, "name": "Bennett Weimann", "email": "buckridge.orin@example.com" }
];
$(document).ready(function () {
$('#example').DataTable({
data: dataSet,
columns: [
{ data: 'id', title: 'Id' },
{ data: 'name', title: 'Name' },
{ data: 'email', title: 'Email' },
],
});
});
Additinally I have a button which makes an ajax post reques, in the answer I get a json Additinally 我有一个按钮可以发出 ajax 发布请求,在答案中我得到一个 json
[{"id":1,"name":"Bennett Weimann","email":"jtremblay@example.com"},
{"id":2,"name":"Bennett Weimann","email":"buckridge.orin@example.com"}]
If I try to add the response like below, I get an error如果我尝试添加如下响应,则会出现错误
request.done(function (response, textStatus, jqXHR) {
table = $('#example').DataTable();
table.clear();
table.rows.add(response);
table.draw();
});
Error is "DataTables warning: table id=example - Requested unknown parameter 'id' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4"错误是“DataTables 警告:表 id=example - 请求第 0 行第 0 列的未知参数 'id'。有关此错误的更多信息,请参阅http://datatables.net/tn/4”
However if I copy and paste the response manually, it has no issue whatsoever.但是,如果我手动复制并粘贴响应,则没有任何问题。
request.done(function (response, textStatus, jqXHR) {
table = $('#example').DataTable();
table.clear();
table.rows.add([
{"id":1,"name":"Bennett Weimann","email":"jtremblay@example.com"},
{"id":2,"name":"Bennett Weimann","email":"buckridge.orin@example.com"}
]);
table.draw();
});
Any help appriciated what could cause such error.任何帮助都知道可能导致此类错误的原因。
I must have been tired.. after a sleep I could see that I was passing an object instead of an array.我一定很累.. 睡了一觉后,我可以看到我正在传递一个对象而不是一个数组。 I am using php as a backend, if I create an array like
我使用 php 作为后端,如果我创建一个像
$data = array([
"id" => 1,
"name" => "foo",
"email" => "bar"
]);
return $data;
and passing the response it works like charm并传递响应它就像魅力一样
request.done(function(response, textStatus, jqXHR) {
table = $('#example').DataTable();
table.clear();
table.rows.add(response);
table.draw();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.