Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

angularjs - Angular Sorting with null objects

I need to take records with null values on top , when I'm sorting by ASC

<tr ng-repeat="footballer in footballers=(footballers | orderBy:predicate)">
predicate : ['team.name','id]

Some footballers have no team, so team object == null and team.name==null, and I need to have them on the top

I wanted to rewrite sort function, but I need to save predicate

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use something like this in your controller:

$scope.nullsToTop = function(obj) {
  return (angular.isDefined(obj.team) ? 0 : -1);
};

And on the HTML:

<tr ng-repeat="footballer in footballers | orderBy:[nullsToTop].concat(predicate)">

This way you can maintain your predicate separately. Just concat the nullsToTop function in the orderBy expression to run it first.

Plunker


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...