Question about fnServerParams

Question about fnServerParams

mathieufanneemathieufannee Posts: 17Questions: 0Answers: 0
edited September 2011 in General
Hello,

First, my compliments to Allan for Datatables. I'm gradually learning to use it those days and as I begin to understand the way it works, I must say I'm happy with it!

My question:
I'm wondering if there might be a bug in the fnServerParams function.

I tried to add custom variables to my Ajax calls to my server, in the way described in this example:
http://www.datatables.net/release-datatables/examples/server_side/custom_vars.html

[code]
"fnServerParams": function ( aoData ) {
aoData.push( { "name": "table", "value": "wordforms" } );
}
[/code]

Somehow it doesn't work.

But I discovered a post in the Datatables forum, which advises to add custom variables another way:
http://www.datatables.net/forums/discussion/6491/display-only-a-partial-list-from-db#Item_2

[code]
"fnServerData": function ( sSource, aoData, fnCallback ) {
aoData.push( { "name": "table", "value": "wordforms" } );
$.ajax( {
"dataType": 'json',
"url": sSource,
"data": aoData,
"success": fnCallback
} );
}
[/code]

But as I understood in this post (http://www.datatables.net/forums/discussion/6475/repopulating-a-datatable-with-new-parameters/p1), the first solution is a replacement for the second.
So how come the first solution isn't working? Am I missing something?

Replies

  • mathieufanneemathieufannee Posts: 17Questions: 0Answers: 0
    Of course if you need more of my code for analysis, I will put it here. Just ask :-)
  • GregPGregP Posts: 500Questions: 10Answers: 0
    Mathieu,

    Let me take a crack at it: fnServerData allows you to wholly replace the data retrieval method, which uses GET. Although people have all kinds of reasons to modify it, one of the main ones is to switch the method from GET to POST.

    fnServerParams allows you to add new data to the request without the need to rewrite the entire fnServerData function.

    Based on what you've provided thus far, you've chosen the right method. Now the trick is to figure out why it's not working for you. If you took the code directly from the example page rather than from the API/Full Reference example, I just noticed that there's a typo in it. That could be throwing you off. (There should be a comma after the sAjaxSource parameter line)
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    fnServerParams is a new 1.8.2 interface. if you have 1.8.1 or earlier, there is no fnServerParams

    (allan, I think you should include version numbers in the reference pages)
  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin
    Yup - as fbas says, this is new in 1.8.2, so please make sure you are running the latest release :-).

    @fbas: Agreed - ultimately the plan is to have documentation a bit like FixedColumns and Scroller where is it versioned (for example http://datatables.net/docs/FixedColumns/2.0.1/ ), but I'll look at adding a version parameter to the current documentation.

    Allan
  • mathieufanneemathieufannee Posts: 17Questions: 0Answers: 0
    My fault, sorry for that. Since I downloaded DataTable only 10 days ago, I didn't pay attention to the version.

    I now upgraded to 1.8.2 (I had 1.8.1) and it's working well.

    Thank you.
  • zzsuzzsu Posts: 2Questions: 0Answers: 0
    I misstood first: "name" is the key and "value" is the other key for other pair.
    Infact [code]aoData.push( { "name": "table", "value": "wordforms" } );[/code] only defines on pair. "table" is the key and "wordforms" is the value.
  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin
    Yup - this was the old jQuery way of doing when when DataTables was first started, so it's been carried on since then. In 2.0 (whenever I get time for tha!) I'll be changing it to use the more understandable straight JSON key / values.

    Allan
  • jasonaworkmanjasonaworkman Posts: 2Questions: 0Answers: 0
    I had a similar problem. I have download 1.8.2, but when I try to use fnServerParams, I get undefined=undefined in the request. I stepped through the code, and it looks like the ajax call is trying to send an array of objects instead of an object.

    my code:
    "fnServerParams": function ( aoData ) {
    aoData.push( {"so": "in", "s": "PENDING" } );
    }

    In the call to _fnServerParams( oSettings, aoData), aoData is passed as empty array and is filled in with my parameters in the first and only element of the array.

    But the call to fnServerData() passes in the entire array instead of just the first element of the array as I would expect. In other words, the 'data' argument is [{"so": "in", "s": "PENDING"}] instead of {"so": "in", "s": "PENDING"}

    Am I missing something?

    Thanks, Jason
  • jasonaworkmanjasonaworkman Posts: 2Questions: 0Answers: 0
    Never mind I see it now. Each object I push into the aoData array has to have 2 elements, one with the name of 'name' and one with the name of 'value'.
  • sunmit9sunmit9 Posts: 19Questions: 1Answers: 0
    Is there a way so that I can add custom parameters using 'fnServerData' and push the custom parameters in the data-table in a function outside.
    eg: It can be done using fnServerParams by doing
    oTable.aoServerParams.push( { name: "myName", value: "myValue"} );
    I do not get the parameters in JSON when I use 'fnServerParams' and 'aoData.push'.
    Can someone please help?
  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin
    > oTable.aoServerParams.push( { name: "myName", value: "myValue"} );

    hmmm - do you not mean:

    [code]
    aoData.push( { name: "myName", value: "myValue"} );
    [/code]

    where aoData is the parameter passed into fnServerParams ?

    Which version of DataTables are you using? Can you run your table through the debugger please ( http://debug.datatables.net ).

    Allan
  • wilfoderekwilfoderek Posts: 1Questions: 0Answers: 0
    Hi friends.
    If you want to send multiples values , You can do this:
    "fnServerParams": function ( aoData ) {
    aoData.push( { "name": "fecIni", "value": "2012-02-12" },{ "name": "fecFin", "value": "2012-05-12" } );
    },
    Enjoy it...
  • yazzodeleonyazzodeleon Posts: 2Questions: 0Answers: 0
    edited October 2012
    hi. i install datatables 1.9 and trying to use fnServerParams but it doesnt load the value i needed. is there something i forgotten to install, like table tools or some coding.
    here is my script

    [code]
    $(document).ready(function() {
    $('#example').dataTable( {
    "bJQueryUI": true,
    "sPaginationType": "full_numbers",
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "script/server_showapply.php",
    "fnServerParams": function ( aoData ) {
    aoData.push( { "name": "position", "value": "encoder" } );
    },
    "fnDrawCallback": function() {
    $("#example tbody tr").click(function () {
    var nTds = this.getElementsByTagName('td');
    var id = nTds[0].innerHTML; // value of the second column in this row
    var sr = nTds[1].innerHTML; // value of the second column in this row
    window.location.href = "show-appli.php?id="+id+"&sr="+sr;
    } );
    }

    } );
    } );[/code]
  • joseray20joseray20 Posts: 2Questions: 1Answers: 0

    Como recibo los valores enviados en el lado del servidor?

  • merlinpjmerlinpj Posts: 4Questions: 0Answers: 0

    @joseray20

    Si usas PHP, los valores puedes recibirlos como $_REQUEST["parametro"]

This discussion has been closed.