简体   繁体   中英

Angularjs sorting using orderBy is not working for me

I am trying to sort my home list based on ratings, pricing and total counts. Unfortunately not working for me, I am new in angularjs. Here I have my html code. Actually it is sorting the things but not in order. ->When user clicks on price btn he should get home with highest price first then lower , lowest etc.(5000,4000,3000) if again he clicks then order must be (3000,4000,5000). Similar things for total counts and rating. I have also created plunker to edit your suggestions. please edit and provide me the working link. Here is demoForEdit .

//html code
<body ng-controller="MainCtrl as ctrl">
    <div class="col-md-offset-2">
      <button ng-click="orderByPopularity='total_counts'; reverseSort = !reverseSort" class="btn btn-sm btn-info">sort on total counts </button>
      <button  ng-click="orderByPopularity='price'; reverseSort = !reverseSort" class="btn btn-sm btn-danger">sort on Price</button>    
      <button  ng-click="orderByPopularity='avg'; reverseSort = !reverseSort" class="btn btn-sm btn-success">sort on ratings</button>    
    </div>
  <br>
<div ng-repeat="home in homes | orderBy:orderByPopularity:reverseSort">
  <div class="panel panel-primary  ">
    <div class="panel-heading">
      Home name: {{home.home_name}}
    </div>
      <div class="panel-body">
        <div class="panel panel-body">
          <table>
          <p>total counts:{{home.total_counts}}</p>
          <p>city:{{home.city}}</p>
          <p>Price:{{home.price}}</p>
          <div ng-repeat="rate in rating">
          <!--  looping inside ng-repeat rating is seprate data but has home id-->
            <div ng-if="rate.home_id==home.id">
            Home raintgs:{{rate.avg}}  
          </div>
        </div>
      </div>
    </div>
  </div>  
</div>

//My app.js code
var app = angular.module('plunker', []);
    app.controller('MainCtrl', function($scope) {
      $scope.homes=[{"id":"48","home_name":"Swastik","city":"Banglore","zipcode":"888888","total_counts":"1","price":"5000"},
    {"id":"49","home_name":"Khushboo","city":"Chennai","zipcode":"456786","total_counts":"17","price":"3333"},
    {"id":"50","home_name":"Raj","city":"Hydrabad","zipcode":"123455","total_counts":"2","price":"20000"}];
    $scope.orderByPopularity ='total_counts';
    $scope.reverseSort = true;
    $scope.rating=[{"home_id":"48","avg":"3.5"},
    {"home_id":"49","avg":"2"},
    {"home_id":"50","avg":"4"}
    ];
    });

It does sort, but your total_counts values are strings, so it orders them lexicographically. If you want to order by number, which I assume you do, you need to remove the quotes and have their values as the number themselves, ie: "total_counts":17 etc..

EDIT : Modified so you could keep your values as strings (inside the custom sorting function).

As for avg rating ordering, you can't do that since your ordering is on $scope.homes , and this array's objects don't have the avg property, that is a property for your other array - $scope.ratings . To sort by that property, you'll have to create your own custom function.

$scope.myValueFunction = function(o) {

   if ($scope.orderByPopularity == "avg") {
     var obj;
     for (var i = 0; i < $scope.rating.length; i++) { 
        if ($scope.rating[i].home_id == o.id) {
          obj = $scope.rating[i]
        }
     }
     return obj.avg;
   }
   else  {
     return parseInt(o[$scope.orderByPopularity]); 
   }

};

And in the HTML:

<div ng-repeat="home in homes | orderBy:myValueFunction:reverseSort">

Look at this Plunker as for a demonstration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM