我的JSON同时包含字符串和整数值。 HTML表列与DataTable列匹配。 DataTables数据是通过Ajax加载的。一切似乎都很好,但我遇到了错误
DataTables warning: table id=blackListMailTable - Requested unknown parameter 'Id' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4
HTML:
<div class="col-lg-12">
<h3>All Black List Mails</h3>
<hr />
<br />
<table id="blackListMailTable" class="table table-bordered table-hover">
<thead>
<tr>
<th>ID</th>
<th>Email</th>
<th>Module</th>
<th>Status</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
DataTable代码:
//making the table
var table;
if ($.fn.dataTable.isDataTable('#blackListMailTable')) {
table.ajax.reload();
}
else {
table = $("#blackListMailTable").DataTable({
ajax: {
url: ".....",
method: "GET",
dataSrc: ""
//success: function (data) {
// console.log(data);
//},
},
columns: [
{ data: "Id" },
{ data: "Email" },
{ data: "Module" },
{ data: "Status" }
]
});
}
我通过使用ajax调用的成功选项测试了JSON数据。在浏览器控制台中,我得到了以下JSON:
[
{
"Id":1,
"Email":"***",
"Module":"MailChimp",
"Status":"Unsubscribed"
},
{
"Id":2,
"Email":"***",
"Module":"MailChimp",
"Status":"Unsubscribed"
},
{
"Id":3,
"Email":"***",
"Module":"MailChimp",
"Status":"Unsubscribed"
},
{
"Id":4,
"Email":"***",
"Module":"MailChimp",
"Status":"Unsubscribed"
},
{
"Id":5,
"Email":"***",
"Module":"MailChimp",
"Status":"Unsubscribed"
},
{
"Id":6,
"Email":"***",
"Module":"MailChimp",
"Status":"Unsubscribed"
},
{
"Id":7,
"Email":"***",
"Module":"MailChimp",
"Status":"Bounced"
},
{
"Id":10002,
"Email":"***",
"Module":"MailChimp",
"Status":"Bounced"
},
{
"Id":10003,
"Email":"***",
"Module":"MailChimp",
"Status":"Unsubscribed"
}
]
我尝试使用mData
,但没有运气。我在做什么错了?
答案 0 :(得分:1)
尝试此操作可能是某些字段没有数据
columns: [
{ data: "Id",defaultContent: '' },
{ data: "Email",defaultContent: '' },
{ data: "Module",defaultContent: '' },
{ data: "Status",defaultContent: '' }
];
否则请删除
dataSrc:""
答案 1 :(得分:0)
如果您的jsonResponse包含一些null或空白值,则可以将defaultContent
添加到columns
中。类似于mysql的默认值。
table = $("#blackListMailTable").DataTable({
ajax: {
url: ".....",
method: "GET",
dataSrc: ""
//success: function (data) {
// console.log(data);
//},
},
columns: [
{ data: "Id" , defaultContent: "" },
{ data: "Email", defaultContent: "" },
{ data: "Module", defaultContent: "" },
{ data: "Status", defaultContent: "" }
]
});
我可以看到的另一个问题是: 就像您以前使用的
if ($.fn.dataTable.isDataTable('#blackListMailTable')) {
table.ajax.reload();
}
此功能检查
如果#blackListMailTable是数据表。 “如果是,请初始化:”
因此,它将引发重新初始化错误。解决在您的if中添加!
没有条件的情况。
if (!$.fn.dataTable.isDataTable('#blackListMailTable')) {
table.ajax.reload();
}
它的意思是:
如果#blackListMailTable是数据表。 “如果否,请初始化: