【问题标题】:angularjs sort orderby on arrayangularjs在数组上排序orderby
【发布时间】:2016-08-31 18:30:01
【问题描述】:

我有类似的数据

{ 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>

这里怎么做排序逻辑??

【问题讨论】:

  • 你能再具体一点吗?你想怎么排序?
  • @UjjwalOjha 在单击 th(根据标题)时进行排序,就像通​​常在数据表中所做的那样
  • 您想在单击列“a”时按索引 0 排序,当单击列“b”时按索引 1 排序等等?
  • @Sarathy 是的

标签: javascript angularjs sorting


【解决方案1】:

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

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];
  };

});
<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多