【发布时间】:2015-07-29 14:00:56
【问题描述】:
TLDR:我想要实现的目标:如果存在 office.vote(如果存在),则按 office.vote.candidateId 过滤。否则,按 office.candiateProfiles.vScore 排序
.offices(ng-repeat='office in service.offices' ng-class="{'last':$last}")
.row.row-format.text-uppercase
.office.ballot(ng-click='showCandidates = !showCandidates')
.col-xs-7.col-sm-4.col-md-4.mainBallot-col
.office-name
{{office.name}}
.col-xs-5.col-sm-8.col-md-8.border-left.mainBallot-col
.ballot(ng-repeat="candidate in office.candidateProfiles | orderBy: '-vScore' | filter: office.vote.candidateId | limitTo: 1 | " )
.row.noPad
.col-xs-1.col-sm-1.col-md-1.straight-col.ballotCheck-col
.col-xs-7.col-sm-7.col-md-7.straight-col.highest-col
%table.highestShow
%tr
%td
.vs
{{candidate.fullName}}
我意识到我尝试将上面的 orderby 和 filter 结合起来是行不通的。我对自定义过滤器不是很熟悉。如何使用自定义过滤器实现我想要做的事情?
我认为我的问题在于 candiateProfiles 是一个数组..
这是范围内的对象:
尝试 2 失败:
.ballot(ng-repeat="candidate in office.candidateProfiles | filter:criteriaMatch(office) | limitTo:1" )
控制器:
$scope.criteriaMatch = function(office) {
console.log(office);
if (office.vote) {
return office.vote.candidateId;
} else {
return orderBy(office.candidateProfiles.vScore)
}
};
在上面的控制器中,这适用于按 office.vote 过滤,但我需要一种新方法让“else”按最高 vScore 过滤。我不认为它在识别 candiateProfiles,因为它是一个数组。
【问题讨论】:
-
我不太清楚这个问题。您能否尝试更具体地说明您的逻辑,甚至更好:为此创建一个小 plnkr? Angular 1.4 的模板plnkr.co/edit/tpl:8rFfZljYNl3z1A4LKSL2
-
我试图弄清楚我想要实现的目标。
-
@limgb,是不是更清楚了?
-
是的。但是:目前尚不清楚为什么要过滤一种情况而只过滤另一种情况。是否有触发过滤的输入?
-
有一个复选框可以创建 office.vote。在您单击候选人旁边的框之前,它不存在。在创建之前,我想根据分数显示最高结果。创建后,选定的候选人 (office.vote.candidateId) 会用分数替换默认过滤器。
标签: angularjs angularjs-ng-repeat angularjs-filter