Fred A
Fred A

Reputation: 1764

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?

Upvotes: 0

Views: 524

Answers (2)

Fred A
Fred A

Reputation: 1764

Ok seems i managed to iterate object in GSP

<g:each var='obj' in='${test}'>
    <g:each var='subObj' in='${obj.value}'>
        <p>${subObj}</p>
    </g:each>
</g:each>

But kind of not elegant enough.

I can do it too with this, as what Donal put in his answer

<g:each var='obj' in='${test.value}'>
    <p>${obj}</p>
</g:each>

But it would be hard i think if i want to iterate each item inside test object separately and uniquely.

Anyone please feel free to fix my approach.

Upvotes: 0

D&#243;nal
D&#243;nal

Reputation: 187499

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>

Upvotes: 2

Related Questions