Grails:在Controller和View中使用列表 [英] Grails: use list in Controller and View

查看:98
本文介绍了Grails:在Controller和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 Candidates 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆