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

JavaCokyMan 2013-11-24

Nope,notwiththeeachclause.

I'djustwritemyowntaglibthattakeslist1andlist2anddoestheiterationforyou,yieldingbacktothe

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

Andinyourtaglibclass:

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
    }

}

Somethinglikethat(tunedtoyourspecificproblem)iseasytowrite,andalsocleansupyourgspfilequiteabit.Iusethesekindsofcustomiteratorsallthetimeinmytaglibs.

相关推荐