【发布时间】:2015-11-15 00:22:00
【问题描述】:
我确实有一个 php 生成的页面,其中有一个在页面加载后存在的表。
<table id="#previewTable">
<thead>
<th class="colA">column a - Firstname</th>
<th>column b - Lastname</th>
</thead>
<tbody>
<tr>
<td class="colA">Alexander
</td>
<td class="colB">Zandvoort
</td>
</tr>
<tr>
<td class="colA">Alexander
</td>
<td class="colB">Brno
</td>
</tr>
<tr>
<td class="colA">Bastian
</td>
<td class="colB">Zolder
</td>
</tr>
</tbody>
</table>
在 doc.ready 期间,我像这样应用 tablesorter 小部件:
var the_table = $('#previewTable');
//the_wrapper ==> any jquery selector (not object!) like '.mywrapper' or '#mywrapper'
//the_table ==> any jquery selector (not object!) like '.mytable' or '#mytable'
//the_childs_class ==> class name of the chilkd rows, like 'child-row' - NO dot!
var idx = $(the_table+'.ts-date').index(); //all headers with german dates
var options = {
showProcessing: true,
sortReset : true,
saveSortReset : false,
resort:true,
widgets: [ 'saveSort','zebra','stickyHeaders' ],
cssInfoBlock : "row-sorter-false",
//those rows will NOT be included in a sorting! useful to implement blockwise sorting
widgetOptions: {
// number or jquery selector targeting the position:fixed element
stickyHeaders_offset : 0,
// added to table ID, if it exists
stickyHeaders_cloneId : '-sticky',
// trigger "resize" event on headers
stickyHeaders_addResizeEvent : true,
// if false and a caption exist, it won't be included in the sticky header
stickyHeaders_includeCaption : true,
// scroll table top into view after filtering
stickyHeaders_filteredToTop: true,
/* make the table scroll within its wrapper */
stickyHeaders_attachTo : the_wrapper
},
cssChildRow: the_childs_class,
dateFormat : "ddmmyyyy", // set the default date format
idx: { sorter: "shortDate", dateFormat: "ddmmyyyy" }
};
$(the_table).tablesorter(options);
完成后一切正常。
现在用户将在十列 A 和 B 中任意两个方向进行排序。
saveSort 小部件可以很好地记住所有内容。
我们确实为每一列“隐藏该列”设置了一个复选框。
取消/选中该复选框,假设 B 应该隐藏/显示相应的 B 列,删除已通过 B 列完成的行的排序并重新应用任何现有的剩余排序 - 在本例中为 A。
我尝试这样做: $(document).on('click','.chk_must',function(){ //所有复选框都必须
theSortList = $( '#previewTable' ).get(0).config.sortList;
var new_sort_list = [];
if($('.chk_must_'+$(this).attr('data-nr')).is(':visible')){
$('.chk_must_'+$(this).attr('data-nr')).hide();//show or hide M1 and so on rows
theColumnNumber = $('.chk_must_'+$(this).attr('data-nr')).attr('data-column');
$(theSortList).each(function(key,element){ //get the header text for each selected
//var element[0] // var element[1]; //[0]=field number starting at 0, [1]=sort order, whereas 0=ASC and 1=DESC
if(element[0]!=theColumnNumber){
new_sort_list.push(element);
}
});
if (new_sort_list.length==0){
//nothing
}else{
$( '#previewTable' ).get(0).config.sortList = [new_sort_list];
$('#previewTable').trigger( 'sorton',[new_sort_list] );
}
$("#previewTable").trigger("updateCache").trigger("updateAll");
$('#previewTable').trigger('refreshWidgets', true,false);
}else{
$('.chk_must_'+$(this).attr('data-nr')).show();//show or hide M1 and so on rows
}
});
我做过什么......排序不会根据 [new_sort_list] 重置和重做,但保持不变。
隐藏/显示列本身也可以。
似乎我没有应用正确的 new_sort_list 类型/格式,这可能是内部缓存不会被重置 - 即使没有使用 refreshWidgets 和 updateCache 等等。
有什么想法可以让它发挥作用吗?
TIA
亚历克斯
【问题讨论】:
标签: javascript php jquery sorting tablesorter