Recursion on a GSP page

I have a domain

class Node {   

    String nodeId
    String label

    Node parent    
}

      

In the GSP page, I want to start at the root and print all of its children (note that I have a link to the parent, not the child). Any ideas how to do this? I'm sorry, but I am a newbie and very confused. I want to print everything that comes from root (not root) and root has no parent (its null). So I wrote

<g:each in="${nodes}" var="node">
<g:if test="${node.parent!=null}">

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

      

In the above code, not sure what parent_node_intance should be. The list of nodes starts with root. I don't want to print this, but start with everything else that has a root as a parent.

node.gsp

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

      

Getting the following error , which I'm pretty sure is caused by the root

2014-10-02 12:28:21,693 [http-bio-8080-exec-1] ERROR errors.GrailsExceptionResolver  - NullPointerException occurred when processing request: [GET] /taxonomy - parameters:
outputFormat: concept
hierarchyId: lp
Cannot invoke method findAllByParent() on null object. Stacktrace follows:
Message: Error evaluating expression [[nodes:Node.findAllByParent(node)]] on line [11]: Cannot invoke method findAllByParent() on null object
    Line | Method
->>   11 | run       in C:/Users/U6021072/Documents/workspace-ggts-3.6.0.RELEASE/ae-and-sdx-analysis-ui/target/work/plugins/taxonomy-toolkit-for-grails-0.02-SNAPSHOT/grails-app/views/taxonomy/concept.gsp
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Caused by NullPointerException: Cannot invoke method findAllByParent() on null object
->>   11 | doCall    in C__Users_U6021072_Documents_workspace_ggts_3_6_0_RELEASE_ae_and_sdx_analysis_ui_target_work_plugins_taxonomy_toolkit_for_grails_0_02_SNAPSHOT_grails_app_views_taxonomy_concept_gsp$_run_closure4
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    198 | doFilter  in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter  in grails.plugin.cache.web.filter.AbstractFilter
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run       in java.lang.Thread

      

+3


source to share


2 answers


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

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 template _node.gsp

:

<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 variable depth model for correct input of each generation, or use the elements <ul>

and <li>

in the template.

parent_node_instance

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

findAllByParent()

- dynamic search function. See http://grails.org/doc/latest/guide/GORM.html#finders for details .

+4


source


you can also use @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}" />

      

+2


source







All Articles