How to manipulate the child items of the tree menu in cakephp 3?

后端 未结 1 1061
小蘑菇
小蘑菇 2021-01-26 18:00

I\'m having doubts about manipulating the child menu items to improve the display style.

I am currently generating according to documentation:

function i         


        
相关标签:
1条回答
  • 2021-01-26 18:58

    As mentioned in the tree behavior docs, if you want to use threaded results, then you need some kind of recursive functionality to iterate not only over the top level items (which your example is doing), but also over the nested child items.

    Also note that using the children finder as in your example, will only retrieve the children of the node with the specified primary key, ie it won't retrieve their parent node, also if the table contains multiple root nodes, they wouldn't be retrieved either. So depending on your data you might need to use only the threaded finder.

    That being said, the children of an item are nested under the children property/key, so a basic example could look like this:

    $renderItems = function($items) use (&$renderItems)
    {
        echo '<ul>';
        foreach ($items as $item) {
            echo '<li>';
            echo h($item->name);
    
            if ($item->children) {
                $renderItems($item->children); // < recursion
            }
    
            echo '</li>';
        }
        echo '</ul>';
    };
    
    $renderItems($list);
    

    That would create a nested list like this (without the formatting/indenting of course):

    <ul>
        <li>
            Fun
            <ul>
                <li>
                    Sport
                    <ul>
                        <li>Surfing</li>
                        <li>Skating</li>
                    </ul>
                </li>
            </ul>
        </li>
        <li>
            Trips
            <ul>
                <li>National</li>
                <li>International</li>
            </ul>
        </li>
    </ul>
    

    See also

    • Cookbook > Database Access & ORM > Behaviors > Tree
    0 讨论(0)
提交回复
热议问题