问题
i have problem with jQuery tablesorter and numbers like 12 345 678,91
$(document).ready(function() {
$.tablesorter.addParser({
id: 'thousands',
is: function(s) {
return false;
},
format: function(s) {
return s.replace(' ','').replace(/,/g,'');
},
type: 'numeric'
});
$("#tablesorter").tablesorter({
headers: {
3: { sorter:'thousands' },
4: { sorter:'thousands' },
5: { sorter:'thousands' }
}
});
});
output filter:
-1 295,76
-331,2
-330,01
-290
0
3 986 495,06
1 942 503,09
0
0
When i replace this: s.replace(' ','').replace(/,/g,''); by this : s.replace(new RegExp(/[^0-9/A-Za-z. ]/g),""); ...even worse than it was. any ideas?
回答1:
The parser isn't replacing all of the spaces; using replace(' ','') will only replace the first space. Also, the comma should be replaced with a decimal point since it indicates a fraction. So, try this (demo):
$.tablesorter.addParser({
id: 'thousands',
is: function (s) {
return false;
},
format: function (s) {
return s.replace(/\s+/g, '').replace(/,/g, '.');
},
type: 'numeric'
});
回答2:
Internet Explorer converts spaces to .
For me works this:
return s.replace(/ /g,'').replace(/\s+/g, '')
回答3:
@Triple_6:
Understand the meaning before implementing the solution: My guess is you don't want spaces or , or anything just pure numbers.
The expression below means replace everything other than numbers, alphabets,a decimal point and space with "".
s.replace(new RegExp(/[^0-9/A-Za-z. ]/g),"");
The above was suggested by me.
Solution:
If you dont want spaces then remove that from the expression.
s.replace(new RegExp(/[^0-9/A-Za-z.]/g),"");
If you want to have the sign then include that
s.replace(new RegExp(/[^0-9/A-Za-z.+-]/g),"");
Meaning of the Regex expression: new RegExp(/[Except this list]/g)," replace everything with this");
Working jsfiddle: http://jsfiddle.net/rK5s4/1/
Next time do make it a point to mention that you would like multiple occurrences of something removed. It is not clear from your question.
Also try putting them through jsfiddle before incorporating it.
来源:https://stackoverflow.com/questions/14606778/jquery-tablesorter-sorting-number-with-space