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

标签 javascript angularjs

这是表格:

<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 must always be the last expression

您可以阅读它 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>

关于javascript - orderBy 不对以下 ngRepeat 表中的项目进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34086531/

相关文章:

javascript - 在溢出 :hidden 的 div 父级内显示弹出窗口

javascript - Angular 指令 - 将绝对定位的死者宽度设置为根的宽度

javascript - 从 FACEBOOK iframe 中的选择下拉列表简单重定向到 URL

javascript - IOS - 如果仪器测试失败则终止应用程序

javascript - Angular ng 构建给我一个错误

javascript - React Bootstrap 模式 es6

javascript - 模板中的 AngularJS 调试

javascript - 什么时候应该使用 call() 与直接调用函数?

javascript - 指令不返回 html 元素

angularjs - 使用 Retangular 处理错误代码 (401, 403)