Need help with Server-Side Processing

Need help with Server-Side Processing

ctdowctdow Posts: 4Questions: 0Answers: 0
edited December 2010 in General
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({

Replies

  • ctdowctdow Posts: 4Questions: 0Answers: 0
    Sorry...I pressed too soon
    [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
  • allanallan Posts: 63,400Questions: 1Answers: 10,452 Site admin
    You do indeed need to do a little conversion at the moment since DataTables will only take the 2D array - so you need to create one (I have been thinking of adding the ability to read an array of objects recently - I think I probably will in a future release).

    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
  • ctdowctdow Posts: 4Questions: 0Answers: 0
    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
This discussion has been closed.