sortBy不对指令表中的项进行排序

sortBy not sorting items in directive table

本文关键字:排序 指令表 sortBy      更新时间:2023-09-26

HTML:

<select class="form-control" ng-model="sortBy">
  <option value="createdAt"><strong>Date</strong></option>
  <option value="ProgressStateNumber"><strong>Progress</strong></option>
  <option value="adminUsername"><strong>adminUsername</strong></option>
</select>
<tbody>
  <tr
    reportrowgroup
      ng-repeat="report in reportTree track by $index | orderBy: sortBy"
      report="report"
    >

指令:

  .directive('reportrowgroup', function () {
    return {
      restrict: 'EA',
      template:
        '<td>{{report.createdAt | date:"yyyy-MM-dd HH:mm"}}</td>'+
        'MORE HTML'
        scope: {
            report: '='
        },
        controller: function() {
        },
        link: function(scope,elem,attr,ctrl) {
      }
        }
    }
})

一切正常,只是当我在select中选择createdAt时,表不按createdAt排序。

可能是什么问题?

您的reportrowgroup指令正在创建一个新的scope,因此缺少对同一sortBy变量的引用。尝试将sortyBy包装在对象中。

<!-- markup -->
<select class="form-control" ng-model="input.sortBy">
// Code
angular.module('myApp').controller('MyController', function($scope) {
  $scope.input = {
    sortBy: 'default-value-goes-here'
  };
});

查看此plunker:http://plnkr.co/edit/MmjuaLfvVnOQb0Ngz0dI?p=preview