I am trying to populate a dataTable as follows:
$("#my-datatable").dataTable( {
"sAjaxSource" : "/someURLOnMyServer",
"bDestroy" : true,
"fnServerParams" : function(serverParams) {
serverParams.push(
{
"name" : "widget",
"value" : token
}
);
}
});
And the HTML table it is populating:
<table id="my-datatable">
<thead>
<tr>
<th>Type</th>
<th>Value</th>
<th>ID</th>
<th>Fizz</th>
<th>Buzz</th>
</tr>
</thead>
<tbody></tbody>
</table>
According to Firebug, the JSON coming back from the server is:
[
{
"id":1,
"attributeType":{
"id":1,
"name":"test1",
"tag":"test-type",
"is-dog":false
},
"attributeValue":{
"id":null,
"name":"blah",
"tag":"BLAH"
},
"buzz":1,
"fizz":"53abc"
}
]
But Firebug is throwing the following JavaScript error in its console:
TypeError: aData is undefined
[Break On This Error]
for ( i=0 ; i<aData.length ; i++ ) --> jquery.dataTables.js (line 2541)
Can anyone spot what's going wrong? Either I'm not setting up my dataTable
object correctly, or the JSON coming back doesn't match the "schema" of the HTML table it is trying to populate. Either way, I'm lost. Thanks in advance!
As of v1. 11, DataTables can be initialised without using jQuery through the new DataTable() constructor. This class takes two parameters: A DOMString selector or HTML elements to pick the table(s) from the DOM.
DataTables warns about the use of them by default, as the majority of the time they are not intended for display - for example, rather than showing null in the table, you might want to show Not yet set, or simply an empty string (empty cell). For this, DataTables has a columns. defaultContent option.
This option allows DataTables to create the nodes (rows and cells in the table body) only when they are needed for a draw.
When DataTables' state saving option is enabled ( stateSave ) KeyTable will automatically store the last cell focused in a table and then restore that state when the page is reloaded.
Datatables requires a certain format for results. If you are not using that format, you have to declare everything.
$('#my-datatable').dataTable( {
"sAjaxSource": "/url/here",
"fnServerData": function ( sSource, aoData, fnCallback ) {
aoData.push( { "name": "widget", "value": "token" } );
request = $.ajax({
"dataType": 'json',
"type": "GET",
"url": sSource,
"data": aoData,
"success": fnCallback
});
},
"aoColumns": [
{ "mDataProp": "id"},
{ "mDataProp": "fizz"},
{ "mDataProp": "name"},
{ "mDataProp": "tag"},
{ "mDataProp": "tag"},
{ "mDataProp": "attributeValue.name"},
{ "mDataProp": "attributeValue.tag"},
],
});
This is the format: http://datatables.net/release-datatables/examples/server_side/post.html
Try to enclose your JSON object with aaData
like:
{"aaData" :
[{"id":1,"attributeType":{"id":1,"name":"test1","tag":"test-type","is-dog":false},"attributeValue":{"id":null,"name":"blah","tag":"BLAH"},"buzz":1,"fizz":"53abc"}]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With