Add Persian number sorting plugin

pull/520/head
Hesam Mohammadi 4 years ago
parent 21dc30cba3
commit 3b805f0fd2

@ -1,41 +1,41 @@
/** /**
* Sorts a column containing Persian numbers. Persian numbers can easily be * Sorts a column containing Persian numbers. Persian numbers can easily be
* mapped 1:1 to latin numbers - ۱ = 1, ۲ = 2, ۱۲ = 12 and so on. * mapped 1:1 to latin numbers - ۱ = 1, ۲ = 2, ۱۲ = 12 and so on.
* *
* *
* @name DataTables Persian Number Sorting * @name DataTables Persian Number Sorting
* @summary Sorts columns containing UTF-8 Persian numbers * @summary Sorts columns containing UTF-8 Persian numbers
* @author [Khorshid](https://khorshidlab.com) * @author [Khorshid](https://khorshidlab.com)
* *
* @example * @example
* $('#example').DataTable( { * $('#example').DataTable( {
* columnDefs: [ * columnDefs: [
* { type: 'kh-persian-numbers', targets: 0 } * { type: 'kh-persian-numbers', targets: 0 }
* ] * ]
* } ); * } );
*/ */
jQuery.extend(jQuery.fn.dataTableExt.oSort, { jQuery.extend(jQuery.fn.dataTableExt.oSort, {
"kh-persian-numbers-pre": function (a) { "kh-persian-numbers-pre": function (a) {
function toEnglishNumber(strNum) { function toEnglishNumber(strNum) {
var pn = ["۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"]; var pn = ["۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"];
var en = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]; var en = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
var cache = strNum; var cache = strNum;
for (var i = 0; i < 10; i++) { for (var i = 0; i < 10; i++) {
var regex_fa = new RegExp(pn[i], 'g'); var regex_fa = new RegExp(pn[i], 'g');
cache = cache.replace(regex_fa, en[i]); cache = cache.replace(regex_fa, en[i]);
} }
return cache; return cache;
} }
return parseFloat(toEnglishNumber(a)) return parseFloat(toEnglishNumber(a))
}, },
"kh-persian-numbers-asc": function (a, b) { "kh-persian-numbers-asc": function (a, b) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0)) return ((a < b) ? -1 : ((a > b) ? 1 : 0))
}, },
"kh-persian-numbers-desc": function (a, b) { "kh-persian-numbers-desc": function (a, b) {
return ((a < b) ? 1 : ((a > b) ? -1 : 0)) return ((a < b) ? 1 : ((a > b) ? -1 : 0))
} }
}); });
Loading…
Cancel
Save