mData for JSON source

mData for JSON source

sdinesh21sdinesh21 Posts: 37Questions: 0Answers: 0
edited July 2013 in DataTables 1.9
Hi,

I am using Datatables 1.9 and have the following JSON source from server
[code]
"aaData": [
[
11443013,
"Trading has officially closed",
"XBRN",
"18-Jul-2013 16:55:08:440"
],
[
11443012,
"Trading has officially closed",
"GITC",
203,
"18-Jul-2013 16:50:04:874"
]
[/code]

I need to access the JSON source to populate the datatable as follows. Since I do not have aaData objects as key/value pair, how do I access the using mData?

Please advise.

Replies

  • allanallan Posts: 63,383Questions: 1Answers: 10,449 Site admin
    Using index values - i.e. `mData: 0` for the first entry in the array.

    Allan
  • sdinesh21sdinesh21 Posts: 37Questions: 0Answers: 0
    Thanks for the response allan. I tried accessing array index values in mData but could not get it to display. I will give further details on the JSON and code. My JSON has 11 columns but I need to display only 6 of them.
    [code]
    "aaData": [
    [
    11443013,
    "Test message 1",
    "XBRN",
    17,
    4,
    1,
    "ABC (XBRN)",
    "18-Jul-2013 16:55:08:440",
    15,
    "Test-A",
    "Test sub"
    ],
    [
    11443012,
    "Test message 2",
    "GITC",
    203,
    4,
    1,
    "BCD (GITC)",
    "18-Jul-2013 16:50:04:874",
    15,
    "Test-B",
    "Test sub"
    ]
    ]
    [/code]

    And, this is how I try to fetch the JSON to populate the datatable.

    [code]
    "aoColumnDefs": [
    { "sTitle": "Time UTC", "aTargets": [ 7 ], "sName": "timeString", "bVisible": true, "bSortable": true},
    { "sTitle": "Type", "aTargets": [ 10 ],"sName": "description", "bVisible": true, "bSortable": true},
    { "sTitle": "Severity", "aTargets": [ 5 ], "sName": "severity", "bVisible": true, "bSortable": true},
    { "sTitle": "Subject", "aTargets": [ 6 ], "sName": "subject", "bVisible": true, "bSortable": true},
    { "sTitle": "User Name", "aTargets": [ 9 ], "sName": "userName", "bVisible": true, "bSortable": true},
    { "sTitle": "Description", "aTargets": [ 1 ], "sName": "message", "bVisible": false, "bSortable": true}
    ],
    [/code]

    Please let me know where am I going wrong. Thanks.
  • sdinesh21sdinesh21 Posts: 37Questions: 0Answers: 0
    I also tried with including mData as follows. It does not work...

    [code]
    "aoColumnDefs": [
    { "sTitle": "Time UTC", "aTargets": [ 7 ], "bVisible": true, "mData" : "7", "bSortable": true},
    { "sTitle": "Type", "aTargets": [ 10 ], "bVisible": true, "mData" : "10", "bSortable": true},
    { "sTitle": "Severity", "aTargets": [ 5 ], "bVisible": true, "mData" : "5", "bSortable": true},
    { "sTitle": "Subject", "aTargets": [ 6 ], "bVisible": true, "mData" : "6", "bSortable": true},
    { "sTitle": "User Name", "aTargets": [ 9 ], "bVisible": true, "mData" : "9", "bSortable": true},
    { "sTitle": "Description", "aTargets": [ 1 ], "bVisible": false, "mData" : "1", "bSortable": true}
    ],
    [/code]
  • allanallan Posts: 63,383Questions: 1Answers: 10,449 Site admin
    I'd suggest using aoColumns rather than aoColumnDefs in this case and don't use aTargets (the use of aTargets above suggests there are 11 columns int he table, but you not that there are only 6).

    [code]
    "aoColumns": [
    { "sTitle": "Time UTC", "bVisible": true, "mData" : "7", "bSortable": true},
    { "sTitle": "Type", "bVisible": true, "mData" : "10", "bSortable": true},
    { "sTitle": "Severity", "bVisible": true, "mData" : "5", "bSortable": true},
    { "sTitle": "Subject", "bVisible": true, "mData" : "6", "bSortable": true},
    { "sTitle": "User Name", "bVisible": true, "mData" : "9", "bSortable": true},
    { "sTitle": "Description", "bVisible": false, "mData" : "1", "bSortable": true}
    ],
    [/code]

    Allan
  • sdinesh21sdinesh21 Posts: 37Questions: 0Answers: 0
    Thanks, Allan! It works fine now.
    I have posted another query in the following link. Can you please check that and help when you find time? Thanks.

    http://datatables.net/forums/discussion/16491/column-data-wrapping-with-dynamic-number-of-columns#Item_2
This discussion has been closed.