orderBy不对以下ngRepeat表中的项目进行排序

orderBy not sorting items in the following ngRepeat table

本文关键字:项目 排序 ngRepeat orderBy      更新时间:2023-09-26

这是表:

<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...)

这是下拉订单按:

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

track by必须始终是最后一个表达式

你可以在这里阅读

请参阅工作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>