【发布时间】:2020-06-14 23:20:46
【问题描述】:
$.ajax({
'url': 'http://localhost:8080/Retail-war/webresources/products/allProducts',
'method': 'GET',
'contentType': 'application/json',
'headers': {"Authorization": 'Bearer ' + sessionStorage.getItem('accessToken')},
}).done( function(data) {
$('#existing-product').DataTable( {
"columnDefs": [
{ "targets": -1, "data": null, "defaultContent": "<button>View More Details!</button>"}
],
"aaData": data,
"columns": [
{ "data": "products.productId"},
{ "data": "products.originalPrice"},
{ "data": "products.currentPrice"},
]
})
这是我的 JSON 文件格式,我想用 products 中的 3 列填充 DataTable,但 DataTable 中没有显示任何内容,也没有引发错误。如何使用 products 对象下 JSON 文件中的列填充每一列?
{
"products": [
{
"productId": 1,
"originalPrice": 60,
"currentPrice": 50
},
{
"productId": 2,
"originalPrice": 80,
"currentPrice": 70
}
]
}
【问题讨论】:
-
只是好奇:您使用的是什么版本的 DataTables?
"aaData": data看起来像 1.10 之前的版本。我希望看到"data": data1.10 及以后的版本。 -
@andrewjames 我目前使用的是最新版本。但是“aaData”和“data”在DataTable中做事的方式有什么区别吗?
-
有什么不同吗...? 不 - 它不应该有所作为 - 向后兼容。但如果您有机会使用新语法,那么这样做可能是个好主意。
-
你能检查你的 JSON 吗?问题中发布的内容无效 - 额外的逗号可能会导致问题。例如:
"currentPrice": 50,无效。 -
@andrewjames 抱歉,JSON 实际上还有其他列,我没有从我在问题中给出的示例中删除“,”。但 JSON 格式正确。
标签: javascript java jquery json datatables