Sorting a checkbox column

Sorting a checkbox column

rgl928rgl928 Posts: 7Questions: 0Answers: 0
edited July 2013 in DataTables 1.9
I'm using this great plugin for my tables. I want to sort by a column that is a checkbox. Reading the info and some posts I did this:

In my view page (mvc) I've 12 columns where the 12th is a check box.

Here is how a Init my table:

[code]


$(document).ready(
$('table').dataTable({
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"aoColumns": [
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
{ "sSortDataType": "dom-checkbox" }]
})
);



[/code]

I'm also using this for the sorting:

[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");
});
return aData;
};

[/code]

When I try to sort that column, I receive the following error:

DataTables warning: (table id='table'): Returned data sort array (col11) is the wrong length. I can't solve it. Help!

Replies

  • PhilGibsonPhilGibson Posts: 1Questions: 0Answers: 0
    Hi rgl928,

    I was having a similar problem to you and struggling with this error.

    My solution was to inspect the check box on my data table. There i found that i had hidden check boxes, so the length was adding 1 each time a check box was found. I then gave my unhidden check box a class, then changed this line:

    [code]
    $('td:eq(' + iColumn + ') input.yourclass', oSettings.oApi._fnGetTrNodes(oSettings)).each(function () {
    [/code]

    In the html file i added:

    [code]

    @Html.CheckBoxFor(model => user.blahblahblah, new { @onClick = "AccountCreated(" + @user.ID+ ", $(this).is(':checked'));", @class = "yourclass" })

    [/code]

    Hope this helped :D
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    because of a recent change in jQuery, you need to update the way the DOM sorting plug-ins work so be something like that used in the updated examples:
    http://datatables.net/release-datatables/examples/plug-ins/dom_sort.html

    Allan
This discussion has been closed.