我已经使用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/