0

目前,我没有任何可能通过使用orderBy-filter来排序列表中的列。问题是我有一个嵌套的ngRepeat。嵌套ng-repeat中的列排序(AngularJS)

查看:

<md-list> 
     <md-list-item> 
      <span ng-repeat="item in ::hItems track by $index" ng-click="sortBy(item)" flex> 
       {{ ::item }} 
      </span> 
     </md-list-item> 
     <md-divider></md-divider> 
     <md-list-item ng-repeat="cItem in ::cItems | orderBy:sortType:sortReverse track by $index"> 
      <span ng-repeat="(key, value) in ::cItem track by $index" flex> 
       {{ ::value }} 
      </span> 
      <md-divider></md-divider> 
     </md-list-item> 
</md-list> 

只要列上的用户点击标题功能sortBy将被调用。该功能在控制器中实现如下:

//Default values: 
$scope.sortType = 'NAME'; 
$scope.sortReverse = false; 
var orderBy = $filter('orderBy'); 

//sortBy func: 
function sortBy(columnKey) { 
    $scope.sortType = columnKey; 
    $scope.sortReverse = !$scope.sortReverse; 
    $scope.cItems = orderBy($scope.cItems, $scope.sortType, $scope.sortReverse); 
} 

该列表仅排序为默认值name。这里是GET请求的阵列输出:

//JSON data 
[ 
    { 
     "ArtNo": "DE123", 
     "SHORTCODE": "ABC", 
     "NAME": "article one", 
     "QUANTITY": 3, 
     "GROUPID": 1, 
     "ACTIVE": 1 
    },... 
] 

所以,我需要,因为你可以看到如何在阵列与琴键号和对象值=>[0:Object, 1:Object...]定义嵌套ngRepeat。我只需要一个解决方案为我的sortBy功能。有没有人有想法?

以下是我的输出列表:

ArtNo | SHORTCODE | NAME   | QUANTITY 
DE123 | ABC001 | article one | 3 
DE456 | ABC002 | article two | 8 
DE789 | ABC003 | article three | 4 
DE321 | ABC004 | article four | 13 
.... 
+1

请简化您的问题代码并提供一个工作小提琴... –

+0

@EliasSoares您在我的代码中不理解什么?然后我可以编辑我的线程。 – yuro

+0

什么是'md-list','md-list-item','md-divider'?我知道这可能对问题没有影响,但是当您提供问题发生的最小代码时,您(和我们)很容易找到问题。 –

回答

1

如果我的理解没错,你想这种行为:

  • 用户点击所需的列进行排序,并在表的内容将被责令该栏。
  • 用户再次点击,排序方向反转。

对不对?

那你为什么要订购两次你的数组?

//Default values: 
$scope.sortType = 'NAME'; 
$scope.sortReverse = false; 
var orderBy = $filter('orderBy'); 

//sortBy func: 
function sortBy(columnKey) { 
    $scope.sortType = columnKey; 
    $scope.sortReverse = !$scope.sortReverse; 
    // You do not need to order anything here. Just define your orderby parameters here to be used on view. 
} 

真正解决

与问题笔者谈,我们没有发现的主要问题就在这里:

ng-repeat使用::语法避免了观看OrderBy更换过滤器的角度,这样的变化没有反映在观点上。

+0

我投了你的答案。我想它可以帮助我:) – yuro