如何从 tablesorter 插件获取当前排序顺序? [英] how to get current sort order from tablesorter plugin?

查看:11
本文介绍了如何从 tablesorter 插件获取当前排序顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用 Christian Bach 出色的 TableSorter 插件,我需要获取列的当前排序方向.我有几列:

I'm just starting to use Christian Bach's excellent TableSorter plugin, and I need to get a column's current sort direction. I have several columns:

  • 身份证
  • 姓名
  • 类别

使用

headers:    { 0: {sorter: false}, 1: {sorter: false} }

我在 Name 上添加了一个单击处理程序,以便它在 Category 列上触发排序事件.使用示例使用表外链接对表进行排序",我能够获得 Name 标头来触发 Category 排序——但它是硬编码的,可以按一个方向排序.

I'm adding a click handler on Name so that it fires the sort event on the Category column. Using the example "Sort table using a link outside the table", I'm able to get the Name header to fire the Category sort -- but it's hard-coded to sort in one direction.

如何让它查看 Category 列当前排序的当前方向,并按相反方向排序?(我可以处理翻转值;由于排序顺序是 0 或 1,我可以对值进行异或运算以得到相反的结果,例如 var sort; sort ^= sort; - 我的问题是如何获取当前值.

How can I get it to look at the current direction the Category column is currently sorted, and sort in the opposite direction? (I can handle flipping the values; since the sort order is 0 or 1, I can XOR the value to get the opposite, like var sort; sort ^= sort; -- my question is how to get the current value.

下面是当前在 Name 列上设置点击处理程序的代码:

Here's the code that currently sets the click handler on the Name column:

$("#nameCol").click(function() {
    var sorting = [[2, 0]];     /* sort 3rd col (Category) descending */
    $("#SearchResults").trigger("sorton", [sorting] );  /* SearchResults is the ID of the sortable table */
    return false;               /* cancel default link action on a#nameCol */
});

谢谢!

推荐答案

表头应该都调用同一个点击事件:

The table headers should all call the same click event:

$('th').click(function() {
     handleHeaderClick(this);
});  

然后单击处理程序应添加/删除适用的类.

Then click handler should add/remove the applicable classes.

function handleHeaderClick(hdr) {
    if ($(hdr).hasClass('headerSortDown') == true) {
        $(hdr).removeClass('headerSortDown');
        $(hdr).addClass('headerSortUp');
    } else if ($(hdr).hasClass('headerSortUp') == true) {
        $(hdr).removeClass('headerSortUp');
        $(hdr).addClass('headerSortDown');
    } else {
        $('th', myTable).removeClass('headerSortUp headerSortDown');
        $(hdr).addClass('headerSortDown');
    }
    doSomething();
};

我希望这会有所帮助.

这篇关于如何从 tablesorter 插件获取当前排序顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆