我正在向服务器发送ajax请求以获取数据。当数组为空时,我希望它显示在表行“emptyTable”中。我收到消息“emptyTable”,如果没有使用aoColumnDefs,但我收到警告“请求未知参数......”。我不想收到警告!
function sendAPIRequest (product) {
return new Promise(function (resolve, reject) {
var url = 'http://localhost:3000/api/product_availability?__url_path_param=';
var xhr = createAPIRequest('GET', url + product);
if (!xhr) {
return;
}
xhr.onload = function () {
if (xhr.status == 200) {
resolve(product);
} else {
reject(Error(xhr.statusText));
}
};
xhr.onerror = function () {
reject(Error('Error fetching data...'));
};
xhr.send();
});
}
function formatProductName (product) {
return product.replace(/ /g,"+").toLowerCase();
}
function getProducts () {
var promises = [];
for (var i in products) {
promises.push(sendAPIRequest(formatProductName(products[i].product_name)));
}
Promise.all(promises).then(function (data) {
console.log(data);
}).catch(function (err) {
console.log(err);
});
}
getProducts();
如果我使用aoColumnDefs,我不会收到警告,但是我得到一个没有消息“emptyTable”的空表。
var searchTable = $('#baseTasks').dataTable({
bFilter: false, bInfo: false, "bLengthChange": false,
scrollY:"600px",scrollCollapse: true, paging:false,
"aaData": tasks,
"aoColumns": [
{ "mData": "name"
},{"mData": "schoolClass"
},{"mData": "complexity"
},{"mData": "author"
}]
});
如果aaData是一个空数组,如何获取带有“emptyTable”消息的表?
答案 0 :(得分:0)
您的代码是正确的。运行以下代码进行演示。
var tasks = [
{
"name": "Tiger Nixon",
"schoolClass": "System Architect",
"complexity": "$320,800",
"author": "2011\/04\/25"
},
{
"name": "Garrett Winters",
"schoolClass": "Accountant",
"complexity": "$170,750",
"author": "2011\/07\/25"
}
];
var tasksEmpty = [];
$(document).ready( function () {
var table = $('#example').DataTable({
"data": tasksEmpty,
"columns": [
{ "data": "name"},
{ "data": "schoolClass" },
{ "data": "complexity"},
{ "data": "author" }
],
"searching": false,
"info": false,
"lengthChange": false,
"scrollY":"600px",
"scrollCollapse": true,
"paging":false,
});
});
<script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<link href="//cdn.datatables.net/1.10.7/css/jquery.dataTables.css" rel="stylesheet" />
<script src="//cdn.datatables.net/1.10.7/js/jquery.dataTables.js"></script>
<table id="example" class="display" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
</tr>
</thead>
</table>
Requested unknown parameter
错误的最可能原因是您的数据结构不正确或某些属性(name
,schoolClass
,complexity
或author
)没有找到。
您的数据应具有以下结构。确保每个数组项都具有所需的所有属性。
var tasks = [
{
"name": "Tiger Nixon",
"schoolClass": "System Architect",
"complexity": "$320,800",
"author": "2011\/04\/25"
},
{
"name": "Garrett Winters",
"schoolClass": "Accountant",
"complexity": "$170,750",
"author": "2011\/07\/25"
}
];
另请注意,您的代码使用不同版本的DataTables的参数。虽然较新的DataTables库是向后兼容的,但我已更新您的代码以使用最新版本的初始化参数。有关详细信息,请参阅this migration guide。
答案 1 :(得分:-1)
将以下代码行添加到数据表中,并将邮件设置为sEmptyTable
。
oLanguage: {
"sEmptyTable": "No data found.",
}