Search code examples
grailsgroovygsp

Passing values to input through g:each iteration


Say if i have this data from my controller that i want to pass it to my GSP

def test = [value: ['TEST1', 'TEST2', 'TEST3']]

And in my GSP, i have the following

<g:set var="counter" value="${0}"/>
<g:set var="count" value="${count}"/>
<g:while test="${counter < count}">
    <g:set var="counter" value="${counter+1}"/>
    <g:each var='obj' in='${test}' status='i'>
        <g:textField id='justATest1' name='justATest' value='obj.value'>
        <g:textField id='justATest2' name='justATest' value='obj.value'>
        <g:textField id='justATest3' name='justATest' value='obj.value'>
    </g:each>
</g:while>

And i want it so that justATest1 will get TEST1, justATest2 will get TEST2 and justATest3 will get TEST3.

How can i achieve that?


Solution

  • Assuming your model contains the following

    def myAction() {
        [value: ['TEST1', 'TEST2', 'TEST3']]
    }
    

    This should do it

    <g:each status="i" in="${value}" var="item">
        <g:textField id="justATest${i}" name='justATest' value="${item}">
    </g:each>