Grails:在Controller和View中使用列表 [英] Grails: use list in Controller and View
问题描述
class Candidate {
String username;
static HasMany=[applications:Application]
}
class Vote {
String name;
Date firstdate;
Date enddate ;
static HAsMany=[applications:Application]
}
class Application {
Date datedemand;
Candidate candidate;
Vote vote;
static belongsTo = [candidate:Candidate,vote:Vote]
}
我想显示投票清单,如果我点击了投票,它会显示此投票的候选人名单。
我开始了下面的尝试,但仍然被屏蔽:
I want to display the list of votes and if I click on a vote, it displays the list of candidates for this vote.
I started the following attempt, and I remain blocked :
def candidatsGrpByVte(){
def results = Application.withCriteria {
createAlias("vote", "vote")
projections {
groupProperty("vote.name")
}
}
}
def candidatesByVName(def vname){
def results= Application.createCriteria().list() {
createAlias("candidate","candidatAlias")
createAlias("vote","voteAlias")
eq('voteAlias.name',vname)
projections {
property('candidatAlias.username')
}
}
return results;
}
我想从应用程序中看到候选人。 b $ b如何在视图中显示。
您能给我一个想法吗?
I want to see the candidates in a vote from Application.
how I displayed in a view.
can you give me an idea ?
推荐答案
映射看起来比它应该是复杂的。 投票
与候选人
没有直接关系,您希望列出每个投票的候选人。
The mappings look complex than it should be. Vote
is not directly related to Candidate
and you want to list candidates per vote.
唯一的方法是 Vote
与候选
有关应用程序
。因此,您必须为 Vote
获取应用程序
,并显示所有候选人$对于
应用程序。
The only way Vote
is related to Candidate
is through Application
. So you have to grab the Application
for a Vote
and show all the Candidate
s for the Application
.
//Lazily:-
def voteCandidature = [:]
Vote.list()?.each{vote->
voteCandidature << [(vote.name) : vote.application?.candidate?.asList()]
}
//Eagerly:-
def candidatesByVote(){
def results = Application.createCriteria().list{
vote{
}
candidate{
}
}
}
results.each{application->
def votes = application.vote
def candidates = application.candidate
//Lost after this
//You have bunch of votes and bunch of candidates
//but both belong to the same application
}
> Vote
其实意味着什么?你能否详细说明每个域的类的用途,主要是 Vote
和 Application
?为什么你不使用复数作为 hasMany
?
What does Vote
signify actually? Can you elaborate on each of the domain classes use, mainly Vote
and Application
? And why don't you use plurals for hasMany
?
这篇关于Grails:在Controller和View中使用列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!