Repopulating a Datatable with new parameters

Repopulating a Datatable with new parameters

arvinsimarvinsim Posts: 20Questions: 1Answers: 0
edited September 2011 in General
I initialised my table like this

[code]
/* POST data to server */
$(document).ready(function() {
$('#example').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "xhr.php",
"fnServerData": function ( sSource, aoData, fnCallback ) {
$.ajax( {
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
"success": fnCallback
} );
}
} );
} );
[/code]

Later on, I want to update the data in the table. How do I do that?

Replies

  • allanallan Posts: 63,531Questions: 1Answers: 10,475 Site admin
    Given that you are using server-side processing, all you need to do is call fnDraw ( http://datatables.net/ref#fnDraw ) which will reload the data from the server.

    Allan
  • arvinsimarvinsim Posts: 20Questions: 1Answers: 0
    Hmmm in that case, how do I add parameters to be posted to the server in fnDraw()? I would want to repopulate the table with data based on some parameters.
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    http://www.datatables.net/forums/discussion/6491/display-only-a-partial-list-from-db#Item_2

    add params to aoData in fnServerData. this callback is called when you fnDraw while using bServerSide
  • allanallan Posts: 63,531Questions: 1Answers: 10,475 Site admin
    If you fancy trying out the 1.8.2 dev version it has a new option called fnServerParams which means you don't need to modify fnServerData, making life that little bit easier.

    So now you can do something like this:

    [code]
    $(document).ready(function() {
    $('#example').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "scripts/server_processing.php",
    "fnServerParams": function ( aoData ) {
    aoData.push( { "name": "more_data", "value": "my_value" } );
    }
    } );
    } );
    [/code]

    Allan
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    very nice. simpler than having to include the ajax call and pass the data to fnCallback when you just want to alter the params.
  • subliminalsubliminal Posts: 6Questions: 0Answers: 0
    Is there an equivalent to fnDraw() that doesn't require that bServerSide be set to true?

    I'd like to use this but still have client side sorting and paging.
  • subliminalsubliminal Posts: 6Questions: 0Answers: 0
    Nevermind I found the information I needed in http://www.datatables.net/forums/discussion/3740/how-to-refresh-ajax-source-when-not-using-server-side-processing/p1

    :)
  • arvinsimarvinsim Posts: 20Questions: 1Answers: 0
    @allan

    That new feature is nice. But isn't the solution the same with fnServerData(that is, you try to redefine the function)?
  • allanallan Posts: 63,531Questions: 1Answers: 10,475 Site admin
    fnServerParams is null by default - DataTables doesn't use that to populate the parameter array, its got its own internal functions to do that. So while yes, you do need define a function, its a lot easier that fnServerData for just adding on a parameter since you don't need to write a full $.ajax call (with error handlers etc).

    Allan
This discussion has been closed.