使用" SideNav Widget"动态创建菜单的正确方法是什么?和2个型号:
类别(id,description)
subcat (id,category_id,description)
修改
我有什么:
$item = [];
$Category = Category::find()->all();
foreach($Category as $model) {
$item[] = ['label' => $model->description, 'url' => '#'];
$Subcat = Subcat::find()->where("category_id=$model->id")->all();
foreach($Subcat as $model2) {
$item[] = ['label' => $model2->description, 'url' => '#'];
}
}
echo SideNav::widget([
'items' => $item,
我想要的:
答案 0 :(得分:2)
您正在寻找这样的事情吗?
为sideName示例提供一个简单的
$menuGiiItems[] = ['label' => Yii::t('app','Gii Generators'), 'icon' => 'th-list', 'url'=>Url::to(['/gii'])],
['label' => Yii::t('app','Generator 2'), 'url'=>Url::to(['/auth/item'])],
['label' => Yii::t('app','Generator 3'), 'url'=>Url::to(['auth/item-child'])],
['label' => Yii::t('app','Generator 4'), 'url'=>Url::to(['/auth/rule'])];
$type = SideNav::TYPE_PRIMARY;
$heading = '<i class="glyphicon glyphicon-education"></i> ' . Yii::t('app',' Gii - Development Automation ');
echo SideNav::widget([
'type' => $type,
'encodeLabels' => false,
'heading' => $heading,
'items' =>$menuGiiItems,
]);
或更复杂的嵌套项目
$menuItems[] = ['label' => 'DFenX - Yii2 User - '. Yii::t('app','Authentication manager'), 'icon' => 'user', 'items' => [
['label' => '/user/admin/index', 'icon' => 'list', 'url'=>Url::to(['/user/admin/index'])],
['label' => '/user/1 (view)', 'icon' =>'eye-open', 'url'=>Url::to(['/user/1'])],
['label' => 'Settings', 'icon' => 'cog', 'items' => [
['label' => '/user/settings', 'icon' => 'cog', 'url'=>Url::to(['/user/settings'])],
['label' => '/user/settings/profile', 'url'=>Url::to(['/user/settings/profile'])],
['label' => '/user/settings/account', 'url'=>Url::to(['user/settings/account'])],
['label' => '/user/settings/networks', 'url'=>Url::to(['/user/settings/networks'])],
]],
['label' => 'Registration', 'icon' => 'user', 'items' => [
['label' => '/user/registration/register', 'icon' => 'user', 'url'=>Url::to(['/user/registration/register'])],
['label' => '/user/registration/resend', 'icon' => 'user', 'url'=>Url::to(['/user/registration/resend'])],
]],
['label' => 'Security', 'icon' => 'user', 'items' => [
['label' => '/user/security/login', 'icon' => 'user', 'url'=>Url::to(['/user/security/login'])],
['label' => '/user/security/logout', 'icon' => 'user', 'url'=>Url::to(['/user/security/logout'])],
]],
['label' => 'Recovery', 'icon' => 'user', 'items' => [
['label' => '/user/recovery/request', 'icon' => 'user', 'url'=>Url::to(['/user/recovery/request'])],
['label' => '/user/recovery/reset', 'icon' => 'user', 'url'=>Url::to(['/user/recovery/reset'])],
]],
]];
$type = SideNav::TYPE_PRIMARY;
$heading = '<i class="glyphicon glyphicon-tasks"></i> ' . Yii::t('app','AUTHENTICATION');
//$heading = ['label' => 'Books', 'icon' => 'book'] ;
echo SideNav::widget([
'type' => $type,
'encodeLabels' => false,
'heading' => $heading,
'items' =>$menuItems,
]);
答案 1 :(得分:0)
好吧,最后我决定使用扩展程序“yii2-menu”。并满足了我的需要。 感谢
答案 2 :(得分:0)
$item = [];
$Category = Category::find()->all();
$count=0;
foreach($Category as $model) {
$item[] = ['label' => $model->description, 'url' => '#'];
$Subcat = Subcat::find()->where("category_id=$model->id")->all();
$cnt=0;
foreach($Subcat as $model2) {
$item[$count]['items'][$cnt] = ['label' => $model2->description, 'url' => '#'];
$cnt++;
}
$count++;
}
echo SideNav::widget([
'items' => $item,
)];