Skip to content

Instantly share code, notes, and snippets.

@RedhatPH8
Forked from wsakaren/data_sort_checkbox
Last active March 8, 2020 03:01
Show Gist options
  • Save RedhatPH8/6d608f25ef84f54dee02ac586dcfc070 to your computer and use it in GitHub Desktop.
Save RedhatPH8/6d608f25ef84f54dee02ac586dcfc070 to your computer and use it in GitHub Desktop.
column sorting with datatables on checkbox
/**LEGACY DataTables 1.9
/* Create an array with the values of all the checkboxes in a column */
$.fn.dataTableExt.afnSortData['dom-checkbox'] = function ( oSettings, iColumn )
{
return $.map( oSettings.oApi._fnGetTrNodes(oSettings), function (tr, i) {
return $('td:eq('+iColumn+') input', tr).prop('checked') ? '1' : '0';
} );
}
*//
/**Updated to DataTables 1.10**/
/* Create an array with the values of all the checkboxes in a column */
$.fn.dataTable.ext.order['dom-checkbox'] = function ( settings, col )
{
return this.api().column( col, {order:'index'} ).nodes().map( function ( td, i ) {
return $('input', td).prop('checked') ? '1' : '0';
} );
}
$('.ratemgr-table').dataTable({
"aoColumnDefs": [
{ "bSortable": false, "aTargets": [ "action" ] },
{ "sSortDataType": "dom-checkbox", "aTargets": [ "enabled" ] }
],
"aaSorting": [
[ 3, "desc" ]
]
});
In the view I have this to enable me to put the checkbox in different columns (not tied to col number):
<th class="enabled">Enabled</th>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment