我的问题很简单:如何使用数据表读取对象内部的数组?
对象
我想读取数组“data”:
{
"success": true,
"data": [
{
"id": "4",
"tienda_id": "5",
"tienda_nombre": "sad",
"total": 123,
"logo": null,
"fecha": "2017-04-02T23:00:00.000Z"
}
]
}
数据表:
var x.DataTable({
"ajax" : myAjaxUrl,
"columns": [{
"data": "data.fecha" // this doesn't work
}, {
"data": "data.total" // this doesn't work
}, {
"data": "data.logo" // this doesn't work
}],
//..............
});
谢谢@Sotjin,我知道如何读取 json,这不是问题,问题出在数据列中:
"columns": [{
"data": "data.fecha" // this doesn't work
}, {
"data": "data.total" // this doesn't work
}, {
"data": "data.logo" // this doesn't work
}],
数据表的 ajax 返回对象,然后在列中迭代该对象并显示数据表中的数据 "data": "data.fecha"//这不起作用
例如:
{
"data": [
{
"id": "4",
"tienda_id": "5",
"tienda_nombre": "sad",
"total": 123,
"logo": null,
"fecha": "2017-04-02T23:00:00.000Z"
}]
}
//...
"columns": [{
"data": "data.fecha"
}, {
"data": "data.total"
}, {
"data": "data.logo"
}],
这有效
{
"success": true,
"data": [
{
"id": "4",
"tienda_id": "5",
"tienda_nombre": "sad",
"total": 123,
"logo": null,
"fecha": "2017-04-02T23:00:00.000Z"
}
]
}
这不起作用
最佳答案
var json = {
"success": true,
"data": [
{
"id": "4",
"tienda_id": "5",
"tienda_nombre": "sad",
"total": 123,
"logo": null,
"fecha": "2017-04-02T23:00:00.000Z"
}
]
}
读取data
属性:
json.data[0].fecha
json.data[0].logo
...
如果data
中有多个对象,您可以运行循环对每个对象执行一些操作:
json.data.forEach(function(obj) {
console.log(obj);
});
关于javascript - DataTables 读取对象内的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42580066/