0

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.

5
  • post the json you load Commented Oct 12, 2016 at 7:15
  • @Sajeetharan : All json value coming from database.I am updating my post again. Commented Oct 12, 2016 at 7:21
  • I presume you're appending the select with your $scope.listOfRestaurant array? If so, add the Select Business Name option first then append the rest Commented Oct 12, 2016 at 7:25
  • can u plz post a plunkr or somethng. Commented Oct 12, 2016 at 7:25
  • @DarrenSweeney : I am adding Select Business Name for 0th index. Check. Commented Oct 12, 2016 at 7:30

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

but i can not get this value $scope.Quarter.value.its throwing error.
hi satya, i ve created fiddle for u just check, hope this will help you . jsfiddle.net/upj8n47m/1
0

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

2 Comments

@ p2 : i can oreder properly but 0th index text is coming 2 times. Please check my post again.
@satya: if you can show some sample of response that would really help me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.