Change Request parameters and refresh datatables

Change Request parameters and refresh datatables

nanobyt3nanobyt3 Posts: 5Questions: 0Answers: 0
edited April 2013 in General
Hi,

I have a datatable object as follows:

[code]
$(document).ready(function() {
$('#dataTable').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "../server_processing.php",
"fnServerData": fnDataTablesPipeline,
"fnRowCallback": fnAfterRow ,
"aLengthMenu": [3, 10, 20, 40, 50, 100],
"aaSorting": [[ 8, "desc" ]],
});
});
[/code]

As you can see, I'm also using server processing for the AJAX to bring the JSON from.


I would like to be able to change some the parameters in the HTTP Request, so that I can bring different data from the server and I need to do this dynamically and "refresh" the table. For example, with the onClick event of a button that has nothing to do with datatables.

At least I would need to be able to change : "sSearch", "iSortCol_0", "sSortDir_0" as well as "iDisplayStart" and iDisplayLength"

This following is just a snippet of the HTTP Request using GET that datatables generates when it loads.

[code]
"?sEcho=1&iColumns=9&sColumns=&iDisplayStart=0&iDisplayLength=10&mDataProp_0=0&mDataProp_1=1&mDataProp_2=2&mDataProp_3=3&mDataProp_4=4&mDataProp_5=5&mDataProp_6=6&mDataProp_7=7&mDataProp_8=8&sSearch=&bRegex=false&sSearch_0=&bRegex_0=false&bSearchable_0=false&sSearch_1=&bRegex_1=false&bSearchable_1=true&sSearch_2=&bRegex_2=false&bSearchable_2=false&sSearch_3=&bRegex_3=false&bSearchable_3=false&sSearch_4=&bRegex_4=false&bSearchable_4=false&sSearch_5=&bRegex_5=false&bSearchable_5=false&sSearch_6=&bRegex_6=false&bSearchable_6=false&sSearch_7=&bRegex_7=false&bSearchable_7=false&sSearch_8=&bRegex_8=false&bSearchable_8=false&iSortCol_0=0&sSortDir_0=asc&iSortingCols=1&bSortable_0=true&bSortable_1=true&bSortable_2=true&bSortable_3=true&bSortable_4=true&bSortable_5=true&bSortable_6=true&bSortable_7=true&bSortable_8=true&_=1366037325412"
[/code]

I haven't been able to manipulate this data and trigger AJAX call and datatable refresh accordingly.

Your help is greatly appreciated,

Thank you in advanced! :)

Replies

  • nanobyt3nanobyt3 Posts: 5Questions: 0Answers: 0
    Also forgot to mention that the "sAjaxSource" parameter remains the same as it was initially:

    "sAjaxSource": "../server_processing.php"

    Just want to modify "filtering" and "sorting" dynamically.

    Thanks!!
  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin
    You can use fnServerParams to modify what is being sent to the server.

    Allan
  • nanobyt3nanobyt3 Posts: 5Questions: 0Answers: 0
    Hi allan,

    Thank you so much for your fast reply. :-) This a great tool and you help so many people here too.

    I understand that I could for example add more parameters as in the example below from the documentation:

    [code]
    "fnServerParams": function ( aoData ) {
    aoData.push( { "name": "more_data", "value": "my_value" } );

    [/code]


    However, I would need to modify the existing ones. I assume I would need to iterate over all the array looking for the ones I need and change the value there directly. is that right? and more importantly how do I do that dynamically, not in the initialization of the datatables object?

    Thanks!
  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin
    > I assume I would need to iterate over all the array looking for the ones I need and change the value there directly. is that right?

    Absolutely correct. Its a bit of a bummer, but it is a legacy thing that I can't really change without breaking all the scripts that are already out there :-(.

    > and more importantly how do I do that dynamically, not in the initialization of the datatables object?

    Remember that the function you give isn't executed in the initialisation, but rather when DataTables makes the request. So it is dynamic in that sense. It is not dynamic in the sense of being able to change the function after initialisation (it is possible not it shouldn't really be done if avoidable, since it would use 'private' parameters).

    Allan
  • nanobyt3nanobyt3 Posts: 5Questions: 0Answers: 0
    I see. wow!!! you really helped me a lot :) I understand that better now.

    I did the following and seems to be working, at least the part of getting and setting the parameters:

    [code]
    function fnSetKey( aoData, sKey, mValue )
    {
    for ( var i=0, iLen=aoData.length ; i
  • nanobyt3nanobyt3 Posts: 5Questions: 0Answers: 0
    The fnReloadajax function is working now. The key was to update the parameters. if they are the same the table doesn't reload which is fantastic.

    Thank you so much!
This discussion has been closed.