使用 jQuery TableSorter 对包含下拉 (SELECT) 标签和美元符号 ($) 的列进行排序

发布于 2024-11-05 02:21:18 字数 404 浏览 0 评论 0原文

我正在使用出色的 jQuery TableSorter 插件自动向表的列添加排序功能(只需单击每列的标题即可)。除了其中几个列之外,这对所有列都完美无缺。

1) 该列的单元格之一的前面包含美元符号(例如:$20、$10、$5)。排序不能正常进行;它按字母顺序排序(并且由于所有单元格内容都以 $ 开头,因此它们都错误地捆绑在一起)。什么代码会强制排序器从第二个字符开始,从而忽略美元符号?

2) 另一列有动态下拉菜单(每个单元格中有 1 个 SELECT 标签),我希望它按每个 SELECT 标签内当前选定的值按字母顺序对列进行排序。有什么想法吗?

如果您至少能为我指明正确的方向,并让我了解如何在这两种情况下自定义排序,我将不胜感激。

提前致谢!!

I am using the fabulous jQuery TableSorter plugin to automatically add sorting functionality to the columns of a table (simply by clicking on each column's header). This works flawlessly for all columns except a couple of them.

1) One of the column's cells contain dollar signs in the front (such as: $20, $10, $5). The sorting does not work properly; it sorts alphabetically (and since all cell contents start with a $, they all get incorrectly bundled together). What code would force the sorter to start from the second character, thus ignoring the dollar signs?

2) Another column has dynamic drop-downs (1 SELECT tag in each cell), and I would like it to alphabetically sort the column by the currently selected values inside each SELECT tag. Any ideas?

If you can at least point me to the right direction and give me an idea about how to customize the sorting in each of the two scenarios, I would greatly appreciate it.

Thanks in advance!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

新一帅帅 2024-11-12 02:21:18

这对我有用。如果未选择任何选项,可能需要一些调整...

$(document).ready(function(){
    // Sortable tables
    $('table').tablesorter ({
        cancelSelection: true,
        sortList: [[0,0]],
        textExtraction: function(node) {
            // Check if option selected is set
            if ($(node).find('option:selected').text() != "") {
                return $(node).find('option:selected').text();
            }
            // Otherwise return text
            else return $(node).text();
        }
    });
});

该库在较新版本中使用缓存。对下拉列表的更新需要刷新表排序器的缓存。如果缓存未刷新,则该列将按下拉列表的原始选择而不是当前选择进行排序。

// hook into the select element's onchange event
$("td > select").change(function() {
        const config = $("table")[0].config;
        //refresh the cache so the newly selected element is used to sort with
        $.tablesorter.updateCache(config);
});

This worked for me. may need some tweaking if no option is selected...

$(document).ready(function(){
    // Sortable tables
    $('table').tablesorter ({
        cancelSelection: true,
        sortList: [[0,0]],
        textExtraction: function(node) {
            // Check if option selected is set
            if ($(node).find('option:selected').text() != "") {
                return $(node).find('option:selected').text();
            }
            // Otherwise return text
            else return $(node).text();
        }
    });
});

This library uses a cache in newer versions. Updates to a dropdown require the table sorter's cache to be refreshed. If the cache is not refreshed then the column will sort by the dropdown's original selection and not its current one.

// hook into the select element's onchange event
$("td > select").change(function() {
        const config = $("table")[0].config;
        //refresh the cache so the newly selected element is used to sort with
        $.tablesorter.updateCache(config);
});
小红帽 2024-11-12 02:21:18

2)另一栏有动态
下拉菜单(每个下拉菜单中有 1 个 SELECT 标签
细胞),我希望它
按字母顺序对列进行排序
每个中当前选择的值
选择标签。有什么想法吗?

我只是想实现这一目标,但不可能以简单的方式实现。解析器函数作为参数获取的字符串 (..format: function(s) {..) 已从标签中剥离。因此无法确定选择哪个值。为此,必须修改表排序器。

我现在所做的是在前面添加一个隐藏选项,其中包含所选值。这是一个解决方法。但就像这样,甚至不需要编写自己的解析器和表排序器即可正确排序。

<option style="display:none">B</option>
<option value="A">A</option>
<option value="B" selected="selected">B</option>
<option value="C">C</option>
...
<option style="display:none">A</option>
<option value="A"  selected="selected">A</option>
<option value="B">B</option>
<option value="C">C</option>

tablesorter 现在排序的字符串是:

  • BABC
  • AABC

2) Another column has dynamic
drop-downs (1 SELECT tag in each
cell), and I would like it to
alphabetically sort the column by the
currently selected values inside each
SELECT tag. Any ideas?

I just tried to achieve that and it's not possible in a straightforward way. The string that the parser function gets as a parameter (..format: function(s) {..) is already stripped from tags. So it's not possible to determine which value is selected. For that, the tablesorter would have to be modified.

What I did now was to add a hidden option up front with the selected value in it. It's a workaround. But like that it's not even necessary to write your own parser and tablesorter sorts correctly.

<option style="display:none">B</option>
<option value="A">A</option>
<option value="B" selected="selected">B</option>
<option value="C">C</option>
...
<option style="display:none">A</option>
<option value="A"  selected="selected">A</option>
<option value="B">B</option>
<option value="C">C</option>

The strings after which tablesorter sorts now are:

  • BABC
  • AABC
关于从前 2024-11-12 02:21:18

如果它可以帮助将来尝试这样做的人,我找到了一个解决方案:

2) Another column has dynamic drop-downs (1 SELECT tag in each cell), and I would like it to
alphabetically sort the column by the currently selected values inside each SELECT tag. Any ideas?

使用 tablesorter 的分支作者:Mottie。它允许在列级别自定义文本提取功能(请参阅 Mottie 的文档)。对于我想要排序的列(在本例中是第一个列,因此索引为 0),我初始化为:

textExtraction: {
    0: function(node) {
        return $(node).find('option:selected').text();
    }
}

遗憾的是,我看不到一种优雅的方法来让它与原始数据一起工作。

In case it helps someone trying to do this in future, I found a solution to:

2) Another column has dynamic drop-downs (1 SELECT tag in each cell), and I would like it to
alphabetically sort the column by the currently selected values inside each SELECT tag. Any ideas?

using a fork of tablesorter by Mottie. It allows for custom text extraction functions at the column level (see Mottie's documentation). For the column I wanted to sort (the first in this case, hence the 0 index), I initialized with:

textExtraction: {
    0: function(node) {
        return $(node).find('option:selected').text();
    }
}

I couldn't see an elegant way to get it to work with the original, sadly.

烟燃烟灭 2024-11-12 02:21:18

对于非标准数据(除了简单文本或数字之外的任何数据),您必须编写一个解析器并通过其 API 添加它。请参见此处:http://tablesorter.com/docs/example-parsers.html

我必须对具有数值(百分比)和图像的表格单元格执行此操作你可以尝试这个简单的。 “$1.58”将排序在数字 1.58 处

// add parser through the tablesorter addParser method 
$.tablesorter.addParser({ 
    // set a unique id 
    id: 'money', 
    is: function(s) { 
        // return false so this parser is not auto detected 
        return false; 
    }, 
    format: function(s) { 
        // format your data for normalization 
        var str = s.replace('
, '').replace(',','') ;
        return parseFloat(str);
    }, 
    // set type, either numeric or text 
    type: 'numeric' 
}); 

For the non-standard data (anything other than simple text or numerics, you have to write a parser and add it via their API. See here: http://tablesorter.com/docs/example-parsers.html

I had to do this for table cells that had a numeric value (a percentage) and an image in the same cell. You could try this for simple "$1.58" to be sorted at the number 1.58

// add parser through the tablesorter addParser method 
$.tablesorter.addParser({ 
    // set a unique id 
    id: 'money', 
    is: function(s) { 
        // return false so this parser is not auto detected 
        return false; 
    }, 
    format: function(s) { 
        // format your data for normalization 
        var str = s.replace('
, '').replace(',','') ;
        return parseFloat(str);
    }, 
    // set type, either numeric or text 
    type: 'numeric' 
}); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文