1

I am using jquery datatables and I need to sort data by the first column which contains checkboxes by displaying checked boxes first

oTable =  $('#userTable', context).dataTable(
{
    "sAjaxSource":'/ajax/getdata/',
    "fnServerData": function ( sSource, aoData, fnCallback, oSettings ) 
    {
        oSettings.jqXHR = $.ajax( 
        {
            "dataType": 'json',
            "type": "POST",
            "url": sSource,
            "data": params,
            "success" :  function(data)
            {
                fnCallback(data);
                fnSortBySelected();
            } 
        });
    }
});

var fnSortBySelected = function()
{
    var oSettings = oTable.fnSettings();
    var i = 0; 

    $('td:first-child input[type=checkbox]',oTable).sort(function(a, b)
    {
        if(a.checked) 
            oTable.fnUpdate( oSettings.aoData[i]._aData, i, 0);
        else 
            oTable.fnUpdate( oSettings.aoData[i+1]._aData, i, 0);
        i++;       
    });
}

thanks for your time , this is what i tried so far :

 oTable =  $('#userTable', context).dataTable({
"sAjaxSource":'/ajax/getdata/',
"fnServerData": function ( sSource, aoData, fnCallback, oSettings ) {
oSettings.jqXHR = $.ajax( {
           "dataType": 'json',
           "type": "POST",
           "url": sSource,
           "data": params,
           "success" :  function(data){
               fnCallback(data);
               fnSortBySelected();
           } 
        } );
}

 });

var fnSortBySelected = function()
{
    var oSettings = oTable.fnSettings();
    var i = 0; 

    $('td:first-child input[type=checkbox]',oTable).sort(function(a, b){
        if(a.checked) 
             oTable.fnUpdate( oSettings.aoData[i]._aData, i, 0);
        else 
             oTable.fnUpdate( oSettings.aoData[i+1]._aData, i, 0);
         i++;       
    } );


}
2
  • What have you tried so far? Providing your current code is a nice gesture saying that your not just asking for help and would like to learn. Commented Oct 1, 2012 at 17:13
  • Hi Nate , thanks for your time , this is what i tried so far : Commented Oct 2, 2012 at 14:53

3 Answers 3

2

I Made it using Snickers answer by creating a hidden column (first column index 0) on every selected checkbox , i set the corresponding hidden column to "selected" then I sort the table on that hidden column and first name column

to hide the first column use

oTable.fnSetColumnVis( 0, false );

for every clicked checkbox do this

$('input[tyope=checkbox]',oTable).each(function(){
  var aPos = oTable.fnGetPosition($(this).parents('tr:first').get(0));
  oTable.fnUpdate( 'selected',aPos, 0 );
  $(this).attr('checked', true);  
});

and then call

oTable.fnSort( [ [0,'desc'],[2,'asc']] );

Thank you guys for your help

Sign up to request clarification or add additional context in comments.

Comments

1

Just add the right aoColumnDefs for your column:

oTable =  $('#userTable', context).dataTable(
{
    "aoColumnDefs": [
        { "sSortDataType": "dom-checkbox", "aTargets": [ 0 ] }
    ],
    .....

See Datatables colums

Comments

0

Maybe you can try this:

$(document).ready(function() {
  var oTable = $('#example').dataTable();

  // Sort immediately with columns 0 and 1
  oTable.fnSort( [ [0,'asc'], [1,'asc'] ] );
} );

1 Comment

Hey Snickers , your fnSort is working but it's not sorting column with checkboxes , any Idea what i can do ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.