2

I'm using the jQuery plugin DataTables to sort tables throughout my site. There are several tables which utilize a check-all checkbox in the header, and a checkbox in each row. Looking at the documentation and plugins, it seems the best course of action is to utilize this plugin: http://www.datatables.net/plug-ins/sorting/custom-data-source/dom-checkbox

To implement, it requires the columns option to be used:

$('.dt-check').dataTable( {
    "columns": [
        { "orderDataType": "dom-checkbox" },
        null,
        null,
        null
    ]
});

My problem is that the numerous tables have different counts of columns, meaning I'd need an additional script for every single different table. Is there a way to implement this where I can sort by the first column, regardless of how many columns there are?

1 Answer 1

3

A bit late, but if you haven't solved this problem already, you can set the column on which to sort by using the "columnDefs" property:

https://datatables.net/reference/option/columns.orderDataType

In your case, if you wanted to order by the first column at all times, you would do this:

$('.dt-check').dataTable( {
    "columnDefs": [
        { "orderDataType": "dom-checkbox", targets: 0 }
    ]
});

I believe the columns are 0 index based, so this setting would apply the dom-checkbox ordering rule to the first column regardless of the column count.

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

Comments

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.