2016-10-12 60 views
0

我遇到問題。下拉列表無法按字母順序使用angular.js進行排序。我在下面解釋我的代碼。無法使用angular.js按字母順序排列下拉列表值使用angular.js

<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) { 
    }) 

這裏我的問題是,我可以對列表進行排序,但Select Business Name在中間進來該名單應該來僅第一和選擇。請幫我解決這個問題。

+0

發佈您加載的json – Sajeetharan

+0

@Sajeetharan:所有json值來自database.I更新我的文章。 – satya

+0

我假設你將追加到你的'$ scope.listOfRestaurant'數組中?如果是這樣,首先添加「選擇商家名稱」選項,然後附加其餘 –

回答

1

,你可以這樣寫。

<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> 

不要將您的選擇商業名稱放在數據列表中。

+0

但是我不能得到這個值'$ scope.Quarter.value'.its引發錯誤。 – satya

+0

可以嗎?請創建一個你的代碼的小提琴? –

+0

嗨satya,我創造了小提琴你只是檢查,希望這會幫助你。 http://jsfiddle.net/upj8n47m/1/ –

0

使用orderBy:'toString()'與NG-重複你可以使用angular.js

排序列表值 alphabetically

例子:

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

</select> 

入住這jsfiddle

+0

@ p2:我可以正確放棄,但是第0個索引文本是2次。請再次檢查我的帖子。 – satya

+0

@satya:如果你可以展示一些'response'的樣本,那真的可以幫助我 –