如何在jQuery中将嵌套的JSON数据设置为datatable

时间:2017-03-02 10:58:55

标签: jquery json

我的JSON数据看起来像这样,我试图通过数据表发送它。

{"data":[{"code":"001","name":"TEST",
"drpList":[{"name":null,"bed":"xxx","mbed":null},
{"name":null,"bed":"eeee","mbed":null}]}]}

我无法弄清楚如何看待传递drpList等并直接获取数据。这就是我到目前为止所拥有的。有人认为他们可以帮忙吗?

$('#table').DataTable({
        "ajax": url,                                                                        
        "bDestroy": true,                                                                       
        "columns": [
            {"data": "code"},                                                               
            {"data": "name"},
            {"data": "drpList.name"},
            {"data": "drpList.bed"},                                                                                        
            {"data": "drpList.mbed"},
            {"data": null,"defaultContent":editview}
        ],
        "language": {
            "lengthMenu": "| View _MENU_ records per page",
            "zeroRecords": "Nothing found - sorry",
            "infoEmpty": "No records available",
            "infoFiltered": "(filtered from _MAX_ total records)"
        },
        "pagingType": "full_numbers",
        "lengthChange": false 
});

1 个答案:

答案 0 :(得分:0)

首先你需要看到传递drplist数组的json,这意味着你可以使用第一个数组的drplist.0.columnname来访问它,就像从drplist中那样。

对于嵌套列表,您需要更喜欢可能对您有帮助的链接https://datatables.net/examples/ajax/deep.html

我找到答案..当你想要打印所有数据时,你可以使用分米并打印数组的所有数据... (使用下面的代码)

获取数组dataobjectofarray[delimeter ].columnname的所有值

$('#table').DataTable({
        "processing": true,
        "serverSide": true,
        "ajax": "Hello.php",
                    "bDestroy":true,                                                                        
                     "columns":[
                        {"data": "code"},                                                               
                        {"data": "name"},
                        {"data": "drpList[| ].name"},
                        {"data": "drpList[, ].bed"},                                                                                        
                        {"data": "drpList[, ].mbed"}
                        ],
                        "language": {
                        "lengthMenu": "| View _MENU_ records per page",
                        "zeroRecords": "Nothing found - sorry",
                        "infoEmpty": "No records available",
                        "infoFiltered": "(filtered from _MAX_ total records)"
                        },
                        "pagingType": "full_numbers",
                        "lengthChange": false 
    });