Question

I have a requirement for a grails application to display a list of questions on the screen with 6 grade options listed below each of the questions. The information for these questions and grades is coming from a lookup table in the database. I have the questions and grades displaying on the screen but I'm not sure how to go about getting the lookup information to save in the database. I would also like to know if there is a way to have a certain grade selected by default for each of the questions. I tried the checked="S" but this only selects the S grade for the very bottom questions.

My code for the view is

  <label for="questions"></label>
    <ul class="one-to-many">
    <!-- Evaluation Questions -->
    <g:each in="${cdeEvaluationInstance?.questions}" var="evalQuestion" status="i">
    <g:hiddenField name="cdeEvaluation.questions[${i}].id" value="${evalQuestion.id}"/>
    <legend>
    ${evalQuestion.areaOfEval.title}
    </legend>
    <p>
    <strong>Focus areas: ${evalQuestion.areaOfEval.focusArea}</strong>
    </p>
    <p>
    <em> ${evalQuestion.areaOfEval.description}
    </em>
    </p>
    <p>
    <g:each in="${evalQuestion.areaOfEval.grades.sort{it.grade}}"
    var="grade" ><div class="radio">
    <span class="clear long"> 
    <input type="radio"
    name="radioGroup" value="${evalQuestion.grade}" checked="S"  /> 
    <label class="long"><strong> ${grade.grade}
    </strong> ${grade.description}</label>
    </div>
    </g:each>

My code for the controller is

def evalQuestions = EvaluationService.fetchActiveEvaluationQuestions();
            //def evaluation = new CdeEvaluation(questions: evalQuestions)

           def evaluation = new CdeEvaluation(params)
           evaluation.setQuestions(evalQuestions)

My domain for the table that the questions and answers are

package gov.mt.mdt.cde.domain.evaluation

import java.util.Date;

class CdeEvalQuestion extends Base{

    CdeAreaOfEvaluation areaOfEval
    CdeAreaOfEvalCriteria grade

    String comments

    static belongsTo = [cdeEvaluation: CdeEvaluation]




    static mapping = {
        id column: 'cevqu_id_seq'
        id generator: 'sequence', params: [sequence: 'cevqu_id_seq']

        areaOfEval column: 'caoe_id_seq'
        grade column: 'caoec_id_seq'
    }

    static constraints = {
        comments(blank:true, nullable:true, maxSize:2000)

        createdBy(blank: false, nullable:false, maxSize:13)
        dateCreated(blank: false, nullable:false)
        lastUpdatedBy(blank: false, nullable:true, maxSize:13)
        lastUpdated(blank: false, nullable:true)
    }
}

I am just starting to learn grails/groovy so any help or examples you could point me to would be great. Thanks!

Was it helpful?

Solution

So selecting a particular question by default you would do something like:

<g:radioGroup name="myGroup" labels="evalQuestion.areaOfEval.grades" values="evalQuestion.areaOfEval.grades*.grade" value="evalQuestion.grade">
    ${it.radio} <label class="long"><strong>${it.label.grade}</strong> ${it.label.description}</label>
</g:radioGroup>

That doesn't require you write the inner each. Basically you pass an array of labels and a parallel array of values. The value attribute is the default value from the values attribute. The inner body of the radioGroup will be repeated for each label and value pair. The *. (aka spread operator) basically is the same thing as using the collect() method.

I removed the spread operator for label and I passed the full object in for the label. Then inside the body of the tag when I do it.label I have the full object and can use different fields it.label.description and it.label.grade.

As for setting the default to grade S. You'll need to write the code to find grade S from evalQuestion.areaOfEval.grades. Something like:

evalQuestion.areaOfEval.grades.find { it.grade == 'S' }

And pass that to value attribute of the tag. You could do this:

<g:set var="defaultGrade" value="evalQuestion.areaOfEval.grades.find { it.grade == 'S' }"/>

<g:radioGroup name="myGroup" 
              labels="evalQuestion.areaOfEval.grades" 
              values="evalQuestion.areaOfEval.grades*.grade" 
              value="defaultGrade">
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top