如何在cakephp 3中操纵树菜单的子项? [英] How to manipulate the child items of the tree menu in cakephp 3?

查看:69
本文介绍了如何在cakephp 3中操纵树菜单的子项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对操作子菜单项以改善显示样式有疑问。

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

我目前根据文档生成:

function index() 
   {
     $list = $this->Categorias->find('threaded')->toArray(); 
     $this->set('list', $list);
   }    







$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);

但是显示错误。显示格式如下:

But the display is in error.. The display is in this format:

我感谢任何评论!

推荐答案

如树行为文档所述,如果要使用线程化结果,则不仅需要在顶级项目上进行迭代,还需要某种递归功能(

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.

还请注意,使用 children 查找程序如您的示例所示,将仅检索具有指定主键的节点的子级,即,不会检索其父级节点,即使表包含多个根节点,也不会检索它们。因此,根据您的数据,您可能只需要使用线程化的查找程序。

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>

另请参见

这篇关于如何在cakephp 3中操纵树菜单的子项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆