How to ignore th with specific class name in tablesorter?
I use the tablesorter to sort the table, but not all columns, I want to ignore the specific column with the defined class name, eg:
<table class="basicList" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th class="ck_field"><input type="checkbox" id="check_all" name="check_all" /></th>
<th class="col_filter"><a href="#" class="btn_filter"> </a></th>
开发者_StackOverflow社区 <th>Name <span class="sort_indicator"> </span></th>
</tr>
<tr class="filter_row">
<td> </td>
<td> </td>
<td> <input type="text" id="the_name" name="name" class="filter_field"/></td>
</tr>
</thead>
</table>
In this example, I don't want to sort the first two columns.
And I tried:
$(document).ready(function() {
console.log($(".basicList .col_filter").index());
console.log($(".basicList .ck_field").index());
var ck_ignore = $(".basicList .ck_field").index();
var filter_ignore = $(".basicList .col_filter").index();
$(".basicList").tablesorter({ widgets: ['zebra'],headers: {
//disable the first checkbox cell
$ck_ignore: {
sorter: false
},
$filter_ignore : {
sorter: false
}
}
});
Which doesn't work, how to solve this problem?
Use it like this:
var myHeaders = {};
myHeaders[ck_ignore] = { sorter: false };
myHeaders[filter_ignore] = { sorter: false };
$(".basicList").tablesorter({ widgets: ['zebra'], headers: myHeaders });
Note: if you have more columns with these classes, it will work only for the first occurences.
As per the documentation, you can now use class="sorter-false"
Ran into the same problem, and here's what I came up with. Assuming your th
class for not sorting is called nosort
:
function setupTablesorter() {
$('table.tablesorter').each(function (i, e) {
var myHeaders = {}
$(this).find('th.nosort').each(function (i, e) {
myHeaders[$(this).index()] = { sorter: false };
});
$(this).tablesorter({ widgets: ['zebra'], headers: myHeaders });
});
}
This has a few advantages over the other answers I saw here. First, it works even if you have multiple non-sortable columns. Second, it also still works if you have multiple tables on the same page, each with different sortable columns.
Just use the headers argument:
$(document).ready(function() {
console.log($(".basicList .col_filter").index());
console.log($(".basicList .ck_field").index());
var ck_ignore = $(".basicList .ck_field").index();
var filter_ignore = $(".basicList .col_filter").index();
$(".basicList").tablesorter({ widgets: ['zebra'],headers: {
// pass the headers argument and assing a object
headers: {
// assign the first column (we start counting zero)
0: {
// disable it by setting the property sorter to false
sorter: false
}
}
,
$filter_ignore : {
sorter: false
}
}
});
精彩评论