Grails / GSP:突破< g:each> [英] Grails/GSP: break out of <g:each>

查看:252
本文介绍了Grails / GSP:突破< g:each>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法打破< g:each>?我有一个页面在其中遍历一个列表,并且我必须确保选中一个复选框是否该值是存储在数据库中的值。

Is there a way to break out of a <g:each>? I have a page wherein I'm iterating through a list and I have to make sure that a checkbox is checked if that was the value stored in DB.

要使其清楚一点,请考虑以下内容:

To make it a little clearer, please consider something like:

<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>

其中list1是Domain1.list()(即所有可能的值),而list2是Domain2。 find(...)(即选定的值)

where list1 is, say Domain1.list() (i.e. ALL possible values) and list2 is Domain2.find(...) (i.e. SELECTED values)

在g:each中,我需要显示list1的所有内容(因此,内部)带有一个复选框,但是我需要确保应该相应地检查list2(保存到数据库的用户选择项)中的内容(如果声明)。

In the g:each, I need to display ALL of list1 (hence, the "..." after the inner each) with a checkbox but I need to make sure that those in list2 (user-selected items that were saved to DB) should be checked accordingly (if statement).

现在,如果检查的状态在第一次迭代中发生了变化,我就需要摆脱内在的...任何方式吗?

Now, if the checked status was changed on the first iteration, i need to get out of the inner each... any way to do this?

谢谢!

推荐答案

不是,不包含each子句。

Nope, not with the each clause.

我只需编写自己的taglib,该标签库将使用list1和list2并为您进行迭代,从而返回到

I'd just write my own taglib that takes list1 and list2 and does the iteration for you, yielding back to the

<g:eachCheckedItem list1="${list1}" list2="${list2}">
    <input type="checkbox" ... checked="checked"/>
</g:eachCheckedItem>

在您的taglib类中:

And in your taglib class:

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
    }

}

类似这样的东西(根据您的特定问题而定)很容易编写,并且还可以清理gsp文件很多。我一直在自己的taglibs中使用这些自定义迭代器。

Something like that (tuned to your specific problem) is easy to write, and also cleans up your gsp file quite a bit. I use these kinds of custom iterators all the time in my taglibs.

这篇关于Grails / GSP:突破&lt; g:each&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆