I'm using the jQuery TableSorter plugin and get the following error Uncaught TypeError: Cannot set property 'count' of undefined
with this code:
$(document).ready(function () {
var copyThead = $(".uiGridContent thead").html();
copyThead = '<table><thead>' + copyThead + '</thead></table>';
$(".uiGridHeader").html(copyThead);
$(".uiGridContent table").tablesorter();
$(".uiGridContent table thead").hide();
$(".uiGridHeader th").click(function () {
// Get updated HTML
var FindcopyThead = $(".uiGridContent thead").html();
var NewcopyThead = '<table><thead>' + FindcopyThead + '</thead></table>';
$(".uiGridHeader").html(NewcopyThead);
var index = $(".uiGridHeader th").index(this);
var sorting = [[index, 0]];
$(".uiGridContent table").trigger("sorton", [sorting]);
return false;
});
});
and the HTML is:
<div class="uiGrid">
<div class="uiGridHeader">
<!-- NEW HEADER GOES HERE -->
</div>
<div class="uiGridContent">
<table class="list sortable">
<thead>
<tr>
<th scope="col"><input type="checkbox" id="checkall" /></th>
<th scope="col">Name</th>
<th scope="col">Post Code</th>
<th scope="col">SIC Code</th>
<th scope="col">№ of Employees</th>
<th scope="col"></th>
<th scope="col"></th>
</tr>
&开发者_运维技巧lt;/thead>
<tbody>
<!-- ROWS -->
The reason I copy the Table Header is because I need to have the header split off for the structure of my app to work. This part is fine BUT because the classes etc get added to the original headers I need to keep updating the HTML with the new ones again this works fine. But I get the error? Any ideas why or how to fix it? Thanks
I was getting the same error. It took me a minute to figure out that the value you must pass is a three-deep array:
$('table').trigger('sorton', [[[1,1]]]);
As opposed to the initial sorting option:
$('table').tablesorter({
sortList: [[1,1]]
});
When you add new data to the table be sure to:
$('table').trigger('update').trigger('appendCache');
However, I could not get the tablesorter to play nice after dynamically adding new columns. Instead in those instances I must let jQuery unbind everything so I can re-init:
$('table').remove().appendTo('body').tablesorter({ ...
There are three problems. The index is returning -1, setting the HTML each time removes the events you have bound to the head, and because you are triggering the sorton event you need to keep track of the direction yourself.
Here is my code, I hope this helps.
var copyThead = $(".uiGridContent thead").html();
copyThead = '<table><thead>' + copyThead + '</thead></table>';
$(".uiGridHeader").html(copyThead);
$(".uiGridContent table").tablesorter();
$(".uiGridContent table thead").hide();
function bindClick() {
$(".uiGridHeader th").click(theadClick);
}
var direction = 0;
function theadClick() {
if(direction) {
direction = 0;
} else {
direction = 1;
}
var index = $(this).index();
var sorting = [[index, direction ]];
$(".uiGridContent table").trigger("sorton", [sorting]);
var FindcopyThead = $(".uiGridContent thead").html();
var NewcopyThead = '<table><thead>' + FindcopyThead + '</thead></table>';
$(".uiGridHeader").html(NewcopyThead);
bindClick();
}
bindClick();
精彩评论