到目前为止,我已经能够使用datatables毫无问题地显示来自json的ajax数据。但是今天我遇到了这个问题。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"}
]
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句