looping through complex json object to display data in datatables

looping through complex json object to display data in datatables

vinayak1592vinayak1592 Posts: 1Questions: 0Answers: 0
edited January 2014 in General
I have a complex json object which looks like the following

[code]
[{"domainName":"User","serverList":[{"serverName":"s21","serverPort":"8080","serverDomain":"user","serverHostName":"10.1.25.6","serverUpTime":"23.45.00"},{"serverName":"s21","serverPort":"8080","serverDomain":"user","serverHostName":"10.1.25.6","serverUpTime":"23.45.00"},{"serverName":"s23","serverPort":"8080","serverDomain":"user","serverHostName":"10.1.25.6","serverUpTime":"23.45.00"},{"serverName":"s24","serverPort":"8080","serverDomain":"user","serverHostName":"10.1.25.6","serverUpTime":"23.45.00"},{"serverName":"s25","serverPort":"8080","serverDomain":"user","serverHostName":"10.1.25.6","serverUpTime":"23.45.00"}]},

{"domainName":"mail","serverList":[{"serverName":"s1","serverPort":"8080","serverDomain":"mail","serverHostName":"10.1.25.3","serverUpTime":"23.45.00"},{"serverName":"s2","serverPort":"8080","serverDomain":"mail","serverHostName":"10.1.25.4","serverUpTime":"23.45.00"},{"serverName":"s3","serverPort":"8080","serverDomain":"mail","serverHostName":"10.1.25.5","serverUpTime":"23.45.00"},{"serverName":"s4","serverPort":"8080","serverDomain":"mail","serverHostName":"10.1.25.6","serverUpTime":"23.45.00"},{"serverName":"s5","serverPort":"8080","serverDomain":"mail","serverHostName":"10.1.25.6","serverUpTime":"23.45.00"}]},

{"domainName":"voice","serverList":[{"serverName":"s16","serverPort":"8080","serverDomain":"voice","serverHostName":"10.1.25.6","serverUpTime":"23.45.00"},{"serverName":"s17","serverPort":"8080","serverDomain":"voice","serverHostName":"10.1.25.6","serverUpTime":"23.45.00"},{"serverName":"s18","serverPort":"8080","serverDomain":"voice","serverHostName":"10.1.25.6","serverUpTime":"23.45.00"},{"serverName":"s19","serverPort":"8080","serverDomain":"voice","serverHostName":"10.1.25.6","serverUpTime":"23.45.00"},{"serverName":"s20","serverPort":"8080","serverDomain":"voice","serverHostName":"10.1.25.6","serverUpTime":"23.45.00"}]},

{"domainName":"web","serverList":[{"serverName":"s11","serverPort":"8080","serverDomain":"web","serverHostName":"10.1.25.6","serverUpTime":"23.45.00"},{"serverName":"s12","serverPort":"8080","serverDomain":"web","serverHostName":"10.1.25.6","serverUpTime":"23.45.00"},{"serverName":"s13","serverPort":"8080","serverDomain":"web","serverHostName":"10.1.25.6","serverUpTime":"23.45.00"},{"serverName":"s14","serverPort":"8080","serverDomain":"web","serverHostName":"10.1.25.6","serverUpTime":"23.45.00"},{"serverName":"s15","serverPort":"8080","serverDomain":"web","serverHostName":"10.1.25.6","serverUpTime":"23.45.00"}]},

{"domainName":"default","serverList":[{"serverName":"s6","serverPort":"8080","serverDomain":"default","serverHostName":"10.1.25.6","serverUpTime":"23.45.00"},{"serverName":"s7","serverPort":"8080","serverDomain":"default","serverHostName":"10.1.25.6","serverUpTime":"23.45.00"},{"serverName":"s8","serverPort":"8080","serverDomain":"default","serverHostName":"10.1.25.6","serverUpTime":"23.45.00"},{"serverName":"s9","serverPort":"8080","serverDomain":"default","serverHostName":"10.1.25.6","serverUpTime":"23.45.00"},{"serverName":"s10","serverPort":"8080","serverDomain":"default","serverHostName":"10.1.25.6","serverUpTime":"23.45.00"}]}]

[/code]

i'm using the following javascript to display the first object, which is itself an array of objects

[code]

function Response(result)
{

if(result!="[]")
{
console.log(result[1].serverList.serverName);
$('#example').dataTable( {
"bPaginate":false,
"bLengthchange":false,
"bFilter":false,
"bSort":true,
"bIntro":false,
"bAutoWidth":false,
"bProcessing": true,
"aaData":result[0].serverList,
"aoColumns": [
{ "mData": "serverName" },
{ "mData": "serverPort" },
{ "mData": "serverDomain" },
{ "mData": "serverHostName" },
{ "mData": "serverUpTime" }
],

} );


}
}
[/code]

now i'm able to display only the first object using "aaData":result[0].serverList.
How do i display all the other objects ?

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    edited January 2014
    You could do:

    [code]
    mData: 'serverList[, ].serverName'
    [/code]

    etc.

    That will show the `serverName` parameter from the inner array objects as a comma separated list in the DataTable. Obviously you can use the other properties for the other objects.

    Pass in `result` , rather than `result[0]` to display all records.

    If you want something more complex that a simple joined list (comma - space in the above example) you would need to use mRender as a function. See the mRender documentation for more information on how to do that.

    Regards,
    Allan
This discussion has been closed.