DataTables with Server side processing

DataTables with Server side processing

jay.chandranjay.chandran Posts: 10Questions: 0Answers: 0
edited July 2011 in General
Hi,

I am using DataTables 1.7.5 in our Grails application and works perfectly. I am using it with option "bServerSide" : true setting. When the datatable loads the first time and subsequent sorting, paging I can see that DataTable sends lot of parameters which are handled in server side.

Datatables specific server parameters send automatically:
[code]
iSortCol_0:0,
iSortingCols:1,
bSortable_5:false,
bSortable_2:true,
bSortable_1:true,
bSortable_4:false,
sSortDir_0:asc,
bSortable_3:true,
bSortable_0:true,
iDisplayStart:0,
sColumns:,
iColumns:6, _:1311790806393,
iDisplayLength:10,
sEcho:1,
[/code]

This part works fine.

The problem I am facing is that, I have custom search (not the datatable search filter) functionality for searching against linked database tables. When a search is matched, I am calling fnReloadAjax on the datatable as below.

[code]
if (comListTable == null || comListTable == 'undefined') {
// datatable init
} else {
var oSettings = comListTable.fnSettings() ;
oSettings.sAjaxSource = url;
comListTable.fnReloadAjax();
}
[/code]

The problem is none of the previously mentioned datatable server parameters are not being send to the server which are required on the server side. What should I do to set the required parameters? How do I make sure the parameters are send the server automatically?

Thank You.
Jay Chandran

Replies

  • StephanStephan Posts: 20Questions: 0Answers: 0
    edited July 2011
    I am not sure what you mean by saying
    [quote]jay.chandran said: When a search is matched, I am calling fnReloadAjax on the datatable as below.[/quote]
    Do you have something like a search and on submit you want to filter the table depending on this form.input.values?
    In this case you could introduce javascript variables for ech search_input. on form.submit set them to the corresponding input values.
    Add this variables to fnServerData.
    In the "else"-tree of your code simply call comListTable.fnDraw() - a xhr request is fired, fnServerdata adds the extra information of the search.
    But use this only for custom search functionality, not for column filtering.

    [code]
    //your form should set this value on submit!
    var searchValue1 = "initialTestValue";

    oTable = $('#tableDiv').dataTable( {
    "bServerSide": true,
    "bProcessing": true,
    "sAjaxSource": yourServerSideScript.php",
    "fnServerData": function ( sSource, aoData, fnCallback ) {
    aoData.push({"name": "parameterNameForServerSideProcessing", "value": searchValue1});
    $.getJSON( sSource, aoData, function (json){
    fnCallback(json)
    });
    }

    [/code]

    What do you want to do here?
    [quote]jay.chandran said: oSettings.sAjaxSource = url;[/quote]
    May be i got you all wrong and you want to alter your ajaxsource depending on inputs?
  • jay.chandranjay.chandran Posts: 10Questions: 0Answers: 0
    Hi Stephan,

    Yes, I am using Search form and on submit, I want to filter the datatable. So if I use [code] .fnDraw() [/code], along with [code] fnServerData [/code] will datatable send all the proper required datatable parameters?

    [code] oSettings.sAjaxSource = url; [/code] is the server side script.

    I will test and let you know.
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    The best way to add parameters to a call to the database script is by overriding fnServerData, and adding objects (with name and value) to the aoData collection:

    [code]
    "fnServerData": function ( sSource, aoData, fnCallback ) {
    /* Add some extra data to the sender */
    aoData.push( {"name": "daterecv_start", "value": $('#daterecv_start').val() } ); // <-- this
    aoData.push( {"name": "daterecv_end", "value": $('#daterecv_end').val() } ); // <-- this

    $.getJSON( sSource, aoData, function (json) {
    /* Do whatever additional processing you want on the callback, then tell DataTables */
    fnCallback(json)
    } );
    },
    [/code]

    fnDraw, when you have bServerSide set to true, will initiate fnServerData. Every call to the server will add your custom parameters to the set.
  • jay.chandranjay.chandran Posts: 10Questions: 0Answers: 0
    Thanks Stephan & fbas for your inputs. It works fine. :) :) Thanks a lot. :)
This discussion has been closed.