简体   繁体   中英

Grails: GSP variable substitution in g:each tag

I have a loop inside a loop and want to substitute the value of first var into second. Below is the code snippet.

<g:each in="${tables}" status="i" var="table">
    <div class="tabletitle">
        ${table.name}
    </div>
    <table>
        <thead>
            <tr>
            <g:each in="${${table.name}DisplayColumns}" status="k" var="displayColumn">
                <td>${displayColumn}</td>
            </g:each>
            </tr>
        </thead>
        <tbody>
            ....
            ....
        </tbody>
    </table>
</g:each>

${table.name} substitution in second g:each tag is not working. Any idea to make it work?

尝试这个:

    <g:each in="${evaluate(table.name+'DisplayColumns')}" status="k" var="displayColumn">

Interesting, I've never used evaluate inside a gsp, as Kelly suggests. But may I suggest a less optimal approach?

You can store ${table.name} inside a variable with <g:set> ( http://grails.org/doc/2.0.x/ref/Tags/set.html )

Do you know that you can pass any object to a GSP? Even maps (you're trying to emulate maps, I don't know why), and use it like:

<g:each in="${displayColumns[table.name]}">

where displayColumns is a Map that contains columns for each table.

Btw, more clean way, is to use special object, that includes this data. Something like TableDetails that have List<String> getColumns() method. So you can use

<g:each in="${tables}" var="table">

   ${table.name}

   <g:each in="${table.columns}" var="column">
     ${column}
   </g:each>

</g:each>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM