Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: OrderBy Descending (null entries)

I'm using Angular JS and Google Places API to build an web app.

I have an ng-repeat which is currently being ordered by "rating" descending(orderBy:'-rating'). So it will list x places from the Google Places API JSON file in the descending order based on the rating.

HOWEVER, items/places that don't have a rating are classed as null and these entries are shown at the top of my ng-repeat feed. These should go to the bottom of the feed as they don't have a rating?

So an example of how i'd like this it to look is:

<ng-repeat orderBy:'-rating'>
  <place rating="5"/>
  <place rating="4.2"/>
  <place rating="2.8"/>
  <place rating="null"/>
  <place rating="null"/>
</ng-repeat>

What's the best way of going about this?

like image 721
Dayle Salmon Avatar asked Apr 22 '16 13:04

Dayle Salmon


People also ask

What is orderBy in AngularJS?

Definition and Usage The orderBy filter allows us to sort an array. By default, strings are sorted alphabetically, and numbers are sorted numerically.

How do I sort a list in AngularJS?

To sort in descending order, set it as true . You can also use + to sort the data in an ascending and – the data in descending order also . Here with the filters in Angular JS, instead of displaying the various rows, we will be sorting it by ascending and descending order .

What is track by $Index in AngularJS?

Track by $index in AngularJS The ngRepeat track by $index allows you to specify which items should be repeated using their index number. The ngRepeat is a directive that can be added to the HTML template of an Angular application. It is used for creating lists, and it can take an expression as an argument.

How can a developer restrict a user from modifying the sorting order of any column in angular?

Disabling sorting If we want to prevent the user from changing the order of any column, then we have to use the matSortDizable bindings when disabled on mat-sort or on the mat-sort-header.


1 Answers

To sort with null or undefined value using orderBy filter. use [] notation orderBy:[ '!rating', '-rating', ].

That will show null or undefined value at the bottom.

like:

 <li ng-repeat="option in options | orderBy:[ '!rating', '-rating', ]">{{option.name}}</li>
like image 115
Shaishab Roy Avatar answered Oct 07 '22 09:10

Shaishab Roy