如何在angularjs中单击按钮时使用ng重复来按升序和降序排序

时间:2019-01-13 18:18:08

标签: javascript html angularjs

我有一个下拉列表,并且有几个div,一旦您更改了下拉列表,div就会基于json和selection进行创建。这一切都很好。但是,一旦我选择了任何选项,我就想按一下升序对div进行排序我再次想根据h4标签的值按第二个按钮的降序对div排序。这是代码和更新的代码。 https://plnkr.co/edit/XEzYi1JEeOtvqC6xn9jZ?p=preview

HTML

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.4.7/angular-filter.js"></script>
<script src="script.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<select class="change" ng-model="x" ng-change="update()">
<option value="technology">technology</option>
<option value="vertical">vertical</option>
</select>
<div><button>Sort-ascending alphabetical order</button></div>
<div><button>Sort-descending alphabetical order</button></div>
<div ng-repeat="(key, value) in players | groupBy: attr" ng-attr-id="{{key}}">
  <h4>Group name: {{ key }}{{value.length}}</h4>
  <p ng-repeat="player in value">
    player: {{ player.name }} 
  </p>
</div>
</div>

script.js

var app = angular.module("myApp", ['angular.filter']);
app.controller("myCtrl", function($scope) {
$scope.players = [
    {
      name: 'projectjava',
      symptom:'fever',
      technology:'java',
       vertical:'insurance',      
      id:'1'
    },

    {
      name: 'projecttabulue',
      symptom:'diesease',
      technology:'tabulue',
       vertical:'Banking',    
      id:'3'
    },
    {
      name: 'projectpython1',
      symptom:'diesease',
      technology:'python',
       vertical:'Health care',    
      id:'3'
    },
    {
     name: 'projectpython2',
      symptom:'colds',
      technology:'python',
       vertical:'Banking',    
      id:'2'
    }

    ];

     $scope.update = function() {      
  if($scope.x == 'technology'){
   $scope.id='technology';
    $scope.attr = 'technology'; 


   }
   if($scope.x == 'vertical'){
   $scope.id='vertical';
   $scope.attr = 'vertical';    
   }
   }
});

1 个答案:

答案 0 :(得分:0)

要按升序/降序排序,可以在ng-repeat中使用orderBy属性。 Reference Docs

谢谢