Sorting only by visible columns

Sorting only by visible columns

datatableusersdatatableusers Posts: 2Questions: 0Answers: 0
edited July 2013 in DataTables 1.9
I probably read almost every thread I could find here but still did not succeed to solve my issue. I hope I am not missing anything obvious :)

I implemented Server-Side rendering. I am showing only 5 columns while the response contains 6 columns. I am using 1 column to render one table column. As such for example when I click on the 4th column it sorts by the 3rd column. I understand I can do something with fnVisibleToColumnIndex but I did not understand where and how I put it.

My jquery:
[code]
$(document).ready(function() {
$('#tablesorter').dataTable({
"sPaginationType": "full_numbers",
"aaSorting": [[ 4, "asc" ]],
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "query.php",
"aoColumns": [
{
"mRender": function ( data, type, row ) {
return '';
},
"aTargets": [ 0 ]
},
{ "mData": 2 },
{ "mData": 3 },
{ "mData": 4 },
{ "mData": 5 }

]
});

} );
[/code]

If I understand correctly all I need to do is to implement the following:
[code]
$.fn.dataTableExt.oApi.fnVisibleToColumnIndex = function ( oSettings, iMatch )
{
return oSettings.oApi._fnVisibleToColumnIndex( oSettings, iMatch );
};
[/code]

I would appreciate any hint on where and how should I put it if it is the right direction at all.

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    You need to have your server-side script aware that you have shifted the column indexes on the client-side. For example sorting on column index 1 in the table means you actually want to sort on column index 2 on the server. That translation should probably be done in the server-side script.

    Personally I'd recommend using objects rather than arrays, particularly if you are going to hide some data. It will make life much easier: http://datatables.net/release-datatables/examples/server_side/object_data.html

    Allan
  • datatableusersdatatableusers Posts: 2Questions: 0Answers: 0
    Well of course :)
    Thanks a lot.

    I made the following change to the PHP example.

    [code]
    $sOrder .= "`".$aColumns[ intval( $_GET['iSortCol_'.$i]+1 ) ]."` ".
    [/code]

    Instead of
    [code]
    $sOrder .= "`".$aColumns[ intval( $_GET['iSortCol_'.$i]) ]."` ".
    [/code]

    Now it works great.
This discussion has been closed.