簡體   English   中英

orderBy來自ng-repeat的字段,帶有ui-sortable

[英]orderBy a field from ng-repeat with ui-sortable

我正在使用ui-sortable在兩個對象數組之間進行拖放,我需要在按名稱字段拖放后對數組中的對象進行排序。

這是我在html文件中的代碼:

<body ng-controller="listCtrl">
  <ul ui-sortable="playerConfig" ng-model="players">
    <li ng-repeat="player in players">
      <!--| orderBy: ['name']-->
      {{player.name}}
    </li>
  </ul>
</body>

 <style type="text/css">
     .beingDragged {
    height: 24px;
    margin-bottom: .5em !important;
    border: 2px dotted #ccc !important;
    background: none !important;
 }
 </style>

在控制器中:

angular.module('app', [
    'ui.sortable'
]).

controller('listCtrl', function ($scope) {

  var baseConfig = {
      placeholder: "beingDragged"
  };

  $scope.playerConfig = angular.extend({}, baseConfig, {
      connectWith: ".players"
  });

  $scope.players = [
      { 
        "name" : "John",
        "id" : "123",
        "score": "456"
      },
      { 
        "name" : "Tom",
        "id" : "234",
        "score": "678"
      },
      { 
        "name" : "David",
        "id" : "987",
        "score": "5867"
      }
   ];

我做了一些搜索,發現github中報告的類似問題為https://github.com/angular-ui/ui-sortable/issues/70 ,但是,Plunker代碼使用了orderByFilter,我無法找到源代碼。 不確定是否有人有類似的問題,可以指出我如何解決這個問題? 謝謝。

orderByFilter是AngularJS的一部分,因此您已經可以訪問它。

就像您找到的示例一樣,您可以將其注入控制器並使用它:

app.controller('MyController', function ($scope, orderByFilter) {

  $scope.players = [{ name: 'Eve'}, { name: 'Adam'}];

  $scope.sorted = orderByFilter($scope.players, 'name');
});

這相當於:

app.controller('MyController', function ($scope, $filter) {

  $scope.players = [{ name: 'Eve'}, { name: 'Adam'}];

  var orderByFilter = $filter('orderBy');
  $scope.sorted = orderByFilter($scope.players, 'name');
});

要不就:

$scope.sorted = $filter('orderBy')($scope.players, 'name');

直接注入orderByFilter而不是$filter只是一個捷徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM