php - 关闭表中的多个垂直菜单

标签 php css menu smarty transitive-closure-table

我正在寻找一些多个垂直菜单,例如 here .我不想要任何下拉菜单。我在我的 mysql 数据库中使用典型的闭包表层次结构(祖先/后代/深度)作为类别,我想呈现它们。要从数据库中获取所有 parent 和 child ,我有以下方法:

   public function getSubtree($node) {
    $tree = $this->connection->query("
    SELECT c.*, cc2.ancestor, cc2.descendant, cc.depth
    FROM
        category c
        JOIN category_closure cc
        ON (c.cat_id = cc.descendant)
            JOIN category_closure cc2
            USING (descendant)
    WHERE cc.ancestor = $node AND cc2.depth = 1
    ORDER BY cc.depth, c.cat_id);

    return $this->parseSubTree($node, $tree);
}

private function parseSubTree($rootID, $nodes) {
    // to allow direct access by node ID
    $byID = array();

    // an array of parrents and their children
    $byParent = array();


    foreach ($nodes as $node) {
        if ($node["cat_id"] != $rootID) {
            if (!isset($byParent[$node["ancestor"]])) {
                $byParent[$node["ancestor"]] = array();
            }
            $byParent[$node["ancestor"]][] = $node["cat_id"];
        }
        $byID[$node["cat_id"]] = (array) $node;
    }

    // tree reconstruction
    $tree = array();
    foreach ($byParent[$rootID] as $nodeID) { // root direct children
        $tree[] = $this->parseChildren($nodeID, $byID, $byParent);
    }

    return $tree;
}

private function parseChildren($id, $nodes, $parents) {
    $tree = $nodes[$id];

    $tree["children"] = array();
    if (isset($parents[$id])) {
        foreach ($parents[$id] as $nodeID) {
            $tree["children"][] = $this->parseChildren($nodeID, $nodes, $parents);
        }
    }

    return $tree;
}

在演示者中我刚刚:

$this->template->categories = $this->category->getSubtree(1);

因为我使用的是 Nette 框架,所以我使用的是 Latte 模板引擎,这与 Smarty 非常相似。为了渲染所有带有 parent 和 child 的类别,我有这个:

    <ul class="tree">
    {block #categories}
        {foreach $categories as $node}
            <li>
                <span">{$node["name"]}</span>
                <ul n:if="count($node['children'])">
                    {include #categories, 'categories' => $node["children"]}
                </ul>
            </li>
        {/foreach}
    {/block}
</ul>

我最大的问题是,如果我想要三级或更多级菜单,如何制作 css 样式。如果选择某个类别,则会显示他的子类别,而另一个类别会隐藏。当被选中时,一些子类别显示他的子类别而另一个子类别隐藏等等。非常感谢您的提前,我很抱歉我的英语。希望你明白我的意思。

最佳答案

如果我理解得很好,您需要一个下拉菜单。我猜对齐方式是垂直的。子类别,它们应该呈现在类别下还是旁边?我知道其余部分应该保持可见。

关于php - 关闭表中的多个垂直菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17660850/

相关文章:

php - SQLSTATE[23000] : Integrity constraint violation: 1048 Column 'user_id' cannot be null error when assign forien key value?

jquery - 滚动 jQuery UI 模式覆盖不滚动

css - 在 MVC Bootstrap 中更改导航栏中按钮的悬停颜色

html - 悬停背景显示不正确?

html - 带有水平子菜单的纯 html css 菜单。悬停在 IE 中无法正常工作

android - 如何创建带有水平项目的菜单

javascript - 当详细信息传递到模态时,id 未定义

javascript - 如何有多个下拉菜单

PHP 循环显示错误文本

css - Bootstrap 图标在本地加载,但在线时不加载