Forums / Developer / Tree View

Tree View

Author Message

Håkan Bergman

Thursday 14 October 2010 7:09:24 am

Hello,

I have been trying to display a tree view.
This is a sample that I have for now:

Object A
-> Child A
-> Grandchild A
-> Child B
-> Grandchild B
Object B
-> Child C
-> Grandchild C
-> Grandchild D
The tree view is listing them in this order:
* Object A
* Object B
* Childobjects
* Grandchildobjects
I need it to iterate Object A + child(s) then Object B + child(s) and so on.
I tried to search the forum and someone else had the same issue but never got a perfect answer on how to solve it practically. Should I use another method or can I add something to the fetch method that will list the objects the way I want?

At the moment the code looks like this:
{def $tree = fetch ( 'content', 'tree', hash( 'parent_node_id', $id, 'class_filter_type', 'include', 'class_filter_array', array ('class') ) ) }

Best regards,

Håkan

Thiago Campos Viana

Thursday 14 October 2010 10:06:37 am

The problem is that a tree is not a tree but a list, see the comments on tree fetch function documentation

You need to specify a depth of 1 and parse the children of each node:

{def $tree_node = fetch('content', 'tree', hash( 'parent_node_id', $pagedata.root_node, 'depth', 1 ) ) }
<ul>
{foreach $tree_node as $key => $item}
        <li{cond($key|eq(0), ' class="firstli"')}><a href={$item.url_alias|ezurl(no)}>{$item.name}</a>
            <ul>
    {foreach $item.children as $subkey => $subitem}
            <li{cond($subkey|eq(0), ' class="firstli"')}><a href={$subitem.url_alias|ezurl(no)}>{$subitem.name}</a></li>
    {/foreach}
            </ul>
        </li>
{/foreach}
</ul>

eZ Publish Certified Developer: http://auth.ez.no/certification/verify/376924

Twitter: http://twitter.com/tcv_br

André R.

Friday 15 October 2010 12:42:32 am

http://doc.ez.no/eZ-Publish/Technical-manual/4.x/Reference/Template-operators/Miscellaneous/content_structure_tree/

It's seriously difficult to work with, and undocumented, but it does fetch content in a tree and maintain structure.

See use in webin ( from "contentstructure" and bellow ):
ezwebin/design/ezwebin/templates/menu/flat_left.tpl

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom

Håkan Bergman

Friday 15 October 2010 12:43:15 am

Thanks.

I took your example and implemented it in my template and it works to depth 2.

What I wanted to avoid was to create manual loops. Right now the code manually loops through depth 2 and prints it.

But if I want to show depth 3 inside of depth 2 I need to do another foreach, and for depth 3 inside of depth 2 I need to add another one.

This probably works if you know you have same depths to show in every case.

But in this case the depth can be dynamic and infinite, so it have to automcatically loop 1 or 50 depths.

Is there a way to do that without adding a new foreach and instead letting the template code automatically print all the child nodes in the right order?

Best regards,

Håkan

Håkan Bergman

Friday 15 October 2010 12:46:12 am

Thanks André.

Will take a look at that.

Im trying to learn so undocumented functions aint the best for me at the moment, but I will give it a shot and try it out.