Tablesorter — How can I sort a column by first or last name (both ascending) rather than toggling between ascending and decending?

huangapple go评论62阅读模式
英文:

Tablesorter — How can I sort a column by first or last name (both ascending) rather than toggling between ascending and decending?

问题

I want to use tablesorter to sort a column (column contains first name + last name) so that clicking the header toggles between sorting by first name and sorting by last name, both in ascending order.

我想使用 tablesorter 来对一个包含名字和姓氏的列进行排序,点击标题时切换按名字升序和按姓氏升序排序。

I have found a jsfiddle (link) that is very close to what I want to do.

我找到了一个 jsfiddle(链接),非常接近我想要的功能。

It toggles between sorting by name (ascending) and sorting by percentage (descending) when the header is clicked. However, in my case, I want it to toggle between sorting first name (ascending) and last name (ascending). I want both sorts to be ascending and I'm not sure how to modify this code to make that happen. If nothing else, I can make them separate columns instead and give up on this, but I think it would look better to have the full name displayed together.

当点击标题时,它在按名称(升序)和按百分比(降序)之间切换排序。然而,在我的情况下,我希望它在名字(升序)和姓氏(升序)之间切换排序。我希望两种排序都是升序的,但我不确定如何修改这段代码以实现这一点。如果没有其他办法,我可以将它们分为两列,但我认为将全名一起显示会更好看。

I tried looking through the documentation, but I wasn't able to figure out a way.

我尝试查看了文档,但我无法找到解决方法。

英文:

I want to use tablesorter to sort a column (column contains first name + last name) so that clicking the header toggles between sorting by first name and sorting by last name, both in ascending order.
I have found a jsfiddle (link) that is very close to what I want to do.

$(function () {

    var $cell;
    $('#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) {
            var x = a.split(';'),
                y = b.split(';'),
                i = $cell && $cell.is('.active') ? 1 : 0;
            return $.tablesorter.sortNatural($.trim(x[i]), $.trim(y[i]));
        },
        initialized: function () {
            $cell = $('#games').find('.percSort');
            $cell.click(function () {
                // activate percentage sort
                $cell.addClass('active');
                return false;
            }).closest('th').click(function () {
                // clicking on header outside of percSort
                // inactivates the percentage sort
                $cell.removeClass('active');
            });

        }
    });

});

It toggles between sorting by name (ascending) and sorting by percentage (descending) when the header is clicked. However, in my case, I want it to toggle between sorting first name (ascending) and last name (ascending). I want both sorts to be ascending and I'm not sure how to modify this code to make that happen. If nothing else, I can make them separate columns instead and give up on this, but I think it would look better to have the full name displayed together.

I tried looking through the documentation, but I wasn't able to figure out a way.

答案1

得分: 0

只需保持HTML结构不变(<div class="xxxx"><th><td>),只更改其文本内容:


&lt;h3&gt;点击“%”符号以按百分比排序第一列;&lt;br&gt;单击外部以按名称排序&lt;/h3&gt;

&lt;table id=&quot;games&quot;&gt;
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th&gt;名字 &lt;span class=&quot;percSort&quot;&gt;&amp;nbsp;姓氏&amp;nbsp;&lt;/span&gt;

            &lt;/th&gt;
            &lt;th&gt;数值&lt;/th&gt;
            &lt;th&gt;动物&lt;/th&gt;
            &lt;th&gt;网站&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;th class=&quot;gamename&quot;&gt;
                &lt;div style=&quot;width:66%;background-color: hsla(84,100%,50%,0.7);&quot;&gt;&lt;/div&gt;
&lt;span class=&quot;name&quot;&gt;Alice&lt;/span&gt;

&lt;span class=&quot;perc&quot;&gt;Petterson&lt;/span&gt;

            &lt;/th&gt;
            &lt;td&gt;10&lt;/td&gt;
            &lt;td&gt;考拉&lt;/td&gt;
            &lt;td&gt;http://www.google.com&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;th class=&quot;gamename&quot;&gt;
                &lt;div style=&quot;width:46%;background-color: hsla(34,100%,50%,0.7);&quot;&gt;&lt;/div&gt;
&lt;span class=&quot;name&quot;&gt;Peter&lt;/span&gt;

&lt;span class=&quot;perc&quot;&gt;Brook&lt;/span&gt;

            &lt;/th&gt;
            &lt;td&gt;234&lt;/td&gt;
            &lt;td&gt;牛&lt;/td&gt;
            &lt;td&gt;http://www.yahoo.com&lt;/td&gt;
        &lt;/tr&gt;
英文:

Just keep the html structure as it is (<div class="xxxx"><th><td>), and change just its text-content:


&lt;h3&gt;Click on the &quot;%&quot; sign to sort the first column by percentages;&lt;br&gt;Click outside to sort by name&lt;/h3&gt;

&lt;table id=&quot;games&quot;&gt;
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th&gt;Firstname &lt;span class=&quot;percSort&quot;&gt;&amp;nbsp;Lastname&amp;nbsp;&lt;/span&gt;

            &lt;/th&gt;
            &lt;th&gt;Numeric&lt;/th&gt;
            &lt;th&gt;Animals&lt;/th&gt;
            &lt;th&gt;Sites&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;th class=&quot;gamename&quot;&gt;
                &lt;div style=&quot;width:66%;background-color: hsla(84,100%,50%,0.7);&quot;&gt;&lt;/div&gt;
&lt;span class=&quot;name&quot;&gt;Alice&lt;/span&gt;

&lt;span class=&quot;perc&quot;&gt;Petterson&lt;/span&gt;

            &lt;/th&gt;
            &lt;td&gt;10&lt;/td&gt;
            &lt;td&gt;Koala&lt;/td&gt;
            &lt;td&gt;http://www.google.com&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;th class=&quot;gamename&quot;&gt;
                &lt;div style=&quot;width:46%;background-color: hsla(34,100%,50%,0.7);&quot;&gt;&lt;/div&gt;
&lt;span class=&quot;name&quot;&gt;Peter&lt;/span&gt;

&lt;span class=&quot;perc&quot;&gt;Brook&lt;/span&gt;

            &lt;/th&gt;
            &lt;td&gt;234&lt;/td&gt;
            &lt;td&gt;Ox&lt;/td&gt;
            &lt;td&gt;http://www.yahoo.com&lt;/td&gt;
        &lt;/tr&gt;

huangapple
  • 本文由 发表于 2023年7月4日 23:28:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76614056.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定