我使用的是tablesorter插件,它很棒。我不得不克隆原始头,然后在可滚动区域上方重新插入克隆。
为了在隐藏的旧表头元素上触发tablesorter插件,当用户单击可见的克隆表时,我使用.trigger()在隐藏表上触发单击。
下面是jQuery:
$('.w_price_assess').delegate('#quotations_clone thead th', 'click', function() {
var $cloneTh = $(this);
var $cloneThIndex = $cloneTh.index('#quotations_clone thead th');
$('#quotations thead th:eq(' + $cloneThIndex + ')').trigger('click');
var $classList =$('#quotations thead th:eq(' + $cloneThIndex + ')').attr('class').split(/\s+/);
console.log($classList);
$.each($classList, function(index, item){
if (item==='headerSortDown') {
$('#quotations_clone thead th').removeClass('headerSortUp headerSortDown');
$('#quotations_clone thead th:eq(' + $cloneThIndex + ')').addClass('headerSortDown');
} else if (item==='headerSortUp') {
$('#quotations_clone thead th').removeClass('headerSortUp headerSortDown');
$('#quotations_clone thead th:eq(' + $cloneThIndex + ')').addClass('headerSortUp');
} else {
//$('#quotations_clone thead th').removeClass('headerSortUp headerSortDown');
}
});
});出现的问题是,当我第一次单击克隆的th元素时,它不会立即返回表排序程序附加到隐藏的th元素的类。
我需要在同一时间注册,但不确定如何去做?
发布于 2010-10-20 00:47:32
我设法通过使用全局变量解决了这个问题,如下所示:
var $orginalHead = $("#tablesorter-demo thead");
globalIndex = null;
$($orginalHead)
.clone()
.insertBefore("#tablesorter-demo")
.wrap('<table id="sorter-clone" />');
$("body").delegate("#sorter-clone thead th", "click", function() {
var $tHeader = $(this);
$tHeaderIndex = $tHeader.index("#sorter-clone thead th");
$('#tablesorter-demo thead th:eq(' + $tHeaderIndex + ')').trigger('click');
});
$("#tablesorter-demo").bind("sortEnd", function() {
var $classList = $('#tablesorter-demo thead th:eq(' + $tHeaderIndex + ')').attr('class').split(/\s+/);
replaceClasses($classList, $tHeaderIndex, globalIndex);
});
function replaceClasses(classes, index, oldIndex) {
globalIndex = index;
var list = classes.toString().replace(",", " ");
var oldHead = $("#sorter-clone thead th:eq(" + oldIndex + ")").removeAttr("class");
var newHead = $("#sorter-clone thead th:eq(" + index + ")").removeAttr("class").addClass(list);
if (oldIndex !== null) {
return {x:oldHead, y:newHead};
} else {
console.log("bar");
}
}https://stackoverflow.com/questions/3933567
复制相似问题