1

I'm working on Angularjs and I need to set my drop down list in alphabetical order.

Here is the ng-option:

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

I'm new to Angularjs.

halfer
  • 19,824
  • 17
  • 99
  • 186
Chethana C
  • 47
  • 1
  • 7
  • 2
    possible duplicate http://stackoverflow.com/questions/12310782/sorting-dropdown-alphabetically-in-angularjs – Sa E Chowdary Dec 20 '16 at 06:25
  • @Sa E Chowdary tried it but not working, because i'm using the function showname to remove the numbers and spaces in my drop down data. – Chethana C Dec 20 '16 at 06:33
  • well,we cant guess what your doing with your function calls right.....so answered according to the data provided by you.as the accepted answe in that you need to pass key to the order by – Sa E Chowdary Dec 20 '16 at 06:42
  • if you provide your data in $scope.baseBranches then we can give you the ecpected output – Sa E Chowdary Dec 20 '16 at 06:44

3 Answers3

4

You can use Order by filter in angular 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>

If you are sure that your option format is something like this [number][space][word] or you need to sort depend up on the word after space, you can write a custom filter for that. I have attached a sample code snippet with a custom filter here with.

<!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>
I have added a custom filter which sorts the array depending on the word after space and order the ng option depending on that. You can find the details of ng-options here.
Nitheesh
  • 19,238
  • 3
  • 22
  • 49
  • @ Nitheesh its correct but my json data is in this format : "branchName": "0082 MAHESANA", – Chethana C Dec 20 '16 at 06:38
  • @ Nitheesh i was used a function showname to remove the numbers and spaces in my drop down data. And the requirement is to show it in Alphabetical order. – Chethana C Dec 20 '16 at 06:39
  • Then try using orderBy: showName(branch.branchName). Im not sure about the result in this case. Please check that. – Nitheesh Dec 20 '16 at 06:44
  • @ChethanaC Are you sure that the name will comes in a unique format say [number][space][word] ? – Nitheesh Dec 20 '16 at 09:10
3

ng-repeat is working for me , check the 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>
Mahesh Kumar
  • 138
  • 1
  • 9
1

try with this

 <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>
Darshak
  • 859
  • 7
  • 18