Can you implement Server side sorting by column/property name

Can you implement Server side sorting by column/property name

MilesyMilesy Posts: 3Questions: 0Answers: 0
edited September 2011 in General
Rather than passing sorting details such as:

iSortCol_0 = 0

Would it be possible to pass something like
iSortCol_0 = somenamedproperty

So it can be more easily mapped to a bean/table.

Is something like this already in place or would I need to expand the plugin myself?

thanks

Chris

Replies

  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    yes, you can. you can add any parameters you want by adding to the aoData array of parameter objects in fnServerData.

    then in the server side script, look for those params.

    the trick will be to find in the DataTables object where the sort has been set. I'm sure it's not hard, but I don't know what it is offhand.

    http://www.datatables.net/ref#fnServerData
    [code]
    /* POST data to server */
    $(document).ready(function() {
    $('#example').dataTable( {
    "sAjaxSource": "data_source.php",
    "fnServerData": function ( sSource, aoData, fnCallback ) {
    /* Add some data to send to the source */
    aoData.push( { "name": "sortby", "value": "colname" } ); // obviously change the colname

    $.ajax( {
    "dataType": 'json',
    //"type": "POST", uncomment this if you're using POST rather than GET for params
    "url": sSource,
    "data": aoData,
    "success": fnCallback
    } );
    }
    } );
    } );
    [/code]

    it's worth noting that DataTables supports multiple column sorting (hold down shift and click columns to have cols added to the sort) so you might want to consider how to support this too. not necessary if your users don't expect or use the feature, though.
  • GregPGregP Posts: 487Questions: 8Answers: 0
    edited September 2011
    Milesy,

    Instead of returning 'simple' JSON, our application returns key: value pairs ( "name" : "Greg"); then we use mDataProp (an option inside aoColumns or aoColumnDefs) to publish the values to the table.

    When you use mDataProp, additional parameters are automatically sent in the request string. So you will end up with a pair of iSortCol_0=5 and mDataProp_5=name

    Skipping past the actual variable creation on the server side, it boils down to something like this (fake code):

    $sortCol = mDataProp_{iSortCol_0}

    Wow, writing that out it sounds way more complicated than it really is. ;-) Getting the two sides to play nicely together is also much easier when your mDataProp matches the key of the incoming JSON.
This discussion has been closed.