角度重复顺序不适用于嵌套范围

时间:2017-03-30 12:08:03

标签: javascript angularjs angularjs-ng-repeat angularjs-filter angularjs-orderby

我有一个html表,其中行是对象,数据是属性:

$scope.rows = [
    { row_id: 1,row_selected:false, data: { col_1: "a", col_2: "b", col_3: "c" } },
    { row_id: 2,row_selected:false, data: { col_1: "d", col_2: "e", col_3: "f" } },
    { row_id: 3,row_selected:false, data: { col_1: "g", col_2: "h", col_3: "i" } }
]

并在表格中:

   <table border="1">
     <tr>
       <th ng-repeat="col in cols | orderBy:'order':false">
         <a href ng-click='changeOrderBy(col)'>{{col.name}} [{{col.order}}]</a>
       </th>
     </tr>
     <tr ng-repeat="row in rows | orderBy:data[orderBy]:reverse | filter:filter">
       <td ng-repeat="col in cols | orderBy:'order':false">
         {{row.data[col.field]}}
       </td>
     </tr>
   </table>

我尝试使用orderBy:data [orderBy]和orderBy:'data [orderBy]'

changeOrderBy函数:

    $scope.changeOrderBy = function(col){
      if($scope.orderBy == col.field){
        $scope.reverse = !$scope.reverse;
      }
      $scope.orderBy = col.field;
    }

但我似乎没有订购行,我想这将是一个类似的解决方案来制作过滤器

jsFiddle

1 个答案:

答案 0 :(得分:0)

我找到了这个解决方案,添加前缀&#39;数据。&#39;在该领域之前,如下

orderBy = 'data.col_1'

JS:

$scope.orderBy = null;
$scope.reverse = false;
$scope.changeOrderBy = function(col){
  var prefix = 'data.';
  if($scope.orderBy == prefix + col.field){
    $scope.reverse = !$scope.reverse;
  }
  $scope.orderBy = prefix + col.field;
}

HTML:

<tr ng-repeat="row in rows | orderBy:orderBy:reverse">

updated jsFiddle