Need help with Server-Side Processing
Need help with Server-Side Processing
I recently replaced my result table with datatable. The plug-in is great, but I immediately ran into the issue with returning large amounts of data. The backend is stable and being used by another interfaces that hasn't converted to datatable yet so I can not change the JSON parameters being returned. My response objects looks something like so:
{"totalItems": 33000, "pageSize": 25, "pageNum": 0, "sortDir": "asc", results: [{data_row1_col1, data_row1_col2, data_row1_col3}, {data_row2_col1, data_row2_col2, data_row2_col3}, {data_row3_col1, data_row3_col2, data_row3_col3}, {data_row4_col1, data_row4_col2, data_row4_col3}]}
How so I handle the results to provide datatables with the "aaData" paramater that it's looking for? Currently my code looks like so:
jQuery("#myresulttable").dataTable({
{"totalItems": 33000, "pageSize": 25, "pageNum": 0, "sortDir": "asc", results: [{data_row1_col1, data_row1_col2, data_row1_col3}, {data_row2_col1, data_row2_col2, data_row2_col3}, {data_row3_col1, data_row3_col2, data_row3_col3}, {data_row4_col1, data_row4_col2, data_row4_col3}]}
How so I handle the results to provide datatables with the "aaData" paramater that it's looking for? Currently my code looks like so:
jQuery("#myresulttable").dataTable({
This discussion has been closed.
Replies
[code]
jQuery("#myresulttable").dataTable({
"bServerSide": true,
"sAjaxSource": "myUrl/getData"});
[/code]
The url actually gets the data, but I'm not sure how to handle it once it's returned. How do I convert the parameters that my json objects to the ones that dataTable is expecting, such as aaData, iTotalRecords, etc.? I hope this is clear. I really need to figure out how to make server processing work because I LOVE all the other features dataTables provide
What to do is use http://datatables.net/usage/callbacks#fnServerData (like this http://datatables.net/examples/server_side/post.html ) and in the callback loop over your array of objects to create a 2D array and then pass that to DataTables.
Allan
Thanks so much for your help. I've changed my code to reflect the example presented: It's looks something like this now:
jQuery("#myresulttable").dataTable({
"sAjaxSource": "myUrl/getData"});
"fnServerData": function ( sSource, aoData, fnCallback ) {
$.ajax( {
"dataType": 'json',
"url": sSource,
"success": fnCallback
} );
}
} );
The call is made and the json object returns but it doesn't get into my callback method. Initially, I had the callback method name, "displayMyData" as the call back. That does nothing. Not sure it even got into the method. Then I simple added the method name as the param for "fnServerData" like so..."fnServerData": displayMyData. That actually goes into the method but does not pass the response data. For this to work, does it have to be POST? Does the callback method have to be called "fnCallback" to override the datatable method?
I'm sorry if this is confusing or if it's my lack of jQuery knowledge confusing me, but if you have any suggestions...I'd greatly appreciate. Thanks a bunch.
Candice