How to use Ajax source, with parameters and an SQL request

How to use Ajax source, with parameters and an SQL request

CharlyPoppinsCharlyPoppins Posts: 16Questions: 2Answers: 0
edited August 2011 in Plug-ins
Hi,

i've got something like [code]var oTable = $('#liste_documents').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "script_affichedossier.php",
"fnServerData": function ( sSource, aoData, fnCallback ) {
$.ajax( {
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
"success": fnCallback
} );
}
} );[/code]

In script_affichedossier.php I'm getting such as 10000 records if my SQL request has no parameters.

How can I get parameters "aoData" given by DataTables ? I didn't manage to "trace" them.

Is that the good way to use DataTables and Ajax ?

Replies

  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    ajax souce doesn't use a database query or the aoData parameters. you would need to use bServerSide for that which requires a lot more support on the server side script.

    I think there might be a way you can add parameters to your sAjaxSource (simply concatenate them, since it's a string) and if your ajax source is a PHP script you can honor the params you add. you'd want to modify sAjaxSource for each call (if the params change). I suspect you can do this with the oSettings.

    [code]
    // make sure "script_affichedossier.php" can accept the params you send and form your JSON appropriately

    var oTable = $('#liste_documents').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "script_affichedossier.php?" + params,
    "fnServerData": function ( sSource, aoData, fnCallback ) {
    $.ajax( {
    "dataType": 'json',
    "type": "POST",
    "url": sSource,
    "data": aoData,
    "success": fnCallback
    } );
    }
    } );
    [/code]

    as your params change, this MIGHT work (test, because i'm not sure. also not sure the best place to put this code.. just somewhere before a DRAW is called)
    [code]
    oTable.fnSettings().sAjaxSource = "script_affichedossier.php?" + newparams;
    [/code]
  • CharlyPoppinsCharlyPoppins Posts: 16Questions: 2Answers: 0
    Bon je n'arrive plus rien à afficher dans mon tableau...

    Je vais repartir à zéro et je verrais bien.

    En tout cas merci pour la réponse :)
This discussion has been closed.