Grails:在控制器和视图中使用列表
我想显示投票列表,如果单击某个投票,它将显示该投票的候选人列表。Grails:在控制器和视图中使用列表,grails,groovy,criteria,gsp,jointable,Grails,Groovy,Criteria,Gsp,Jointable,我想显示投票列表,如果单击某个投票,它将显示该投票的候选人列表。 我开始以下尝试失败,我仍被阻止: class Candidate { String username; static HasMany=[applications:Application] } class Vote { String name; Date firstdate; Date enddate ; static HAsMany=[applications:Application
我开始以下尝试失败,我仍被阻止:
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]
}
我想在申请表中看到候选人的投票。我在视图中的显示方式。
你能给我一个想法吗?映射看起来比它应该的复杂<代码>投票与
候选人
没有直接关系,您希望在每次投票中列出候选人
投票
与候选人
相关的唯一方式是通过应用程序
。因此,您必须抓取应用程序
进行投票
,并显示应用程序
的所有候选人
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;
}
//懒洋洋地:-
def votecandidate=[:]
Vote.list()?.each{Vote->
Vote候选资格
def votes=application.vote
def候选者=application.candidate
//从此迷失
//你有很多选票和候选人
//但两者属于同一应用程序
}
投票实际上意味着什么?您能否详细说明每个域类的使用情况,主要是投票
和应用程序
?为什么不使用复数来表示hasMany
?一个应用程序同时具有多个候选对象和一个候选对象?通过删除这些分号来消除java口音!+为什么不使用复数来表示hasMany
?尝试回答中提到的惰性路径。请更改类域。是不是还有别的建议
//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
}