From f9bebe8d8c5f3c6aaaaf4dd45ef9c20664f18f16 Mon Sep 17 00:00:00 2001 From: lucasandrade Date: Thu, 13 Nov 2014 14:35:05 -0200 Subject: [PATCH 1/4] Included string extension to remove accents. --- jquery.tablesorter.js | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/jquery.tablesorter.js b/jquery.tablesorter.js index c5eabf0..8ec78d4 100644 --- a/jquery.tablesorter.js +++ b/jquery.tablesorter.js @@ -890,7 +890,7 @@ is: function (s) { return true; }, format: function (s) { - return $.trim(s.toLocaleLowerCase()); + return $.trim(s.toLocaleLowerCase().cleanAccents()); }, type: "text" }); @@ -1036,3 +1036,31 @@ } }); })(jQuery); + +// Convert characters with accents to normal +String.prototype.cleanAccents = function () { + var string = this; + var mapaAcentosHex = { + a: /[\xE0-\xE6]/g, + A: /[\xC0-\xC6]/g, + e: /[\xE8-\xEB]/g, + E: /[\xC8-\xCB]/g, + i: /[\xEC-\xEF]/g, + I: /[\xCC-\xCF]/g, + o: /[\xF2-\xF6]/g, + O: /[\xD2-\xD6]/g, + u: /[\xF9-\xFC]/g, + U: /[\xD9-\xDC]/g, + c: /\xE7/g, + C: /\xC7/g, + n: /\xF1/g, + N: /\xD1/g, + }; + + for (var letra in mapaAcentosHex) { + var expressaoRegular = mapaAcentosHex[letra]; + string = string.replace(expressaoRegular, letra); + } + + return string; +} From f9951a1eaba9f8b3e235b8c894e5f65d3a0a1a49 Mon Sep 17 00:00:00 2001 From: lucasandrade Date: Thu, 13 Nov 2014 14:44:42 -0200 Subject: [PATCH 2/4] Added option to configure decimal and thousand character. --- jquery.tablesorter.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/jquery.tablesorter.js b/jquery.tablesorter.js index c5eabf0..59f5691 100644 --- a/jquery.tablesorter.js +++ b/jquery.tablesorter.js @@ -862,7 +862,8 @@ }; this.isDigit = function (s, config) { // replace all an wanted chars and match. - return /^[-+]?\d*$/.test($.trim(s.replace(/[,.']/g, ''))); + var regex = new RegExp('^[\\-\\+]?\\d*\\' + config.decimal + '?\\d*$', 'g'); + return regex.test($.trim(s.replace(new RegExp('\\' + config.thousandsSeparator, 'g'), ''))); }; this.clearTableBody = function (table) { if ($.browser.msie) { @@ -899,17 +900,18 @@ is: function (s, table) { var c = table.config; return $.tablesorter.isDigit(s, c); - }, format: function (s) { - return $.tablesorter.formatFloat(s); + }, format: function (s, table) { + return $.tablesorter.formatFloat(s.replace(new RegExp('\\' + table.config.thousandsSeparator, 'g'), '').replace(new RegExp('\\' + table.config.decimal, 'g'), '.')); }, type: "numeric" }); ts.addParser({ id: "currency", - is: function (s) { - return /^[£$€?.]/.test(s); + is: function (s, table) { + var c = table.config; + return /^\w?[£$€\?.]/.test(s) && $.tablesorter.isDigit(s.replace(new RegExp(/^\w?[£$€\?.]/g), ""), c); }, format: function (s) { - return $.tablesorter.formatFloat(s.replace(new RegExp(/[£$€]/g), "")); + return $.tablesorter.formatFloat(s.replace(new RegExp(/^\w?[£$€\?.]/g), "").replace(new RegExp('\\' + table.config.thousandsSeparator, 'g'), '').replace(new RegExp('\\' + table.config.decimal, 'g'), '.')); }, type: "numeric" }); From 847ce740026265f22f883962a2ff40bc1a9f4163 Mon Sep 17 00:00:00 2001 From: lucasandrade Date: Thu, 13 Nov 2014 14:50:10 -0200 Subject: [PATCH 3/4] Missing config attributes. --- jquery.tablesorter.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/jquery.tablesorter.js b/jquery.tablesorter.js index 22e8190..b1240f5 100644 --- a/jquery.tablesorter.js +++ b/jquery.tablesorter.js @@ -128,7 +128,8 @@ sortList: [], headerList: [], dateFormat: "us", - decimal: '/\.|\,/g', + decimal: '.', + thousandsSeparator: ',', onRenderHeader: null, selectorHeaders: 'thead th', debug: false From 0770c303e509340494c65f6453497b0554bc2cd3 Mon Sep 17 00:00:00 2001 From: lucasandrade Date: Fri, 14 Nov 2014 11:49:28 -0200 Subject: [PATCH 4/4] Removed cleanaccent and set the text sort using page locale. --- jquery.tablesorter.js | 43 +++++++++++-------------------------------- 1 file changed, 11 insertions(+), 32 deletions(-) diff --git a/jquery.tablesorter.js b/jquery.tablesorter.js index b1240f5..312f5a7 100644 --- a/jquery.tablesorter.js +++ b/jquery.tablesorter.js @@ -604,7 +604,7 @@ // var s = (table.config.parsers[c].type == "text") ? ((order == 0) // ? makeSortText(c) : makeSortTextDesc(c)) : ((order == 0) ? // makeSortNumeric(c) : makeSortNumericDesc(c)); - var s = (table.config.parsers[c].type == "text") ? ((order == 0) ? makeSortFunction("text", "asc", c) : makeSortFunction("text", "desc", c)) : ((order == 0) ? makeSortFunction("numeric", "asc", c) : makeSortFunction("numeric", "desc", c)); + var s = (table.config.parsers[c].type == "text") ? ((order == 0) ? makeSortFunction("text", "asc", c, table) : makeSortFunction("text", "desc", c, table)) : ((order == 0) ? makeSortFunction("numeric", "asc", c, table) : makeSortFunction("numeric", "desc", c, table)); var e = "e" + i; dynamicExp += "var " + e + " = " + s; // + "(a[" + c + "],b[" + c @@ -640,13 +640,19 @@ return cache; }; - function makeSortFunction(type, direction, index) { + function makeSortFunction(type, direction, index, table) { var a = "a[" + index + "]", b = "b[" + index + "]"; if (type == 'text' && direction == 'asc') { - return "(" + a + " == " + b + " ? 0 : (" + a + " === null ? Number.POSITIVE_INFINITY : (" + b + " === null ? Number.NEGATIVE_INFINITY : (" + a + " < " + b + ") ? -1 : 1 )));"; + if (table.config.sortLocaleCompare) + return "(" + a + " == " + b + " ? 0 : (" + a + " === null ? Number.POSITIVE_INFINITY : (" + b + " === null ? Number.NEGATIVE_INFINITY : " + a + ".localeCompare(" + b + "))));"; + else + return "(" + a + " == " + b + " ? 0 : (" + a + " === null ? Number.POSITIVE_INFINITY : (" + b + " === null ? Number.NEGATIVE_INFINITY : (" + a + " < " + b + ") ? -1 : 1 )));"; } else if (type == 'text' && direction == 'desc') { - return "(" + a + " == " + b + " ? 0 : (" + a + " === null ? Number.POSITIVE_INFINITY : (" + b + " === null ? Number.NEGATIVE_INFINITY : (" + b + " < " + a + ") ? -1 : 1 )));"; + if (table.config.sortLocaleCompare) + return "(" + a + " == " + b + " ? 0 : (" + a + " === null ? Number.POSITIVE_INFINITY : (" + b + " === null ? Number.NEGATIVE_INFINITY : " + b + ".localeCompare(" + a + "))));"; + else + return "(" + a + " == " + b + " ? 0 : (" + a + " === null ? Number.POSITIVE_INFINITY : (" + b + " === null ? Number.NEGATIVE_INFINITY : (" + b + " < " + a + ") ? -1 : 1 )));"; } else if (type == 'numeric' && direction == 'asc') { return "(" + a + " === null && " + b + " === null) ? 0 :(" + a + " === null ? Number.POSITIVE_INFINITY : (" + b + " === null ? Number.NEGATIVE_INFINITY : " + a + " - " + b + "));"; } else if (type == 'numeric' && direction == 'desc') { @@ -892,7 +898,7 @@ is: function (s) { return true; }, format: function (s) { - return $.trim(s.toLocaleLowerCase().cleanAccents()); + return $.trim(s.toLocaleLowerCase()); }, type: "text" }); @@ -1040,30 +1046,3 @@ }); })(jQuery); -// Convert characters with accents to normal -String.prototype.cleanAccents = function () { - var string = this; - var mapaAcentosHex = { - a: /[\xE0-\xE6]/g, - A: /[\xC0-\xC6]/g, - e: /[\xE8-\xEB]/g, - E: /[\xC8-\xCB]/g, - i: /[\xEC-\xEF]/g, - I: /[\xCC-\xCF]/g, - o: /[\xF2-\xF6]/g, - O: /[\xD2-\xD6]/g, - u: /[\xF9-\xFC]/g, - U: /[\xD9-\xDC]/g, - c: /\xE7/g, - C: /\xC7/g, - n: /\xF1/g, - N: /\xD1/g, - }; - - for (var letra in mapaAcentosHex) { - var expressaoRegular = mapaAcentosHex[letra]; - string = string.replace(expressaoRegular, letra); - } - - return string; -}