Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list childrens recursively in a GSP page?

i have a Media Domain

def Media {
   String name

   static belongsTo = [parent:Media]
   static hasMany = [children:Media]
}

In the show.gsp page i want to list all my root Media (that haven't parent) into an ul list an their children and their children recursively in another ul lists. I have used the Tag for the first list but i don't know how to do this recursively for the children.

So have you an idea of how to do this ?

Thanks.

like image 801
SlimIT Avatar asked Dec 20 '25 09:12

SlimIT


1 Answers

You could put the recursive part into a GSP-template and then call it recursively, e.g.:

index.gsp: Assuming rootMedias is passed into the view

<g:each in="${rootMedias}" var="media">
    <g:render template="step" model="${[media: media]}" />
</g:each>

_step.gsp

<ul>
    <g:each in="${media.children}" var="child">
    <li>
        ${child.name}
        <g:render template="step" model="${[media: child]}" />
    </li>
    </g:each>
</ul>
like image 117
Chris Avatar answered Dec 24 '25 02:12

Chris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!