如何使用自然排序插件使DataTables工作? [英] How to make work DataTables with Natural sort plugin?
问题描述
我看到了一些类似的问题,但是我的情况没有正确的答案.
I've seen some similar questions, but no correct answer in my case.
我使用 DataTables 插件对某些表进行排序.
I use the DataTables plugin to sort some tables.
但是,我们无法对数字(而不是数字)进行排序(螺母;-p). 例如,我们无法对格式价格进行排序:
But we can't sort datas like numerics while they are not (nut ;-p). For example, we can't sort formatted prices like those :
- 2150000€
- 4 500 000€
- 225 000欧元
因此,我尝试包括一个名为"
So I tried to include a DataTables plugin called "Natural sort". But it doesn't seems to work, I may do some mistakes, but I can't find them.
任何帮助将不胜感激.
您需要查看/测试的所有内容: JSfiddle (尝试对列进行排序...)
All you need to see/test it : JSfiddle (try to sort the columns...)
推荐答案
因此,您似乎有两个问题.首先,dT不会选择您的自定义排序-只是忽略它并使用标准排序.我可以通过为每列指定排序来解决此问题:
So it looks like you have two problems. First, dT isn't picking up your custom sort - it's just ignoring it and using the standard sort. I was able to fix this by specifying the sort for each column:
$('#table_list').dataTable({
"aoColumns": [null, {"sType": "natural"}, null, null],
"aaSorting": [[ 1, "asc" ]],
"sScrollX": "100%",
});
第二,自然排序不会按照您期望的方式对这些数字进行排序.它将占用第一个数字部分直到空格并对其进行排序,如下所示:
Second, natural sort won't sort those numbers the way you expect. It will take the first number section up until the space and sort on that, like so:
2 150 000 €
4 750 000 €
210 000 €
因此,您可能只想编写自己的排序.这是我尝试的示例:
So you probably just want to write your own sort. Here's an example I tried:
function testSort( a, b ) {
var aa = a.replace(/[ \.]/g,''), bb = b.replace(/[ \.]/g,'');
aa = parseInt( aa.substring( 0, aa.length - 1 ) );
bb = parseInt( bb.substring( 0, bb.length - 1 ) );
return aa == bb ? 0 : ( aa < bb ? -1 : 1 );
}
关键是删除空格,句点和欧元符号,以便可以将其读取为数字.
The key is removing the spaces, periods and euro signs so that it can be read as a number.
这篇关于如何使用自然排序插件使DataTables工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!