Expanded tree macros

I’m looking for a macro that inserts an expanded tree view of the current node and below into the document. I want to see the structure of the node and below without having to expand individual nodes first. I’ve been experimenting with the different tree macros but couldnt find anything that fits. Am I missing something obvious?

1 Like

That’s the document tree macro: http://extensions.xwiki.org/xwiki/bin/view/Extension/Document%20Tree%20Macro

Ah you want to automatically open all children… That’s more complex indeed. I don’t think the macro has this feature by default. It would need a parameter to mention how many levels to open.

Maybe @mflorea knows some options to do this or how to write a custom filter or something to enable that.

The Tree macro (http://extensions.xwiki.org/xwiki/bin/view/Extension/Tree%20Macro) could work since it’s static and shows everything in it. But we’d need a way to provide the right format for it to list all children.

It shouldn’t be too hard with some velocity, if you want, say, to display the next N levels.

Maybe something like this (not working):

{{velocity}}
{{tree}}
#foreach ($child in $doc.children)
  * [[$child]]
  #foreach ($subchild in $child.children)
    ** [[$subchild]]
  #end
#end
{{/tree}}

{{velocity}}

Thanks! That does nicely render the first level under the $doc, but nothing further down. Where would I find documentation on the available variables? http://platform.xwiki.org/xwiki/bin/view/SRD/Navigation?xpage=embed Does not seem to show the available properties of $doc or it’s children.

You can install http://extensions.xwiki.org/xwiki/bin/view/Extension/Scripting%20Documentation%20Application in your wiki to have the list of all apis you can access.

Yes there’s a bug in my script above but didn’t get the time to look at it. Two issues that I don’t understand:

  • it shows only 1 level
  • it doesn’t show the node as a clickable link
  • I need an extra NL at the end that I don’t understand

@mflorea any idea?

Replying to myself: $child is a stirng not a Document… so you need to $xwiki.getDocument() on it :wink:

Something like:

{{velocity}}
{{tree}}
#foreach ($child in $doc.children)
  * [[$child]]
  #set ($childDoc = $xwiki.getDocument($child))
  #foreach ($subchild in $childDoc.children)
    ** [[$subchild]]
  #end
#end
{{/tree}}

{{velocity}}
{{velocity}}
{{tree}}
#foreach ($child in $doc.getChildren())
  * [[$child]]
  #foreach ($subchild in $xwiki.getDocument($child).getChildren())
    ** [[$subchild]]
    #foreach ($subsubchild in $xwiki.getDocument($subchild).getChildren())
      *** [[$subsubchild]]
    #end
  #end
#end
{{/tree}}
{{/velocity}}

Seems to work - but closes the nodes instead of leaving them open. Can’t seem to find anything on the Tree macro page to make it expand by default

In the Static Tree section you have

See the jsTree HTML data documentation for more information.

and if you follow the link you’ll find out that

Similarly you can set the jstree-open class on any <li> element to make it initially extended, so that its children are visible.

and there’s an example

<li class="jstree-open" id="node_1">Root...

Unfortunately the XWiki 2.1 syntax doesn’t support list item parameters so this means you can’t set the jstree-open class using wiki syntax. You’ll have to use the HTML macro.

Thanks for the reply - that gave me a working solution.

{{tree links="true"}}
{{velocity}}
{{html wiki=true}}
<ul>
#foreach ($child in $doc.getChildren())
  <li class="jstree-open">
    [[$child]]
    <ul>
    #foreach ($subchild in $xwiki.getDocument($child).getChildren())
      <li class="jstree-open">
        [[$subchild]]
        <ul>
        #foreach ($subsubchild in $xwiki.getDocument($subchild).getChildren())
          <li class="jstree-open">
            [[$subsubchild]]
            <ul>
            #foreach ($subsubsubchild in $xwiki.getDocument($subsubchild).getChildren())
              <li class="jstree-open">
                [[$subsubsubchild]]
              </li>
            #end
            </ul>
          </li>
        #end
        </ul>
      </li>
    #end
    </ul>
  </li>
#end
</ul>
{{/html}}
{{/velocity}}
{{/tree}}

Way harder than it should have been, but works. Thanks for the help, both of you! :smiley:

4 Likes

I completely agree. We need something better. Would you mind creating a jira issue to add a new parameter to automatically open a certain level of children nodes? Thanks!

Sure thing. Should that go under XWiki, XRendering or some other project?

Here: http://jira.xwiki.org/browse/XWIKI/component/14040

Thx!

Sorry for bumping up this old thread, but was this feature enhancement ever implemented? If so, how would I go about adding such an expanded tree view to a particular page ?

I don’t recall but it doesn’t ring a bell. Did you search for the jira issue? @Fake51 never mentioned if he created such issue or not in the end.