2016-12-20 32 views
1

我正在使用Angularjs,我需要按字母順序設置我的下拉列表。如何使用Angularjs在ng選項中使用字母順序?

這裏是NG選項:

<div class="form-group"> 
    <label>Branch : <i class="mandate">*</i></label> 
    <select class="form-control input-md" ng-model="query.branch" ng-options="showName(branch.branchName) as showName(branch.branchName) for branch in baseBranches | orderBy:'toString()'" name="branchName"> 
    <option value="" selected>Select Branch</option> 
    </select> 
    <span class="error" ng-show="search_form.branchName.$error.required">Branch is required</span> 
</div> 

我是新來Angularjs。

+2

可能重複http://stackoverflow.com/questions/12310782/so​​rting-dropdown-alphabetically- in-angularjs –

+0

@Sa E Chowdary嘗試了它,但沒有工作,因爲我正在使用函數showname刪除我的下拉數據中的數字和空格。 –

+0

好吧,我們不能猜測你對你的功能調用正確的做什麼.....所以根據你提供的數據來回答。作爲接受的答案,你需要通過 –

回答

4

您可以通過過濾器的角度JS使用秩序。

<select class="form-control input-md" 
     ng-model="query.branch" 
     ng-options="showName(branch.branchName) as showName(branch.branchName) for branch in baseBranches | orderBy:'branchName'" name="branchName"> 
    <option value="" selected>Select Branch</option> 
</select> 

如果您確信您選擇的格式是這樣的[number][space][word],或者你需要理清了依賴於空間以後的話,你可以寫一個自定義過濾器。我附上了一個自定義過濾器的示例代碼片段。

<!DOCTYPE html> 
 
<html> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script> 
 

 
<body> 
 

 
    <div ng-app="myApp" ng-controller="myCtrl"> 
 

 
     <select ng-model="selectedName" ng-options="item.Name for item in names | customSorter:'Name'"> 
 
     </select> 
 

 
    </div> 
 

 
    <script> 
 
     var app = angular.module('myApp', []); 
 
     app.controller('myCtrl', function ($scope) { 
 
      $scope.names = [{ 
 
        "Id": 0, 
 
        "Name": "10095 Gil" 
 
      }, { 
 
        "Id": 1, 
 
        "Name": "00085 Tobias" 
 
      }, 
 
       { 
 
        "Id": 2, 
 
        "Name": "145 Linus" 
 
      }]; 
 
     }); 
 
     app.filter('customSorter', function() { 
 
      return function (items, field) { 
 
       var filtered = []; 
 
       angular.forEach(items, function (item) { 
 
        filtered.push(item); 
 
       }); 
 
       filtered.sort(function (a, b) { 
 
        return (a.Name.split(' ')[1] > b.Name.split(' ')[1]); 
 
       }); 
 

 
       return filtered; 
 

 
      }; 
 
     }); 
 
    </script> 
 

 
</body> 
 

 
</html>
我添加自定義過濾器,其排序取決於空間後的字陣列,並命令 ng option取決於。 你可以找到 ng-options here.

+0

在這裏找一個小提琴http://jsfiddle.net/aBccw/ – Nitheesh

+0

@ Nitheesh它的正確,但我的JSON數據是這樣的格式:「branchName」:「0082 MAHESANA 「, –

+0

@ Nitheesh我使用了一個函數showname刪除我的下拉數據中的數字和空格。要求是按字母順序顯示。 –

1

嘗試用這種

<div class="form-group"> 
    <label>Branch : <i class="mandate">*</i></label> 
    <select class="form-control input-md" ng-model="query.branch" ng-options="showName(branch.branchName) as showName(branch.branchName) for branch in baseBranches | orderBy:'branchName'"> 
    <option value="" selected>Select Branch</option> 
    </select> 
    <span class="error" ng-show="search_form.branchName.$error.required">Branch is required</span> 
</div> 
3

NG重複的細節工作對我來說,檢查pen

<div class="form-group"> 
     <label>Branch : <i class="mandate">*</i></label> 
     <select > 
     <option value="" selected>Select Branch</option> 
     <option ng-repeat=" item in main.items | orderBy:'toString()'" value="  {{item}}">{{item}}</option> 
     </select> 

    </div>