I am using jQuery datatables. I am getting few values in JSON as columns in datatables. In two of those columns , one has only radio buttons and one has only checkboxes. I want to sort on the basis of checkboxes (like if the checkbox is checked it should appear first) and radio buttons. How can I do that?
1 Answer
See Live DOM ordering example.
/* 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';
} );
}
/* Initialise the table with the required column ordering data types */
$(document).ready(function() {
$('#example').DataTable( {
"columns": [
{ "orderDataType": "dom-checkbox" }
]
} );
} );
2 Comments
gene
What if I don't want to click on a column to change the sorting, and instead I want the table to be populated automatically based on a selected radio button where the row with selected radio button is the top one?
Gyrocode.com
@gene, you can apply initial ordering to the column containing radio buttons using
order option.