symfony1 - symfony - 嵌套集和父/子节点的渲染

标签 symfony1 symfony-1.4

我已经使用Symfony1.4中的nestedSet行为学说创建了一个模型,因为我正在尝试创建一个具有分层页面的基本cms。

我有几个页面,带有父节点和子节点

Parent_1
   Child_1
   Child_2
Parent_2
   Child_3

我的问题是根据导航标题呈现这些项目。 (<ul><li>等)

最简单/最好的方法是什么?

我想要root节点具有诸如 /parent_1 之类的 URL及其后续子节点为 parent_1/child_1

谢谢

最佳答案

我编写了一个递归函数,它将从任何节点开始绘制树。指定根节点将绘制整个树。我的购物车插件中使用了它,您可以查看完成的 UI 的演示 here

我已经粘贴了下面的函数,但对我的实现进行了修改以使其更加清晰。

<?php
//Render a nested set.  Recurses for all descendants of that node. Can be used to draw entire tree, when specifying root id.
//Secondary parameter ($node) is used for performance boost, internally in function.
public static function display_node($id, $node = null) {

    $isRoot = false;

    if ($node == null) {
        $node = Doctrine_Core::getTable('YOURNESTEDTABLENAME')->findOneById($id)->getNode();
        if ($node->isRoot()) {
            $isRoot = true;
        }
    }
    $record = $node->getRecord();

    if (!$isRoot) {
        echo "<li class='tree_item' id='list_". $record->getId() . "'>"; 
        echo "<div class='listitem' id='listitem_".$record->getId()."'>";
        echo $record->getName();
        echo "<div style='clear:both'></div>";
        echo "</div>";

        if ($node->hasChildren()) {
            echo "<ol>";
            foreach ($node->getChildren() as $child) {
                self::display_node($child->getId(), $child->getNode());
            }
            echo "</ol>";
        }
    }
    else {
        if ($node->hasChildren()) {
            echo "<ol class='sortable'>";
            echo "<li class='tree_item root_item' style='position: relative;' id='list_". $record->getId() . "'>";
            foreach ($node->getChildren() as $child) {
                self::display_node($child->getId(), $child->getNode());
            }
            echo "</ol>";
        }
    }
}
?>

您还可以轻松修改代码以根据需要添加 URL。 希望这有帮助。如果您需要澄清,请告诉我。

关于symfony1 - symfony - 嵌套集和父/子节点的渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5975821/

相关文章:

php - 无法重新声明类 sfconfig

mysql - Doctrine 与 YAML 的一对二关系

symfony1 - "Integrity constraint violation: 1062 Duplicate entry"具有 Symfony Doctrine 版本

php - Symfony 自定义配置级联

php - 有没有更好的方法来对包含各种语言的字符串的数组进行排序

php - 在 Ubuntu 中使用 CLI 终端访问 symfony 1.4 URL

symfony1 - Symfony 2 还是 Symfony 1.4?

php - 如何使用 symfony 1.4 修改生产数据库的结构

php - 上传图像(JPEG)时有没有办法检查 DPI?

php - Symfony 1.4 sfThumbnail 不生成缩略图