angularjs在数组上排序orderby

时间:2016-08-31 07:52:12

标签: javascript angularjs sorting

我有像

这样的数据
{ column : [a,b,c], data : [["a",1,2],["b",4,5],["c",3,2]]}

表格结构

<thead>
<tr>
  <th ng-repeat="n in column" ng-click="click($index)">n</th>  
</tr>
</thead>
<tr ng-repeat="i in data | orderBy:sortType:sortReverse ">
  <td ng-repeat="j in i">i</td>
</tr>

如何在这里制作排序逻辑?

1 个答案:

答案 0 :(得分:4)

您可以使用自定义功能尝试订购。以下是一个例子。希望这会有所帮助。

&#13;
&#13;
angular.module("app", []);
angular.module("app").controller("Test", function($scope) {
  $scope.sortReverse = false;
  $scope.column = ['a', 'b', 'c'];
  $scope.data = [
    ["a", "1", "2"],
    ["b", "4", "5"],
    ["c", "3", "2"]
  ];

  $scope.sort = function(index) {
    $scope.sortColumnIndex = index;
    $scope.sortReverse = !$scope.sortReverse;
  };

  $scope.sortByIndex = function(item) {
    return item[$scope.sortColumnIndex];
  };

});
&#13;
<html ng-app="app">

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
</head>

<body>
  <div ng-controller="Test">
    <table>
      <thead>
        <tr>
          <th ng-repeat="n in column" ng-click="sort($index)">{{n}}</th>
        </tr>
      </thead>
      <tr ng-repeat="i in data | orderBy:sortByIndex:sortReverse">
        <td ng-repeat="j in i">{{j}}</td>
      </tr>
    </table>
  </div>
</body>

</html>
&#13;
&#13;
&#13;