# Generate a simple tree-menu in Grails

<span style="font-weight: bold;"></span>It's seems one of the many posts you can find in internet that helps you to generate a tree-menu using javascript/java/... What I want to illustrate is a method that use recursion in back-end (java code that generate the Tree structure) and front-end (a gsp that shown the tree calling itself!!).<br /><br />In fact, the start point, is that our page is not a "simple" page but is written as grails templates.<br />Template is a grails way to get your front-end code structured and provide an highly re-usable mechanism that you can call simply using a defined taglib.<br /><br /><pre><code>class TreeMenu {<br /><br />    def addNode = {nodeElement, machine, tagList -&gt;<br />        def nodes = [:]<br />        nodes[machine.hostName] =  machine<br />        def newList = tagList - nodeElement<br />        newList?.each {currentTag -&gt;<br />            nodes[currentTag.name] = addNode(currentTag, machine, newList)<br />        }<br /><br />        nodes<br />    }<br />}<br /></code></pre><br />Is an extraction of my program codes... in the original version the data structure is not a simple Map but I've a complex object, so I can do, for example, a check if there is a node with current provided name and so on.<br />What this code try to do, is to add a Machine to each Tree-Tag I'm sending to function as a List.<br />"nodeName" is the current node where I want my machine<br />"machine" is the object name I want in my tree<br />"tagList" is the list of all tree-node where my machine will be put<br /><br />For example, I could have this situation<br /><br />machine: "TryMachine"<br />tagList: ["A", "B"]<br /><br /><pre><code>TreeMenu.addNode(&quot;Root&quot;, machine, tagList)<br /></code></pre><br />The result of this method invokation will be:<br /><pre><code> Root <br /> &#124;-&gt; A<br /> &#124;   &#124;-&gt;B<br /> &#124;   &#124;  &#124;TryMachine<br /> &#124;   &#124;TryMachine<br /> &#124;-&gt; B<br /> &#124;   &#124;TryMachine<br /> &#124;   &#124;-&gt; A<br /> &#124;   &#124;   &#124;TryMachine<br /> &#124; TryMachine<br /></code></pre><br /><span style="font-weight: bold;">Display the tree</span><br />The extraordinary feature offers by grails is, as I said, the usage of recursion on the front-end, that make you able to create a page without insertion of some java codes: all just with default grails taglibs.<br />Here an example gets from my code:<br /><pre><code>&lt;g:each in=&quot;${nodes}&quot; var=&quot;element&quot;&gt;<br />    &lt;g:if test=&quot;${element.value instanceof Machine}&quot;&gt;<br />            ${element.name}<br />    &lt;/g:if&gt;<br />    &lt;g:else&gt;<br />    &lt;g:machineList template=&quot;/templates/machineTree&quot; data=&quot;${element}&quot;/&gt;<br />    &lt;/g:else&gt;<br />&lt;/g:each&gt;<br /></code></pre><br />And in your page, where you want to put your tree, you can just simply call the template:<br /><pre><code>&lt;g:machineList template=&quot;/templates/machineTree&quot; data=&quot;${treeData}&quot;/&gt;<br /></code></pre><br />Is a just a simple example (and, in fact, I'm not sure that with mods I've done to create this post, all work well :P), If you want you can make some improvements to this code, attaching, for example, javascript functions to get your tree-node opened and closed, or some other kinds of object type.u
