can't make serverSide works

can't make serverSide works

VaNVaN Posts: 5Questions: 0Answers: 0
edited January 2012 in General
Hello,

I'd like to make my datatable runs with Ajax, but I'm facing a problem.

here is the javascript :

[code]$('.datatable').dataTable({
"bJQueryUI": true,

"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "ajax_list_candidats.php",

});[/code]

and my ajax file :
[code]<?php
$str = '{
"aaData":[
[
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8"
],
[
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8"
]
],
"sEcho":"1"
}';

echo $str;
?>[/code]

My table just displays a "Processing popup", but anything more happens.

Where am I mistaken ?

Replies

  • allanallan Posts: 63,397Questions: 1Answers: 10,451 Site admin
    Because your string isn't valid JSON - you've got unescaped newline characters in there ( http://json.org for the spec and http://jsonlint.com for testing).

    If you want to use server-side processing, then you'll need a slightly more complicated script than that :-). http://datatables.net/usage/server-side

    Allan
  • VaNVaN Posts: 5Questions: 0Answers: 0
    I edited my ajax file :
    [code]<?php
    $str = '{"aaData":[["1","2","3","4","5","6","7","8"],["1","2","3","4","5","6","7","8"]],"sEcho":"1","iTotalRecords": 2,"iTotalDisplayRecords": 2}';

    echo $str;
    ?>[/code]
    same thing, i'm getting stuck to a "Processing" popup, my 2 lines are not pushed into my table. I can't even see the file call in firebug..

    I can't figure what's wrong in my javascript.
  • allanallan Posts: 63,397Questions: 1Answers: 10,451 Site admin
    You have a trailing comma which will break IE, but Firefox should be okay. If you can give us a link that would be very useful.

    Allan
  • VaNVaN Posts: 5Questions: 0Answers: 0
    Ok, i've got something working, partially. When I load my page, datable calls the ajax file, and populate the table with 25 rows. But when i click on the next page, the "Processing..." popup shows, but the table doesn't refresh rows.

    My Ajax file is ok, as it loads properly on page load.

    Here is my DataTable instanciation :

    [code]$('.datatable').dataTable({
    "bJQueryUI": true,
    "iDisplayLength": 25,
    "aLengthMenu": [25, 50, 100, 500],
    "sPaginationType": "full_numbers",
    "oLanguage": {
    "sLengthMenu": "Afficher _MENU_ enregistrements",
    "sSearch": "Recherche :",
    "sZeroRecords": "Aucune donnée correspondante",
    "sInfo": "Affichage de _START_ à _END_ sur _TOTAL_ enregistrements",
    "sInfoEmpty": "0 résultats",
    "sInfoFiltered": "( total de _MAX_ enregistrements)",
    "oPaginate": {
    "sFirst": "Début",
    "sLast": "Fin",
    "sNext": "Suivant",
    "sPrevious": "Précédent",
    }
    },
    "aoColumnDefs": [
    { "sType": "string", "aTargets": [ -2 ] }
    ],
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "ajax_list_candidats.php",
    "fnServerData": function ( sSource, aoData, fnCallback ) {
    $.ajax( {
    "dataType": 'json',
    "type": "POST",
    "url": sSource,
    "data": aoData,
    "success": fnCallback
    } );
    }
    });[/code]
  • allanallan Posts: 63,397Questions: 1Answers: 10,451 Site admin
    > My Ajax file is ok, as it loads properly on page load.

    It is okay for the first load - but not the second! Have a look at the documentation for server-side processing that I linked to before: http://datatables.net/usage/server-side . sEcho must be echoed back as it is sent (ideally parsed as an integer for security) - not just echoed as '1' - which would cause the problem you are seeing.

    Allan
  • VaNVaN Posts: 5Questions: 0Answers: 0
    great, it works perfectly now. Thanks for your help.
This discussion has been closed.