Sorting a checkbox column
Sorting a checkbox column
rgl928
Posts: 7Questions: 0Answers: 0
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!
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!
This discussion has been closed.
Replies
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
http://datatables.net/release-datatables/examples/plug-ins/dom_sort.html
Allan