JSON Object as Data
JSON Object as Data
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)?
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)?
This discussion has been closed.
Replies
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]
@merih: The sAjaxDataProp option will only have an effect when using Ajax loading of data.
Allan
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.
Nevermind, I just needed to add the sTitle property to each column (duh). Problem Solved.