Recursion in GSP page

不羁的心 提交于 2019-12-01 20:57:31

So, you want to print all the nodes that have the known parent?

What about something like:

<g:each in="${Node.findAllByParent(parent_node_instance)}" var="node">
  ${node}<br>
</g:each>

But since you are talking about recursion, I think you also want to print all descendants.

Create a _node.gsp template:

<g:each in="${nodes}" var="node">
  ${node}<br>
  <g:render template="node" model="[nodes:Node.findAllByParent(node)]" />
</g:each>

And call with:

<g:render template="node" model="[nodes:Node.findAllByParent(parent_node_instance)]" />

You can easily add a depth variable to the model to indent each generation properly, or use the <ul> and <li> elements in the template.

parent_node_instance is the root you want to start printing your tree from, it can be the absolute root or any node in the tree.

findAllByParent() is a dynamic finder function. See http://grails.org/doc/latest/guide/GORM.html#finders for details.

you can also use the @collection/@bean:

_node.gsp

<div class="fancy nested">
  id:${it.nodeId} - label:${it.label}
  <g:render template="node" collection="${Node.findAllByParent(it.node)}" />
</div>

main.gsp

<g:render template="node" bean="${rootNode}" />
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!