【问题标题】:How to sort data with ng-click after already loaded with ng-click angular已加载 ng-click angular 后如何使用 ng-click 对数据进行排序
【发布时间】:2016-09-01 14:47:22
【问题描述】:

我正在尝试使用 ng-click 获取表中的信息以进行更改。我有加载、排序和正常工作的功能, 但我无法在两者之间切换。我的意思是,如果我点击 加载,我无法排序,反之亦然。我想是因为我重置了 每次点击的国家,但我不知道如何识别它们 如果我使用 ng-repeat 将它们放在不同的变量中。请帮忙!!

    <!DOCTYPE html>
      <html lang="en" ng-app="frontendTest">
      <head>
        <title>Frontend Test</title>
        <meta charset="utf-8"/> 

        <link rel="stylesheet" type="text/css" href="stylesheet.css">
        <link rel="stylesheet" type="text/css" href="resources/bootstrap/dist/css/bootstrap.min.css"> 

        <script type="text/javascript" src="resources/angular/angular.min.js"></script>
        <script type="text/javascript" src="resources/angular/angular-route.min.js"></script>
        <script type="text/javascript" src="app.js" ></script>
        <script type="text/javascript" src="controller.js" ></script>
      </head>
      <body ng-controller="CountriesController as countries">

        <header>
          <h1>Frontend Test</h1>  
          <div></div>
        </header>

        <section class="buttons">
          <input type="button" value="Load Countries" ng-click="load()">
          <input type="button" value="Sort Countries" ng-click="sort()">
        </section>

        <table class="table">
          <thead>
            <tr>
              <th class="col-md-6">Name</th>
              <th class="col-md-6">Population</th>
            </tr> 
          </thead>
         <tbody>
           <tr ng-repeat="country in countries track by $index" >            
             <td class="col-sm-6">{{country.country}}</td>
             <td  class="col-sm-6">{{country.population}}</td>
           </tr>
         </tbody>
        </table>                        
      </body>
    </html>

angular
  .module("frontendTest",[])
  .controller("CountriesController", CountriesController);

  CountriesController.inject =['$scope'];

  function CountriesController($scope){

    $scope.data = 
    [
      {"country" : "Aruba", "population" : 103000},
      {"country" : "Afghanistan", "population" : 22720000},
      {"country" : "Angola", "population" : 12878000},
      {"country" : "Anguilla", "population" : 8000},
      {"country" : "Albania", "population" : 3401200},
      {"country" : "Andorra", "population" : 78000},
      {"country" : "Netherlands Antilles", "population" : 217000},
      {"country" : "Zimbabwe", "population" : 11669000}
    ];

    $scope.countries= [];
    $scope.sorted = [];
    $scope.loaded = false;

    $scope.load = function (){
      //show all CountriesController and population
      console.log("LOAD");
      $scope.loaded = true;
      for(var i = 0; i < Object.keys($scope.data).length; i++){
        $scope.countries.push($scope.data[i]);
        // console.log($scope.countries);
      }
    }

    $scope.sort = function (){
      // if($scope.loaded === true){
      console.log("SORT");
      $scope.sortByCountry = $scope.data.slice(0);  
      $scope.sortByCountry.sort(function(a,b){
        //isolate countries and population sort by name            
        var x = a.country;
        var y = b.country;

        return x < y ? -1 : x > y ? 1 : 0;
      });

      for(var i = 0; i < $scope.sortByCountry.length; i++){
        // console.log($scope.sortByCountry[i]);  
        $scope.countries.push($scope.sortByCountry[i]);
        console.log($scope.sortByCountry[i]);

      } 
      // }
    }
  }

【问题讨论】:

    标签: javascript angularjs object


    【解决方案1】:

    问题是当您可以对现有数组进行排序并保持原样时,您正在追加到现有数组。我已经简化了您的代码,它应该可以按照您的意愿工作。您还可以使用来自 AngularJS 的 orderBy 帮助程序,这应该会减少代码量。

    angular
      .module("frontendTest",[])
      .controller("CountriesController", CountriesController);
    
      CountriesController.inject =['$scope'];
    
    function CountriesController($scope){
    
       $scope.data = 
       [
      {"country" : "Aruba", "population" : 103000},
      {"country" : "Afghanistan", "population" : 22720000},
      {"country" : "Angola", "population" : 12878000},
      {"country" : "Anguilla", "population" : 8000},
      {"country" : "Albania", "population" : 3401200},
      {"country" : "Andorra", "population" : 78000},
      {"country" : "Netherlands Antilles", "population" : 217000},
      {"country" : "Zimbabwe", "population" : 11669000}
      ];
    
      $scope.countries= [];
      $scope.sorted = [];
      $scope.loaded = false;
    
      $scope.load = function (){
        $scope.loaded = true;
        // Cloning data
        $scope.countries = JSON.parse(JSON.stringify($scope.data));
      }
    
    
    
      $scope.sort = function (){
        if($scope.loaded === true){ 
          $scope.countries.sort(function(a,b){
            var x = a.country;
            var y = b.country;
    
            return x < y ? -1 : x > y ? 1 : 0;
          });
        }
      }
    }
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
    <body ng-app="frontendTest" ng-controller="CountriesController as countries">
    
            <header>
              <h1>Frontend Test</h1>  
              <div></div>
            </header>
    
            <section class="buttons">
              <input type="button" value="Load Countries" ng-click="load()">
              <input type="button" value="Sort Countries" ng-click="sort()">
            </section>
    
    
            <table class="table">
              <thead>
                <tr>
                  <th class="col-md-6">Name</th>
                  <th class="col-md-6">Population</th>
                </tr> 
              </thead>
             <tbody>
               <tr ng-repeat="country in countries track by $index" >
    
                 <td class="col-sm-6">{{country.country}}</td>
                 <td  class="col-sm-6">{{country.population}}</td>
               </tr>
             </tbody>
            </table>
    
    
            </body>

    【讨论】:

    • 好的,完全有道理。我意识到我在重复自己,但我不知道如何保存数据并为排序功能操作它。我还会检查 orderby 并试一试。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多