satya
satya

Reputation: 3560

Cannot order drop down list value alphabetically using Angular.js

The drop down list can not ordered alphabetically using Angular.js. Here is my code:

<div class="input-group bmargindiv1 col-md-12">
<span class="input-group-addon ndrftextwidth text-right" style="width:180px">Business Name :</span>
<select class="form-control"  id="restau" ng-model="restaurant" ng-options="qua.name for qua in listOfRestaurant | orderBy:'name' track by qua.value" ng-change="getDayFromSpecial('restau');">
 </select>
</div> 

$scope.listOfRestaurant=[{
        name:'Select Business Name',
        value:''
}]
$scope.restaurant=$scope.listOfRestaurant[0];

$http({
        method:'GET',
        url:"php/customerInfo.php?action=restaurant",
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    }).then(function successCallback(response){
        angular.forEach(response.data,function(obj){
            var data={'name':obj.rest_name,'value':obj.member_id};
            $scope.listOfRestaurant.push(data);
        })
    },function errorCallback(response) {
    })

Here my problem is I can order the list but Select Business Name is coming in middle in this list which should come only first and selected.

Upvotes: 0

Views: 104

Answers (2)

Anil Kumar Ram
Anil Kumar Ram

Reputation: 1221

you can write like this.

<select name="quarter" ng-model="Quarter" 
        ng-options="obj.value as obj for obj in options | orderBy:'toString()'">
        <option value="" >Select Business Name</option>
</select>

don't put your select business name in data list.

Upvotes: 1

Pawan B
Pawan B

Reputation: 4623

By using orderBy:'toString()' with ng-repeat you can sort list value alphabetically using angular.js

Example :

<select name="quarter" ng-model="Quarter" 
        ng-options="obj.value as obj for obj in options | orderBy:'toString()'">

</select>

Check this jsfiddle

Upvotes: 0

Related Questions