2

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 1

1

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" }
        ]
    } );
} );
Sign up to request clarification or add additional context in comments.

2 Comments

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?
@gene, you can apply initial ordering to the column containing radio buttons using order option.

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.