在我的国家,表达数字的惯例是使用.作为千位分隔符和小数分隔符.一个例子是:25.367,212我无法使datatables.js对使用此格式的任何列进行排序.
我使用以下扩展名:
//formatted number sort jQuery.extend(jQuery.fn.dataTableExt.oSort, { "formatted-num-pre": function (a) { a = (a === "-" || a === "") ? 0 : a.replace(/[^\d\-\.]/g, ""); return parseFloat(a); }, "formatted-num-asc": function (a, b) { return a - b; }, "formatted-num-desc": function (a, b) { return b - a; } }); //formatted number autodetection jQuery.fn.dataTableExt.aTypes.unshift( function (sData) { var deformatted = sData.replace(/[^\d\-\.\/a-zA-Z]/g, ''); if ($.isNumeric(deformatted) || deformatted === "-") { return 'formatted-num'; } return null; } );
我搜索了数据表文档和论坛,但没有找到解决方案.有什么建议?我使用的是jquery 1.9.1和datatables 1.9.4
我最终通过以下方式解决了这个问题:
jQuery.fn.dataTableExt.oSort['numeric-comma-asc'] = function (a, b) { //remove the dots (.) from the string and then replaces the comma with a dot var x = (a == "-") ? 0 : a.replace(/\./g, "").replace(/,/, "."); var y = (b == "-") ? 0 : b.replace(/\./g, "").replace(/,/, "."); x = parseFloat(x); y = parseFloat(y); return ((x < y) ? -1 : ((x > y) ? 1 : 0)); }; jQuery.fn.dataTableExt.oSort['numeric-comma-desc'] = function (a, b) { var x = (a == "-") ? 0 : a.replace(/\./g, "").replace(/,/, "."); var y = (b == "-") ? 0 : b.replace(/\./g, "").replace(/,/, "."); x = parseFloat(x); y = parseFloat(y); return ((x < y) ? 1 : ((x > y) ? -1 : 0)); }; //numeric comma autodetect jQuery.fn.dataTableExt.aTypes.unshift( function (sData) { //include the dot in the sValidChars string (don't place it in the last position) var sValidChars = "0123456789-.,"; var Char; var bDecimal = false; /* Check the numeric part */ for (i = 0 ; i < sData.length ; i++) { Char = sData.charAt(i); if (sValidChars.indexOf(Char) == -1) { return null; } /* Only allowed one decimal place... */ if (Char == ",") { if (bDecimal) { return null; } bDecimal = true; } } return 'numeric-comma'; } );