使用空对象进行角度排序 [英] Angular Sorting with null objects
本文介绍了使用空对象进行角度排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我按 ASC 排序时,我需要在顶部取空值的记录
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]
有些足球运动员没有团队,所以团队object == null
和team.name==null
,我需要让他们在顶部
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
推荐答案
你可以在你的控制器中使用这样的东西:
You can use something like this in your controller:
$scope.nullsToTop = function(obj) {
return (angular.isDefined(obj.team) ? 0 : -1);
};
在 HTML 上:
<tr ng-repeat="footballer in footballers | orderBy:[nullsToTop].concat(predicate)">
这样你就可以单独维护你的谓词.只需在 orderBy 表达式中连接 nullsToTop
函数以先运行它.
This way you can maintain your predicate separately. Just concat the nullsToTop
function in the orderBy expression to run it first.
这篇关于使用空对象进行角度排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文