2015-12-04 47 views
1

這是表:ORDERBY不排序在以下ngRepeat表項

<table class="table table-striped"> 
    <thead> 
    <tr> 
     <th class="col-md-1">Name</th> 
    </tr> 
    </thead> 
    <tbody> 
    <tr reportrowgroup="" ng-repeat="report in reporttree track by $index | orderBy:sortBy" report="report" rowindex="index" class="ng-scope ng-isolate-scope"> 
     <td><a href="#">Marcos Dima</a></td> 

(etc...) 

這是下拉ORDERBY:

<select class="pull-right" ng-model="sortBy"> 
    <option value="createdAt">Sort by: <strong>Date</strong></option> 
    <option value="adminRealname">Sort by: <strong>Username</strong></option> 
</select> 

reporttree(樣品)含量:

[ 
    { 
    "createdAt": "2015-08-12T13:06:54.901Z", 
    "adminRealname": "Marcos Dima", 
    }, 
    { 
    "createdAt": "2015-12-12T13:06:54.901Z", 
    "adminRealname": "Another name", 
    }, 

(etc...) 

但是,當我從下拉列表中選擇adminRealnamecreatedAt時 發生。難道我做錯了什麼?

編輯:

該表是一個指令,它是在一個單獨的表。也許這就是問題所在?

  ' </tr>'+ 
      '</thead>'+ 
      ' <tbody>'+ 
      '  <!-- <tr ng-repeat="report in reporttree track by $index"> -->'+ 
      '  <tr reportrowgroup ng-repeat="report in reporttree | orderBy:sortBy" report="report" rowindex="index"></tr>'+ 
      ' </tbody>'+ 
      '<!-- </div> -->'+ 
      '</table>' 
+0

嘗試orderBy:'sortBy' – Gianmarco

+0

您的下拉菜單和表屬於同一個控制器嗎?所以這兩個引用山姆實體sortBy。 –

+0

只要你的​​的{{adminRealname}}格式 – Jax

回答

1

track by必須始終是最後一個表達式

你可以閱讀它here

請參閱工作jsFiddle

var myApp = angular.module('application',[]); 

myApp.controller('TestController', function ($scope) { 
    $scope.data = [{ 
     createdAt: "2015-08-15T13:06:54.901Z", 
     adminRealname: "Marcos Dima"  
     },{ 
     createdAt: "2015-12-15T13:06:54.901Z", 
     adminRealname: "Another name"  
    }]; 
}); 

和HTML

<div ng-controller="TestController as vm"> 
     <select class="pull-right" ng-model="sortBy"> 
      <option value="createdAt">Sort by: <strong>Date</strong></option> 
      <option value="adminRealname">Sort by: <strong>Username</strong></option> 
     </select> 
     <table class="table table-striped"> 
     <thead> 
      <tr> 
       <th class="col-md-1">Name</th> 
      </tr> 
     </thead> 
     <tbody> 
      <tr ng-repeat="dataItem in data | orderBy:sortBy track by $index" report="report"> 
       <td><a href="#">{{dataItem.adminRealname}}</a></td> 
      </tr> 
     </tbody> 
    </table> 
</div>