Grails/GSP:突破<g:each>

cal*_*e16 2 each grails break gsp

有没有办法打破<g:each>?我有一个页面,其中我正在遍历列表,我必须确保选中一个复选框,如果这是存储在DB中的值.

为了使它更清晰,请考虑以下事项:

<g:each in=${list1}>
    <g:each in=${list2}>
        <g:if test="${list1.id == list2.id}">
            <input type="checkbox" ... checked="checked" />
        </if>
    </g:each>
    ...
</g:each>
Run Code Online (Sandbox Code Playgroud)

其中list1是,例如Domain1.list()(即所有可能的值),list2是Domain2.find(...)(即SELECTED值)

在g:each中,我需要显示所有list1(因此,在每个内部之后的"......")带有一个复选框,但是我需要确保list2中的那些(用户选择的项目保存到DB中) )应相应检查(如果声明).

现在,如果在第一次迭代中更改了检查状态,我需要离开内部每个...任何方式来做到这一点?

谢谢!

Ted*_*eid 5

不,不是每个条款.

我只是编写自己的taglib,它接受list1和list2并为你做迭代,然后回到

<g:eachCheckedItem list1="${list1}" list2="${list2}">
    <input type="checkbox" ... checked="checked"/>
</g:eachCheckedItem>
Run Code Online (Sandbox Code Playgroud)

在你的taglib类中:

def eachCheckedItem = { attrs, body ->
    def list1 = attrs.list1
    def list2 = attrs.list2

    list1.findAll { list2.contains(it) }.each {
        out << body(listItem: it)  // access to listItem variable inside gsp
    }

}
Run Code Online (Sandbox Code Playgroud)

类似的东西(根据您的具体问题调整)很容易编写,并且还可以清理您的gsp文件.我在taglib中一直使用这些自定义迭代器.