使用 jQuery TableSorter 对包含下拉 (SELECT) 标签和美元符号 ($) 的列进行排序
我正在使用出色的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这对我有用。如果未选择任何选项,可能需要一些调整...
该库在较新版本中使用缓存。对下拉列表的更新需要刷新表排序器的缓存。如果缓存未刷新,则该列将按下拉列表的原始选择而不是当前选择进行排序。
This worked for me. may need some tweaking if no option is selected...
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.
我只是想实现这一目标,但不可能以简单的方式实现。解析器函数作为参数获取的字符串 (..format: function(s) {..) 已从标签中剥离。因此无法确定选择哪个值。为此,必须修改表排序器。
我现在所做的是在前面添加一个隐藏选项,其中包含所选值。这是一个解决方法。但就像这样,甚至不需要编写自己的解析器和表排序器即可正确排序。
tablesorter 现在排序的字符串是:
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.
The strings after which tablesorter sorts now are:
如果它可以帮助将来尝试这样做的人,我找到了一个解决方案:
使用 tablesorter 的分支作者:Mottie。它允许在列级别自定义文本提取功能(请参阅 Mottie 的文档)。对于我想要排序的列(在本例中是第一个列,因此索引为 0),我初始化为:
遗憾的是,我看不到一种优雅的方法来让它与原始数据一起工作。
In case it helps someone trying to do this in future, I found a solution to:
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:
I couldn't see an elegant way to get it to work with the original, sadly.
对于非标准数据(除了简单文本或数字之外的任何数据),您必须编写一个解析器并通过其 API 添加它。请参见此处:http://tablesorter.com/docs/example-parsers.html
我必须对具有数值(百分比)和图像的表格单元格执行此操作你可以尝试这个简单的。 “$1.58”将排序在数字 1.58 处
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