jQuery tablesorter:确定列的排序顺序 [英] jquery tablesorter : determining sort order for column

查看:231
本文介绍了jQuery tablesorter:确定列的排序顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://tablesorter.com 是否可以根据升序还是升序对不同的值进行排序降序?

Is it possible for http://tablesorter.com to sort on different values depending on whether the sort is ascending or descending ?

我看到了如何使用解析器基于任意数据返回单元格的排序值(即:

I see how to return sort values for a cell based on arbitrary data with a parser (i.e.: http://code-cocktail.in/hidden-value-sorting-in-jquery-tablesorter-plugin/ ) , so returning different values in different situations seems simple enough.

我不禁要问的一点是,我似乎无法确定解析时的排序顺序.通过以下方式请求排序时,看起来我可以重新解析单元格:

The bit I'm banging my head on is that I cannot seem to determine the sort order when parsing. It looks like I can re-parse cells when a sort is being requested via the following:

 $(".tablesorter").bind("sortStart",function() { 
        $(".tablesorter").trigger("update");
 });

...但是,它看起来像 在"sortStart"点,排序顺序未知,因此解析器无法根据该列的排序顺序提供不同的值.

... however it looks like at the "sortStart" point the order of the sort isn't known, so the parser cannot provide different values based on that column's sort order.

这可能吗?谢谢:-)

推荐答案

为使以下代码正常工作,您需要使用我的

For the following code to work, you'll need to use my fork of tablesorter which has css, widgets and addons that are not compatible with the original version.

这有点类似于此Stackoverflow问题,只是它允许使用两个不同的值对一列进行排序(两个答案都可以使用不同的方法很好地工作).

This is somewhat similar to this Stackoverflow question except that it allows sorting one column using two different values (both answers work nicely using different methods).

在进行升序排序时使用一个值,在降序排序时使用另一个值是必需的( demo ):

A minor modification is required to use one value during ascending sort and another during descending sort (demo):

HTML(一个单元格)

HTML (one cell)

<th class="gamename">
    <span class="name">Fun Fun</span>
    <span class="perc">96%</span>
</th>

脚本

$(function () {

    $('#games').tablesorter({
        theme: 'blue',
        textExtraction: {
            0: function (node, table, cellIndex) {
                var $n = $(node);
                // add semi-colon between values
                return $n.find('.name').text() + ';' + $n.find('.perc').text();
            }
        },
        textSorter: function (a, b, direction, columnIndex, table) {
            if (columnIndex === 0) {
                var c = table.config,
                    x = a.split(';'),
                    y = b.split(';'),
                    // sort column by percentage when descending sort is active
                    i = c.$headers.eq(0).hasClass('tablesorter-headerDesc') ? 1 : 0;
                return $.tablesorter.sortNatural($.trim(x[i]), $.trim(y[i]));
            } else {
                return $.tablesorter.sortNatural(a,b);
            }
        }
    });

});

这篇关于jQuery tablesorter:确定列的排序顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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