Raj
Raj

Reputation: 1724

OrderBy not working in AngularJS

I am using orderBy to order the drop down list but it is not working . Can someone please suggest on this ? Please let me know if you need any other information ..

<select class="selectbox form-control" class="form-control" data-ng-model="ctrl.task.delegateFromId" data-ng-options="user.marsId as user.fullName for user in ctrl.userList | orderBy:'fullName' ">
   <option value="">-- Select Area Owner --</option>
</select>

here is plunker for this question 1st drop down order by is working but for 2nd it is not working

Upvotes: 0

Views: 69

Answers (2)

Amulya Kashyap
Amulya Kashyap

Reputation: 2373

If you want names to be sorted alphabetically then try this :

<select class="selectbox form-control" class="form-control" data-ng-model="ctrl.task.delegateFromId" data-ng-options="user.marsId as user.fullName for user in ctrl.userList | orderBy:'toString()' ">
   <option value="">-- Select Area Owner --</option>
</select>
  1. You need to add ng-model to correctly make the binding works for a list of strings.
  2. You can use toString() to sort if the input contains a list of strings. Since the expression of orderBy can be a Getter function. The result of this function will be sorted using the <, =, > operator.

Upvotes: 0

Hadi
Hadi

Reputation: 17289

I don't know what is your problem but i put simple example.

var myapp = angular.module('myapp', []);
myapp.controller('Ctrl', function ($scope) {
  var vm = this;
    vm.userList =JSON.parse('{"11":{"marsId":"11","firstName":"cc","lastName":"cc","fullName":"cc cc"},"12":{"marsId":"12","firstName":"aa","lastName":"aa","fullName":"aa aa"},"45":{"marsId":"45","firstName":"kk","lastName":"kk","fullName":"kk kk"}}');
  
  
  vm.userList2 = [];
for (var key in vm.userList) {
  var tempObj = "";
  tempObj = vm.userList[key];
  vm.userList2.push(tempObj);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp" ng-controller="Ctrl as vm">
        <select class="selectbox form-control" class="form-control" data-ng-model="vm.task.delegateFromId" data-ng-options="user.marsId as user.fullName for user in vm.userList2 | orderBy:'fullName' ">
   <option value="">-- Select Area Owner --</option>
          
</select>
</div>

Upvotes: 1

Related Questions