ng-grid的默认排序不是字母数字。 是否可以应用自定义排序函数以获得正确的排序,如:
'11''2''eé''ed'=> '2''11''ed''eé'
答案 0 :(得分:3)
您可以定义your own sorting function。
这是一个example with your data。 (使用Devin Torres' Natural Sorting algorithm)
修正了它var myAwesomeSortFn = function(a,b){
var NUMBER_GROUPS = /(-?\d*\.?\d+)/g;
var myAwesomeSortFn = function(a, b) {
var aa = String(a).split(NUMBER_GROUPS),
bb = String(b).split(NUMBER_GROUPS),
min = Math.min(aa.length, bb.length);
for (var i = 0; i < min; i++) {
var x = parseFloat(aa[i]) || aa[i].toLowerCase(),
y = parseFloat(bb[i]) || bb[i].toLowerCase();
if (x < y) return -1;
else if (x > y) return 1;
}
return 0;
};
$scope.myData = [{name: "Moroni", age: 11},
{name: "Tiancum", age: 2},
{name: "Jacob", age: 'eé'},
{name: "Nephi", age: 'ed'}];
$scope.gridOptions = {
data: 'myData',
columnDefs: [{field: 'name', displayName: 'Name'}, {field:'age', displayName:'Age', sortFn: myAwesomeSortFn}]
};
答案 1 :(得分:0)
这是我到目前为止所做的:
$scope.links = {
sortFun: function(a, b) { console.log('a:' + a + ', b: ' + b); }
};
...
$scope.data.grid.columnDefs.push(
{'field': key, 'sortingAlgorithm': $scope.links.sortFun }
);
...
$scope.data.grid.columnDefs.push({'field': key, 'sortingAlgorithm': $scope.links.sortFun });