Reputation: 953
I'm trying to display a list of movies, each which will have a title, series name, and a series number.
The way I want these movies to be ordered is that movies with the same series are grouped together, and ordered inside that group by their series number. The entire list is sorted by title (unless the movie has a series name, in which case it is sorted by series name obviously). For example if I had the following movies:
Up ( no series )
500 Days of Summer ( no series)
Alien ( series: 'Alien', seriesnumber: 1 )
Aliens ( series: 'Aliens', seriesnumber: 2 )
Alien Resurrection ( series: 'Alien Resurrection, 'seriesnumber: 4 )
Alien 3 ( series: 'Alien 3, 'seriesnumber: 3 )
They would be ordered as:
500 Days of Summer
Alien
Aliens
Alien 3
Alien Resurrection
Up
I have already achieved this ordering in my SQL query, so I can initially display these items in their intended order, as long as I don't apply any filters to the ng-repeat. (For those wondering, the SQL query ORDER BY is:
ORDER BY CASE WHEN seriesName is NOT NULL THEN seriesName ELSE title END
Is there any way I can get this sort of ordering by using ng-repeat's order by? The only reason I want to have it in the ng-repeat is so that the user can add titles, and when they are added they are correctly ordered - so if there is an alternative way of getting that solution that would be appreciated.
Upvotes: 2
Views: 1712
Reputation: 20073
orderBy
also takes an array which can save you some lines of JavaScript:
<div ng-repeat="data in datum | orderBy: ['name', 'ip']">
See here: http://jsfiddle.net/xjcw6/
Upvotes: 0
Reputation: 12661
Yes, you can introduce
$scope.decoratedName = function(title, seriesnumber) {
return title + '-' + seriesnumber;
}
and then use ng-repeat ... orderBy:decoratedName(title, seriesnumber)
.
Upvotes: 2