通过组合角度的两个字段来排序

时间:2021-08-27 07:43:37

I know that it is possible to sort a table in angular with two column separately, i.e. first sort by name then sort by family.

我知道可以分别用两个列对角度表进行排序,即按名称排序,然后按族排序。

https://plnkr.co/edit/bJIIlmGLyGWsxD5AQti0

var friends = [
  {name: 'a',   family: 'z',  age: 10},
  {name: 'z',   family: 'b',  age: 19},
  {name: 'c',   family: 'z',  age: 21},
  {name: 'z',   family: 'd',  age: 35},
  {name: 'z',  family: 'e',  age: 29}
];

$scope.propertyName = 'age';
$scope.reverse = true;
$scope.friends = orderBy(friends, $scope.propertyName, $scope.reverse);

$scope.sortBy = function(propertyName) {
  $scope.reverse = (propertyName !== null && $scope.propertyName === propertyName)
      ? !$scope.reverse : false;
  $scope.propertyName = propertyName;
  $scope.friends = orderBy(friends, ['name','family'], $scope.reverse);
};
}]);
})(window.angular);

I wonder if it is possible to sort them by combining the two fields, e.g order by "name" and "family" alphabetically, so it is appearing as "a,b,c,d,e"?

我想知道是否可以通过组合两个字段来对它们进行排序,例如按字母顺序排列“name”和“family”,因此它显示为“a,b,c,d,e”?

2 个解决方案

#1


1  

You can simply pass an Array to angular orderBy filter, this way:

你可以简单地将一个数组传递给angular orderBy过滤器,这样:

ng-repeat="friend in friends | orderBy:['name', 'family']"

UPDATE: after question change by OP, I ask you to better state the requested ordering function, since it is not very clear (to me) from the question...

更新:在OP更改问题后,我要求您更好地说明所请求的订购功能,因为对我来说问题不是很明确(对我而言)......

#2


0  

<div ng-repeat="student in students | orderBy:['name','family']">
    {{student.name}}-{{student.family}}
</div>

#1


1  

You can simply pass an Array to angular orderBy filter, this way:

你可以简单地将一个数组传递给angular orderBy过滤器,这样:

ng-repeat="friend in friends | orderBy:['name', 'family']"

UPDATE: after question change by OP, I ask you to better state the requested ordering function, since it is not very clear (to me) from the question...

更新:在OP更改问题后,我要求您更好地说明所请求的订购功能,因为对我来说问题不是很明确(对我而言)......

#2


0  

<div ng-repeat="student in students | orderBy:['name','family']">
    {{student.name}}-{{student.family}}
</div>