简体   繁体   English

将嵌套的JSON对象发送到数据表

[英]Sending nested JSON Objects to datatables

My JSON data looks like this and I am trying to get it sent through datatables. 我的JSON数据看起来像这样,我正在尝试通过数据表发送它。

{
    "RANDOM-UNIQUE-STRING-1":
    {
        "column1": "stuff",
        "column2": "more stuff",
        "column3": "example"
    },
    "RANDOM-UNIQUE-STRING-2":
    {
        "column1": "stuff",
        "column2": "more stuff",
        "column3": "example"
    },
    { ... }
}

I can't figure out how to look passed RANDOM-UNIQUE-STRING-1 , etc and go straight for the data. 我不知道如何看待传递的RANDOM-UNIQUE-STRING-1等,直接进入数据。 This is what I have so far. 到目前为止,这就是我所拥有的。 Anyone think they could help? 有人认为他们可以提供帮助吗?

var theTable = $('#mytable').dataTable({
    "bProcessing": true,
    "aaData": data, //data == my above JSON object
    "aoColumns": [
        { "mData": "column1" },
        { "mData": "column2" },
        { "mData": "column3" }
    ]
});

You could make a function that "normalizes" the JSON. 您可以创建一个“标准化” JSON的函数。 Like this : 像这样 :

function normalize(data) {
    var result = [];
    for (var row in data) {
        result.push(data[row]);
    }
    return result;
}

and then call that function in your dataTables initialisation : 然后在您的dataTables初始化中调用该函数:

var theTable = $('#mytable').dataTable({
    aaData: normalize(data),
    aoColumns: [
        { mData: "column1" },
        { mData: "column2" },
        { mData: "column3" }
    ]    
});

demo -> http://jsfiddle.net/o27hgzjr/ 演示-> http://jsfiddle.net/o27hgzjr/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM