欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

grails中添加自定义标签解决gsp页面双层循环问题

程序员文章站 2022-05-23 16:21:17
...
Nope, not with the each clause.

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>

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
}

}

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