sort by checkbox columns doesn't work

sort by checkbox columns doesn't work

coolpalcoolpal Posts: 2Questions: 0Answers: 0
edited February 2013 in DataTables 1.9
I have come across this wonderful plugin just last week and I am trying to build a DataTable using javascript array (returned from server via ajax call). The last column in the table is rendered as a checkbox and I tried to incorporate the sorting extention based on "DataTables live DOM sorting example" from here: http://datatables.net/examples/plug-ins/dom_sort.html

In my case, that doesn't seem to affect anything when I sort by the checkbox column. I tried reviewing what the custom sort data function is returning and it seems to be fine. Even tried the function to return numeric array and changed the sType to numeric, which seems to alter the table but the sorting is all wonky (it sorts at random).

here's the snippets of code I am trying.

[code]

$.fn.dataTableExt.afnSortData['dom-checkbox'] = function ( oSettings, iColumn )
{
var aData = [];
$( 'td:eq('+iColumn+') input', oSettings.oApi._fnGetTrNodes(oSettings) ).each( function () {
aData.push( this.checked==true ? "1" : "0" );
} );
alert(aData);
return aData;
}

oTable = $('#table1').dataTable( {
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"fnInitComplete": function() {
this.fnAdjustColumnSizing();
//$(".dataTables_wrapper").css("width", $("#table1").css("width"));
},
"bDeferRender": false,
"aoColumns": [
{ "mDataProp": "geid", "sTitle": "Geid",
"mRender": function ( data, type, full ) {
return ''+ data +'';
}
},
{ "mDataProp": "deptId", "sTitle": "Department Id" },
{ "mDataProp": "firstName", "sTitle": "First Name" },
{ "mDataProp": "lastName", "sTitle": "Last Name" },
{ "mDataProp": "emailAddress", "sTitle": "Email Id"}, //, "sClass": "wrapword2" },
{ "mDataProp": "selected", "sTitle": "Select",
"mRender": function ( data, type, full ) {
return '';
},
"sSortDataType": "dom-checkbox"
}
],
"aaData": resultArr,
"aaSorting": [[3, 'asc']]
});
[/code]

Any help is greatly appreciated.

Replies

  • kkopczykkkopczyk Posts: 1Questions: 0Answers: 0
    edited May 2013
    For sorting a checkbox column, no sorting plug-in is necessary. You'll just have to modify your mRender function to be similar to the following:

    [code]
    function (data, type, full) {
    if (type === 'display') {
    // for display/render purposes, create a checkbox.
    return '';
    }
    // for sorting purposes, return the actual data.
    return data;
    }
    [/code]

    I was able to figure this out using Allen's response found here: http://datatables.net/forums/discussion/12063/mdata-mrender-and-ordering/p1
  • coolpalcoolpal Posts: 2Questions: 0Answers: 0
    thanks kkopczyk
This discussion has been closed.