【问题标题】:How to manipulate the child items of the tree menu in cakephp 3?如何在 cakephp 3 中操作树形菜单的子项?
【发布时间】:2020-06-07 11:38:55
【问题描述】:

我对操纵子菜单项以改善显示风格存有疑问。

我目前正在根据文档生成:

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

但是显示有误。。显示是这样的格式:

感谢任何 cmets!

【问题讨论】:

    标签: php cakephp cakephp-3.0


    【解决方案1】:

    正如树行为文档中提到的,如果您想使用线程结果,那么您需要某种递归功能来迭代顶级项目(您的示例正在执行的操作)以及嵌套的子项项目。

    还请注意,在您的示例中使用children finder 只会检索具有指定主键的节点的子节点,即它不会检索它们的父节点,如果表包含多个根节点,他们也不会被检索到。因此,根据您的数据,您可能只需要使用threaded finder。

    话虽如此,项目的子项嵌套在 children 属性/键下,因此基本示例可能如下所示:

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

    这将创建一个像这样的嵌套列表(当然没有格式/缩进):

    <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>
    

    另见

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-24
      • 2012-05-28
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多