0

In my view I am doing this:

<div ng-repeat="item in items">
  {{item.some_key}}
</div>

some_key variable has only 3 type of string output: "unsafe", "elevated" and "trace". I want to order output of items in this order: first all items which have unsafe, then "elevated" and at the end "trace" ... I can't do this in a simple orderBy filter ... How to achieve this?

Thanks for a help in advance!

1
  • create a custom filter ... see filter docs ... or map the data in controller Commented Jan 20, 2015 at 17:57

2 Answers 2

2

Make a custom orderBy

<div ng-repeat="item in items | orderBy: statusEvent">
    {{item.some_key}}
</div>

$scope.statusEvent = function(item) {
    if (item.some_key == "unsafe") return 0
    else if (item.some_key == "elevated") return 1
    else if (item.some_key == "trace") return 2
    else return 3
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This clarifies the issue a lot!
1

Use a custom filter

var orders = {
  unsafe:0,
  elevated:1,
  trace:2
}
$scope.someKeyOrder = function(item){
  return orders[item.some_key];
}

and use it

ng-repeat="item in items | orderBy: someKeyOrder"

Demo at http://plnkr.co/edit/4Tjsc8?p=preview

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.