Mr Smith
Mr Smith

Reputation: 3486

Ordering options in ng-options with AngularJS

The select element shown below is populated with Angular 1.47 (code also shown). I'd prefer that the current value -45 bp - be at the top of the list, not the bottom. How can I accomplish this?

My dropdown

  <div class="modal-content">
      <div class="modal-header edit-modal" style="border-style: solid; border-width: 1px;">
        <button type="button" class="close" ng-click="close()" data-dismiss="modal" aria-hidden="true">×</button>
        <div>Test Dialog </div>
      </div>
      <div class="modal-body">
        <form class="form-horizontal ng-pristine ng-valid" role="form">
             <div class="row edit-modal">
                <div class="col-sm-1"></div>
                <div class="col-sm-5 text-left label-no-padding">Option  1</div>
                <div class="col-sm-5">
                 <select style="width:150px" ng-model="Option1" ng-options="Option1 as Option1.label for Option1 in dropdown.Option1 | orderBy:'orderId'" class="ng-pristine ng-valid ng-touched"></select>
                </div>
                <div class="col-sm-1"></div>
            </div>  
            <div class="row edit-modal">
                <div class="col-sm-1"></div>
                <div class="col-sm-5 text-left label-no-padding">Option 2</div>
                <div class="col-sm-5">
                 <select style="width:150px" ng-model="Option2" ng-options="Option2 as Option2.label for Option2 in dropdownValueMap.Option2| orderBy:'orderId'" class="ng-pristine ng-untouched ng-valid"></select>
                </div>
                <div class="col-sm-1"></div>
            </div>  
            <div class="row edit-modal">
                <div class="col-sm-1"></div>
                <div class="col-sm-5 text-left label-no-padding">Option 3</div>
                <div class="col-sm-5">
                <select style="width:150px" ng-model="Option3" ng-options="Option3 as Option3.label for Option3 in dropdown.Option3 | orderBy:'orderId'" class="ng-pristine ng-untouched ng-valid"></select>
                </div>
                <div class="col-sm-1"></div>
            </div>  
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" ng-click="closeSave()" class="btn btn-primary" data-dismiss="modal">OK</button>
        <button type="button" ng-click="close()" class="btn" data-dismiss="modal">Cancel</button>
      </div>
    </div>

Upvotes: 1

Views: 43

Answers (1)

Mistalis
Mistalis

Reputation: 18309

I saw that you are using orderBy filter.

  • If you use orderBy:'orderId', order is increasing (27, 28, 29, 30).
  • If you use orderBy:'-orderId', order is descending (30, 29, 28, 27).

Take a look to: https://docs.angularjs.org/api/ng/filter/orderBy

Hope I helped :)

Upvotes: 2

Related Questions