Grails: use list in Controller and View

限于喜欢 提交于 2019-12-12 02:58:38

问题


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;
    }

I want to see the candidates in a vote from Application.
how I displayed in a view.
can you give me an idea ?


回答1:


The mappings look complex than it should be. Vote is not directly related to Candidate and you want to list candidates per 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
    }

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?



来源:https://stackoverflow.com/questions/16745620/grails-use-list-in-controller-and-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!