新行中的数据表对象 json (ajax)
Datatables objects in a new row with json (ajax)
到目前为止,我已经能够毫无问题地使用数据表显示 ajax 来自 json 的数据。
但是今天我运行陷入了这个问题。 Json 具有以下结构:
{
"aaData": [
{
"id": 22,
"name": "flavor 22",
"flavorItem": [
{
"hostSuffix": "google"
},
{
"hostSuffix": "yahoo"
},
{
"hostSuffix": "google"
},
{
"hostSuffix": "yahoo"
},
{
"hostSuffix": "google"
},
{
"hostSuffix": "yahoo"
},
{
"hostSuffix": "google"
},
{
"hostSuffix": "yahoo"
},
{
"hostSuffix": "google"
},
{
"hostSuffix": "yahoo"
},
{
"hostSuffix": "google"
},
{
"hostSuffix": "yahoo"
},
{
"hostSuffix": "google"
},
{
"hostSuffix": "yahoo"
},
{
"hostSuffix": "google"
},
{
"hostSuffix": "yahoo"
},
{
"hostSuffix": "google"
},
{
"hostSuffix": "yahoo"
},
{
"hostSuffix": "google"
},
{
"hostSuffix": "yahoo"
},
{
"hostSuffix": "google"
},
{
"hostSuffix": "yahoo"
},
{
"hostSuffix": "google"
},
{
"hostSuffix": "yahoo"
}
]
}
]
}
加载到数据表时,这会显示同一行中的所有对象 "hostSuffix"。
如何为每个对象显示新行?
我的脚本:
$("#example").dataTable({
"serverSide": true,
"searching": false,
"aLengthMenu": [[5, 10, 15, -1], [5, 10, 15, "All"]],
"iDisplayLength": 10,
"ajax": "<c:url value='/ajax/selectflavorEditor?id=22'/>",
"columns": [
{"data": "flavorItem[, ].hostSuffix"}
]
});
您必须将 dataSrc
设置为指向嵌套数组 flavorItem
:
$("#example").dataTable({
"serverSide": true,
"searching": false,
"aLengthMenu": [[5, 10, 15, -1], [5, 10, 15, "All"]],
"iDisplayLength": 10,
"ajax": {
url: "<c:url value='/ajax/selectflavorEditor?id=22'/>",
dataSrc: function(json) {
return json['aaData'][0].flavorItem
}
},
"columns": [
{"data": "hostSuffix"}
]
});
到目前为止,我已经能够毫无问题地使用数据表显示 ajax 来自 json 的数据。 但是今天我运行陷入了这个问题。 Json 具有以下结构:
{
"aaData": [
{
"id": 22,
"name": "flavor 22",
"flavorItem": [
{
"hostSuffix": "google"
},
{
"hostSuffix": "yahoo"
},
{
"hostSuffix": "google"
},
{
"hostSuffix": "yahoo"
},
{
"hostSuffix": "google"
},
{
"hostSuffix": "yahoo"
},
{
"hostSuffix": "google"
},
{
"hostSuffix": "yahoo"
},
{
"hostSuffix": "google"
},
{
"hostSuffix": "yahoo"
},
{
"hostSuffix": "google"
},
{
"hostSuffix": "yahoo"
},
{
"hostSuffix": "google"
},
{
"hostSuffix": "yahoo"
},
{
"hostSuffix": "google"
},
{
"hostSuffix": "yahoo"
},
{
"hostSuffix": "google"
},
{
"hostSuffix": "yahoo"
},
{
"hostSuffix": "google"
},
{
"hostSuffix": "yahoo"
},
{
"hostSuffix": "google"
},
{
"hostSuffix": "yahoo"
},
{
"hostSuffix": "google"
},
{
"hostSuffix": "yahoo"
}
]
}
]
}
加载到数据表时,这会显示同一行中的所有对象 "hostSuffix"。 如何为每个对象显示新行?
我的脚本:
$("#example").dataTable({
"serverSide": true,
"searching": false,
"aLengthMenu": [[5, 10, 15, -1], [5, 10, 15, "All"]],
"iDisplayLength": 10,
"ajax": "<c:url value='/ajax/selectflavorEditor?id=22'/>",
"columns": [
{"data": "flavorItem[, ].hostSuffix"}
]
});
您必须将 dataSrc
设置为指向嵌套数组 flavorItem
:
$("#example").dataTable({
"serverSide": true,
"searching": false,
"aLengthMenu": [[5, 10, 15, -1], [5, 10, 15, "All"]],
"iDisplayLength": 10,
"ajax": {
url: "<c:url value='/ajax/selectflavorEditor?id=22'/>",
dataSrc: function(json) {
return json['aaData'][0].flavorItem
}
},
"columns": [
{"data": "hostSuffix"}
]
});