2

Im comparing two object arrays and displaying them with those that have the same object checkbox checked. I now want to sort the list the with checkbox check to be first as a default state. I also want to sort by clicking the header to sort by ascending order .

 <div>header</div>
    <div class="search" ng-repeat="items in products ">
        <label >{{item.name}}</label>

   <input type="checkbox" ng-model="item.checked" 
                          ng-click="checked(item,productlist)"
                          ng-checked=" checked(item,productlist)"> 
    </div>

I tried using orderBy:[‘-checked’,’item’]: true to sort the checked ones on top but its not working. Any ideas

2
  • 1
    You've forced the second argument to orderBy to true, which will always sort the items in reverse order. Also, specifying 'item' as a secondary ordering field might not work as you'd expect because it'd be comparing by object reference. plnkr.co/edit/quxAI4?p=preview Commented Sep 6, 2018 at 22:25
  • Thanks this works fine when checkbox are tick after page load but not when checkbox are pre-check before page load. Commented Sep 9, 2018 at 11:06

1 Answer 1

2

A few problems here...

  1. You have ng-repeat="items in products", but then you refer to item instead of items within this scope.

  2. You have this checked() method that you're passing to ng-click and ng-checked. What exactly is this doing? You should never be using ng-checked in conjunction with ng-model - https://docs.angularjs.org/api/ng/directive/ngChecked

  3. By passing '-checked' to orderBy, items will be ordered by their checked value in reverse, i.e. they will be ordered false first, true last. Then you're passing true as the reverse parameter to orderBy, so the list will be reversed again. You're reversing the ordering twice unnecessarily - https://docs.angularjs.org/api/ng/filter/orderBy

This might work better...

<div>header</div>
<div class="search" ng-repeat="item in products | orderBy: 'checked'">
    <label>{{item.name}}</label>
    <input type="checkbox" ng-model="item.checked">
</div>
Sign up to request clarification or add additional context in comments.

Comments

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.