jQuery tablesorter 没有正确排序数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1459710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery tablesorter is not sorting number correctly
提问by Fons
I've been trying for days now to get jQuery tablesorter correctly sort numbers in my table column.
我已经尝试了好几天来让 jQuery tablesorter 在我的表列中正确地对数字进行排序。
I am using the current latest versions of both scripts.
我正在使用这两个脚本的当前最新版本。
The table is rendered fine, but sorting the numbers is not working correctly.
表格呈现良好,但对数字进行排序无法正常工作。
When I sort a number column it gives me the following results:
当我对数字列进行排序时,它会给我以下结果:
8 7 4 32 31 3 etc..
8 7 4 32 31 3 等等。
where you would expect: 32 31 8 etc...
您期望的位置:32 31 8 等...
I read some comments on adding extra javascript code but I can't find any good javascript examples.
我阅读了一些关于添加额外 javascript 代码的评论,但我找不到任何好的 javascript 示例。
The jQuery I'm using now is as follows:
我现在使用的 jQuery 如下:
$(document).ready(function()
{
$("#table1")
.tablesorter(
{
sortList: [[0,0]],
widthFixed: true,
widgets: ['zebra']
} )
}
);
Here is my HTML:
这是我的 HTML:
<table id="table1" class=tablesorter>
<thead>
<tr>
<th width=65>Name</th>
<th width=40>Count</th>
</tr>
</thead>
<tbody>
<tr><td>Name_1</td><td>32</td></tr>
<tr><td>Name_2</td><td>12</td></tr>
<tr><td>Name_3</td><td>11</td></tr>
<tr><td>name_4</td><td>14</td></tr>
<tr><td>Name_5</td><td>7</td></tr>
<tr><td>Name_6</td><td>3</td></tr>
<tr><td>Name_7</td><td>32</td></tr>
<tr><td>Name_8</td><td>31</td></tr>
<tr><td>Name_9</td><td>35</td></tr>
</tbody>
</table>
回答by Dave
Hopefully this will help someone if they find this post, in tablesorter you can now simply use.
希望这对找到这篇文章的人有所帮助,在 tablesorter 中,您现在可以简单地使用。
$(".table").tablesorter({
headers: {
5: { sorter: 'digit' } // column number, type
}
});
回答by Fons
<th width=110 class=\"{sorter: 'digit'}\">Count</th>
This solved the problem. Telling the javascript to handle the value's as a digit made the sorting work correct. Still bit silly that number values are not checked in the script as being numbers. But i guess there is a higher purpose for that in the end.
这解决了问题。告诉 javascript 将值作为数字处理使排序工作正确。在脚本中没有将数字值检查为数字仍然有点愚蠢。但我想最终会有一个更高的目的。
Thanks all for your time and help
感谢大家的时间和帮助
/Fons
/丰
回答by Jeff Steil
This may have been obvious to others (not to me) but to get the solution working with the {sorter: 'digit'} metadata you need to use the jQuery metadata plugin.
这对其他人(不是我)来说可能很明显,但要使解决方案与 {sorter: 'digit'} 元数据一起使用,您需要使用jQuery metadata plugin。
回答by r0skar
I know this is an old question, but I came across the very same problem and instead of trying ANY solution posted here, you should first check the version of your plugin. Every problem was solved when I found out I wasnt using the newest version (2.0.5)
我知道这是一个老问题,但我遇到了同样的问题,与其尝试这里发布的任何解决方案,不如先检查插件的版本。当我发现我没有使用最新版本 (2.0.5) 时,所有问题都解决了
回答by pjesi
It looks like you need to pad your numbers. That explains why 8, 7, and 4 are ordered before 32, and 31.
看起来你需要填充你的数字。这就解释了为什么 8、7 和 4 排在 32 和 31 之前。
Try this:
尝试这个:
function padLeft(s,len,c){
c=c || '0';
while(s.length< len) s= c+s;
return s;
}
$("table").tablesorter({
textExtraction: function(node) {
return padLeft(node.innerHTML,2);
}
});
Use higher value than 2 if you need to sort bigger numbers.
如果您需要对更大的数字进行排序,请使用大于 2 的值。
回答by Paul G Petty
You might try this as well:
你也可以试试这个:
$(document).ready(function() {
$("table").tablesorter({
// put other options here ...
textExtraction: function(node) {
return parseInt($(node).text());
}
});
});
... this treats the sorted cells' content as integers, after extracting just the text.
...在仅提取文本后,这会将排序后的单元格内容视为整数。
回答by tvanfosson
Can you show your html as well? Tablesorter ought to detect and handle numeric sorting without any special options. Is it possible that your numeric values are surrounded by html? In that case you may need a custom method to extractthe values from the html.
你也可以显示你的html吗?Tablesorter 应该在没有任何特殊选项的情况下检测和处理数字排序。您的数值是否有可能被 html 包围?在这种情况下,您可能需要一个自定义方法来从 html 中提取值。
Example from the referenced link:
来自引用链接的示例:
$(document).ready(function() {
// call the tablesorter plugin
$("table").tablesorter({
// define a custom text extraction function
textExtraction: function(node) {
// extract data from markup and return it
return node.childNodes[0].childNodes[0].innerHTML;
}
});
});
回答by user2261392
Find in jquery.tablesorter.js code:
在 jquery.tablesorter.js 代码中查找:
this.isDigit = function(s,config) {
var DECIMAL = '\' + config.decimal;
var exp = '/(^[+]?0(' + DECIMAL +'0+)?$)|(^([-+]?[1-9][0-9]*)$)|(^([-+]?((0?|[1-9][0-9]*)' + DECIMAL +'(0*[1-9][0-9]*)))$)|(^[-+]?[1-9]+[0-9]*' + DECIMAL +'0+$)/';
return RegExp(exp).test($.trim(s));
};
And replace it with:
并将其替换为:
this.isDigit = function(s,config) {
var DECIMAL = '\' + config.decimal;
var exp = '/(^[+]?0(' + DECIMAL +'0+)?$)|(^([-+]?[1-9][0-9]*)$)|(^([-+]?((0?|[1-9][0-9]*)' + DECIMAL +'(0*[1-9][0-9]*)))$)|(^[-+]?[1-9]+[0-9]*' + DECIMAL +'0+$)/';
//return RegExp(exp).test($.trim(s));
return !isNaN(parseFloat($.trim(s))) && isFinite($.trim(s));
};