0
votes

I'm new to grails 1.3.7 and I try to access my database and to show my data on a gsp. Now Ive got the following problem: I've got a list of questions (listofQuestions) and a list of answers (listofAnswers). To each question belongs one Lpicanswer object which contains various answers (answera, answerb)

So when I create those lists, in the end I've got one list containing the questions and one list containing lpicanswer-objects. each lpicanswerobject has an lpicid (which is the id of the question), so that they are related to each other.

Here is the code to create those lists:

    List listofQuestions = []
    List listofAnswers = []

    def ques
    def question
    def ans
    // we create a questions list containing questions
    // we create a answers list containing answers-objects for a question
    for (int i = 0; i <= cacheService.questionList.size()-1; i++) {
        ques = Lpicquestions.get(cacheService.questionList[i]);
        question = ques.question;
        listofQuestions.add(question);
    }

    for (int i = 0; i <= cacheService.questionList.size(); i++) {
        ans = Lpicanswers.get(cacheService.questionList[i]);
        listofAnswers.add(ans);
    }

     return new ModelAndView("/result/resultdetail", [ qlist : listofQuestions, alist : listofAnswers ]);}

now I want to show them on my gsp. here is what I do:

<g:each in="${qlist}">

<b>${it}</b><br/>

${alist.answera}<br/>
${alist.answerb}<br/>
${alist.answerc}<br/>
${alist.answerd}<br/>
${alist.answere}<br/>
${alist.answerf}<br/>
${alist.answerg}<br/>
${alist.answerh}<br/>

</g:each>

what happens is, that the questions are given out correct, but the answers of course not. For each question all answersa, all answersb, etc are shown (like: [answera-from-question1, answera-from-question2] and so on) how can I solve this?

any help will be apreciated! :-)

[EDIT] Here is the structure of lpicquestions and lpicanswers, thanks for helping!! :-)

package com.lpic

class Lpicquestions {

    int lpicchapter 
    String question

    static constraints = {
        question(nullable:false, blank:false, maxSize:1000)
        lpicchapter(nullable:false, blank:false)
    }

}


package com.lpic

class Lpicanswers {

    Lpicquestions lpicid
    String answera
    String answerb
    String answerc
    String answerd
    String answere
    String answerf
    String answerg
    String answerh

    static constraints = {
    }
}
1

1 Answers

1
votes

aList is not an object or map. So you can't put something like: ${alist.answera}

change the view to.

<g:each var="question" in="${qlist}">
    <b>${question}</b><br/>
    <g:each var="answer" in="${aList}">
        <g:if test="${answer.lpicid?.question == question}">
            <b>${answer.answera}</b><br/>
            <b>${answer.answerb}</b><br/>
            <b>${answer.answerc}</b><br/>
            <b>${answer.answerd}</b><br/>
            <b>${answer.answere}</b><br/>
            <b>${answer.answerf}</b><br/>
            <b>${answer.answerg}</b><br/>
            <b>${answer.answerh}</b><br/>
        </g:if>
    </g:each>
</g:each>

if assuming that cacheService.questionList contains list of id for Lpicquestions change

for (int i = 0; i <= cacheService.questionList.size(); i++) {
    //ans = Lpicanswers.get(cacheService.questionList[i]);
    ans = Lpicanswers.findWhere(['lpicid' : Lpicquestions.get(cacheService.questionList[i])]);
    listofAnswers.add(ans);
}