server-side processing function

server-side processing function

yingzunyingzun Posts: 2Questions: 0Answers: 0
edited August 2011 in General
After getting data from the server, for example :
$(document).ready(function() {
$('#example').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "xhr.php"})}

I want to sort the data by the specified column on the client side using js language, what should I do ? How to sort the data I get from the server using the functions datatable provide?

Replies

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Sorting in DataTables, when server-side processing is enabled as you have above, is entirely done on the server-side. The Javascript code in DataTables doesn't do any sorting (since it doesn't have the full data set, so any sorting it would do is likely wrong).

    If you do want to do that however, you could use fnServerData to intercept the JSON return from the server and just use standard Javascript sorting techniques before passing it on to DataTables for display.

    Allan
  • yingzunyingzun Posts: 2Questions: 0Answers: 0
    Thanks,Allan. Here is my problem. Now I've already got the data formatted as jason. Does Datatables have some functions to sort the data after I have already got the data from the server?
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    edited August 2011
    If you're using JSON, but want client-side processing, just turn off bServerSide. That variable is meant to indicate using a server side script to perform processing.

    You can also provide an intialization variable to declare the default sorting you want:
    [code]
    /* Sort by 3rd column first, and then 4th column */
    $(document).ready( function() {
    $('#example').dataTable( {
    "bProcessing": true,
    "sAjaxSource": "xhr.php",
    "aaSorting": [[2,'asc'], [3,'desc']] /// specify default sort state
    } );
    } );
    [/code]
This discussion has been closed.