-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatatables-persian-number-sorting.js
41 lines (37 loc) · 1.23 KB
/
datatables-persian-number-sorting.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* Sorts a column containing Persian numbers. Persian numbers can easily be
* mapped 1:1 to latin numbers - ۱ = 1, ۲ = 2, ۱۲ = 12 and so on.
*
*
* @name DataTables Persian Number Sorting
* @summary Sorts columns containing UTF-8 Persian numbers
* @author [Khorshid](https://khorshidlab.com)
*
* @example
* $('#example').DataTable( {
* columnDefs: [
* { type: 'kh-persian-numbers', targets: 0 }
* ]
* } );
*/
jQuery.extend(jQuery.fn.dataTableExt.oSort, {
"kh-persian-numbers-pre": function (a) {
function toEnglishNumber(strNum) {
var pn = ["۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"];
var en = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
var cache = strNum;
for (var i = 0; i < 10; i++) {
var regex_fa = new RegExp(pn[i], 'g');
cache = cache.replace(regex_fa, en[i]);
}
return cache;
}
return parseFloat(toEnglishNumber(a))
},
"kh-persian-numbers-asc": function (a, b) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0))
},
"kh-persian-numbers-desc": function (a, b) {
return ((a < b) ? 1 : ((a > b) ? -1 : 0))
}
});