0

I'm still relatively new to Grails. I'm developing an online survey. I keep running into issues with webflow that I can't seem to find solutions for on the web. I have a class Survey that has many Topics that has many questions. In my web flow the amount of topics and questions are dynamic depending on which survey you choose. My question is how can you iterate (in the web flow) through each of the topics and questions (sort of like an arrray or list) where I can grab specific ones and set them to the flow variable. for example I'll give my Survey and Topic class:

class Survey implements Serializable {

List topic
String surveyName
float version
static hasMany = [topic:Topic]

static constraints = {

    surveyName (blank:false)
}
}

and Topic:

class Topic implements Serializable {

List primaryQuestion
String topicName
static belongsTo = [survey:Survey]
static hasMany = [primaryQuestion:PrimaryQuestion]


static constraints = {

    topicName (blank:false)
}
}

So I set my flow variable

    onStart {
        flow.survey = Survey.get(params.id)
    }

So how can I get each individual topic and question within the controller? My goal is to have one view represent each question with a "next" and previous". With my understanding of web flows I know this can be possible. I just can't figure out how to pass a single question to the view instead of iterating through all of them at the same time.

moultonjr
  • 85
  • 4

2 Answers2

0

If I understand your question correctly then all you need to do is iterate over your lists. First, a tip; you don't need to push your model into the flow scope in most cases. You can simply do the following:

onStart {
  [survey : Survey.get(params.id)]
}

And then in your view:

<g:each in="${survey.topic}" var="topic">
  <g:each in="${topic.primaryQuestion}" var="question">
     ...
  </g:each>
</g:each>

Another hint, rename topic to topics in your Survey domain and primaryQuestion to primaryQuestions in Topic. Makes more sense that way.

If I'm way off here you might need to edit your question and make it a bit clearer.

Gregg
  • 34,973
  • 19
  • 109
  • 214
  • I understand how to iterate through it using the tag. However, what if I want to display only one question per view? Or more accurately, have a web flow that has a "next" and "previous" button that navigates through the questions one at a time. I'm wondering how to pick out individual questions in the controller and not the GSP. If I do it that way, I'll have all the topics and questions in one view. I don't want that. Also, you're right about the names, I should probably change them. – moultonjr Jul 28 '12 at 22:18
0

I finally figured it out going through the GORM documentation. This is what I was looking for:

flow.topics = flow.survey ? Topic.findAllBySurvey(flow.survey) : []

This put all topics within that survey into a list, and I can grab each one by using:

onEntry{
    flow.topic = flow.topics.get(i)
}

Thanks again for your response.

moultonjr
  • 85
  • 4