JSON Object as Data

JSON Object as Data

tyrsiustyrsius Posts: 3Questions: 0Answers: 0
edited March 2012 in General
I am trying to get DataTables to work with a JSON object (not from ajax), but it only results in an empty table.

The json data looks like this:

[code]
[{"PartNumber":"FDD2512","VdsMax":150,"VgsMax":20,"Package":"TO-252","RdsOnType":0,"EntityName":"FDD2512"},
{"PartNumber":"BS270","VdsMax":60,"VgsMax":20,"Package":"TO-92","RdsOnType":1,"EntityName":"BS270"},
{"PartNumber":"NDS356P","VdsMax":-30,"VgsMax":20,"Package":"SSOT-3","RdsOnType":0,"EntityName":"NDS356p"}]
[/code]


And the init looks like this
[code]
$('#tableUpperMosfet').dataTable({
'iDisplayLength': 10,
'sPaginationType': 'full_numbers',
"aData": mosfets,
"aoColumns": [
{ "mDataProp": "PartNumber" },
{ "mDataProp": "VdsMax" },
{ "mDataProp": "VgsMax" },
{ "mDataProp": "RdsOnType" },
{ "mDataProp": "Package" },
{ "mDataProp": "EntityName" }
]
});
[/code]

i have also tried wrapping the JSON in an object so that it was mosfets = { "aaData": mosfets};, but this didn't work either. Is there a right way to do this, or does the JSON object need to be flat (array instead of key/value pair)?

Replies

  • merihmerih Posts: 24Questions: 0Answers: 0
    edited March 2012
    Hi trysius,

    Add "sAjaxDataProp" property in your datatable function using aoColumns.

    [code]
    $('#tableUpperMosfet').dataTable({
    'iDisplayLength': 10,
    'sPaginationType': 'full_numbers',
    "aData": mosfets,
    "sAjaxDataProp": "",

    "aoColumns": [
    { "mDataProp": "PartNumber" },
    { "mDataProp": "VdsMax" },
    { "mDataProp": "VgsMax" },
    { "mDataProp": "RdsOnType" },
    { "mDataProp": "Package" },
    { "mDataProp": "EntityName" }
    ]
    });
    [/code]
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    @tyrsius: Just change "aData" in your initialisation to aaData and that should do it. The rest all looks fine.

    @merih: The sAjaxDataProp option will only have an effect when using Ajax loading of data.

    Allan
  • tyrsiustyrsius Posts: 3Questions: 0Answers: 0
    @allen

    I tried aaData (though I swear I tried it before and it failed), and I got data in the table. However, there are no column names. The sorting still works, but the headers are all missing.
  • tyrsiustyrsius Posts: 3Questions: 0Answers: 0
    @allen

    Nevermind, I just needed to add the sTitle property to each column (duh). Problem Solved.
This discussion has been closed.