AngularJS - 对多个列进行排序,包括字符串格式的日期

时间:2015-05-20 09:06:44

标签: javascript angularjs angular-filters

是否可以对angularjs过滤器orderBy中的多个列进行操作和排序?

Fiddle

JS

var myApp = angular.module('myApp', []);

function MyCtrl($scope, $filter) {
    var r = [
        {'group':1,'sub':1, 'date': '11/4/2014 2:01 PM'}, 
        {'group':2,'sub':10, 'date': '11/4/2014 4:36 PM'}, 
        {'group':1,'sub':2, 'date': '11/4/2014 8:59 AM'},
        {'group':1,'sub':20, 'date': '4/14/2015 5:54 PM'},
        {'group':2,'sub':1, 'date': '11/3/2014 9:09 PM'},
        {'group':2,'sub':11, 'date': '9/4/2014 2:48 PM'}
    ];

    $scope.mcards = $filter('orderBy')(r, [function(a){
        return -new Date(a.date).getTime(); 
        //manipulate the string and then sort it by ascending or descending order.
    }]);
}

它没有按预期工作,而当我按照以下方式尝试它时效果很好,

$scope.mcards = $filter('orderBy')(r, ['group', function(a){
    return -a.sub;
}]);

这里有什么问题,我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

删除过滤器

中的减号符号表单
 var myApp = angular.module('myApp', []);

function MyCtrl($scope, $filter) {
    var r = [
        {'group':1,'sub':1, 'date': '11/4/2014 2:01 PM'}, 
        {'group':2,'sub':10, 'date': '11/4/2014 4:36 PM'}, 
        {'group':1,'sub':2, 'date': '11/4/2014 8:59 AM'},
        {'group':1,'sub':20, 'date': '4/14/2015 5:54 PM'},
        {'group':2,'sub':1, 'date': '11/3/2014 9:09 PM'},
        {'group':2,'sub':11, 'date': '9/4/2014 2:48 PM'}
    ];

    $scope.mcards = $filter('orderBy')(r, [function(a){        
        return new Date(a.date).getTime();
    }],true);

    $scope.cards = $filter('orderBy')(r, ['group', function(a){
        return a.sub;
    }],true);
}

如果要对反向排序进行排序,请在过滤器的最后一个参数中添加true

请参阅jsfiddle update http://jsfiddle.net/br5sssv7/6/