Question about fnServerParams
Question about fnServerParams
mathieufannee
Posts: 17Questions: 0Answers: 0
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?
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?
This discussion has been closed.
Replies
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)
(allan, I think you should include version numbers in the reference pages)
@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
I now upgraded to 1.8.2 (I had 1.8.1) and it's working well.
Thank you.
Infact [code]aoData.push( { "name": "table", "value": "wordforms" } );[/code] only defines on pair. "table" is the key and "wordforms" is the value.
Allan
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
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?
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
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...
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]
Como recibo los valores enviados en el lado del servidor?
@joseray20
Si usas PHP, los valores puedes recibirlos como $_REQUEST["parametro"]