0

Having

<div class="item" data-ng-repeat="course in courses | filter:filterCourses | orderBy:predicate">...

and

$scope.predicate = function(course) {
    //$scope.orderProp
};

what code do I need to put inside the predicate so I can

  1. order the same way the default orderBy:predicate will order(not sure if this makes sense)
  2. place empty course.StartDate at the end of the list

Notes:

  • course is an Object with different properties like title, startDate, code
  • $scope.orderProp contains the value to sort by: title, startDate, code. When startDate is empty I would like to place those items at the end of the list but still keep sorting by date properly, start date is an "integer" that I later display with ng-date in nicer form

Thank you

Answer: Here's what I ended up with http://jsfiddle.net/3hz2j8j7/

3
  • did you try reading the orderBy docs? Commented Sep 10, 2014 at 21:19
  • saw those but I'm not sure they relate to my problem Commented Sep 10, 2014 at 21:38
  • What is the "default orderBy:predicate" ??? Commented Sep 10, 2014 at 22:25

2 Answers 2

2

OrderBy can accept a list of predicates (a bit like saying "order by A, then B"). So your first predicate will ensure that blank start dates go at the end, and your second one will use the value of orderProp:

<div ... orderBy:[nonEmptyStartDate, orderProp]">

$scope.nonEmptyStartDate = function(course) {
    return course.startDate=="" ? 1 : 0;
}
Sign up to request clarification or add additional context in comments.

Comments

1

in addition to Constantinos answer, I did a jsfiddle for you with your exact data. All the Angular magic is in

<div ng-repeat="course in courses|orderBy:[nonEmptyStartDate,orderProp]:true">

Where orderProp='title' so that first data are ordered by non empty date first and then by title. Please let us know .

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.