我假设您已经为 MenuTable 设置了模型菜单,并且已经定义了关系
public function relations()
{
return array(
'childMenuItems' => array(self::HAS_MANY, 'Menu', 'parent_id'),
);
}
获取顶级父菜单
// the parent menu would not have parent menu id (0, empty or IS NULL, my case was using comapare with 0 as default FK column value)
$topMenus = Menu::model()->findAllByAttributes(array('parent_id' => '0'));
渲染菜单
<ul data-role="listview" data-inset="true">
<?php
$render = "";
echo renderMenuItems(topMenus, &$render);
}?>
</ul>
处理递归 n 子菜单渲染的函数
<?php
function renderMenuItems($parentItem, &$render){
if(count($parentItem->childMenuItems) > 0){
$render .= '<li class="custom-li">
<div data-role="collapsible" class="custom-collapsible2" data-iconpos="right">
<h3><?php echo $parentItem->title ?></h3>
<ul data-role="listview">';
foreach($parentItem->childMenuItems as $item){
$render .= renderMenuItems($parentItem,$render);
}
$render .= '</ul>
</div>
</li>';
}
else{
$render .= '<li><a href="<?php echo $menu->actionUrl ?>"><?php echo $menu->title ?></a></li>';
}
return $render;
}
?>
我还没有调试它,但想法是一样的。不只是这个菜单,如果在子级菜单上有重复的布局结构,很多其他菜单的逻辑几乎是一样的