yii2 Menu::widget 最全用法 [ 新手入门 ]
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2016/12/14
* Time: 16:59
*/
namespace backend\controllers;
use yii\base\Controller;
use yii\widgets\Menu;
class WidgetController extends Controller
{
public function actionIndex()
{
return '';
}
///////////////////////////////////////////////////////////////////
public function actionMenuTa()
{
echo Menu::widget([
'items' => [
['label' => 'AAA', 'url' => 'http://www.baidu.com'],
['label' => 'BBB', 'url' => ['site/index', 'tag'=>'xx']],
['label' => 'CCC', 'url' => ['widget/menu-ta']],
['label' => 'DDD', 'url' => ['widget/menu-tb']],
['label' => 'EEE'],
]
]);
}
///////////////////////////////////////////////////////////////////
/*
* [[activateItems]] bool , 为全局参数,默认是 true
* [[active]] bool , 为局部参数,默认为 false
* [[activeCssClass]] string , 默认为 'active'
* */
public function actionMenuTb()
{
echo Menu::widget([
'activateItems' => false, //默认是 true
'activeCssClass'=>'act',
'items' => [
['label' => 'AAA', 'url' => 'http://www.baidu.com'],
['label' => 'BBB', 'url' => ['site/index', 'tag'=>'xx']],
['label' => 'CCC', 'url' => ['widget/menu-ta']],
[
'label' => 'DDD',
'url' => ['widget/menu-tb'],
'active' => true, // 默认是 false
],
['label' => 'EEE'],
]
]);
}
///////////////////////////////////////////////////////////////////
/**
* [[firstItemCssClass]], [[lastItemCssClass]] items 的前和尾 css class 名称
* 这里需要注意,子 items 也会加入
*/
public function actionMenuTc()
{
echo Menu::widget([
'firstItemCssClass'=>'fistitemclass',
'lastItemCssClass' =>'lastitemclass',
'items' => [
['label' => 'AAA', 'url' => 'http://www.baidu.com'],
['label' => 'BBB', 'url' => ['site/index', 'tag'=>'xx']],
['label' => 'CCC', 'url' => ['widget/menu-ta']],
['label' => 'DDD', 'url' => ['widget/menu-tb']],
[
'label' => 'EEE',
'visible' => false,
],
]
]);
}
///////////////////////////////////////////////////////////////////
public function actionMenuTd()
{
echo Menu::widget([
'options' => [
'class' => 'navbar-nav nav',
'id'=>'navbar-id',
'style'=>'font-size: 14px;',
'data-tag'=>'yii2-menu',
],
'items' => [
['label' => 'AAA', 'url' => 'http://www.baidu.com'],
['label' => 'BBB', 'url' => ['site/index', 'tag'=>'xx']],
['label' => 'CCC', 'url' => ['widget/menu-ta']],
['label' => 'DDD', 'url' => ['widget/menu-tb']],
['label' => 'EEE'],
]
]);
}
///////////////////////////////////////////////////////////////////
public function actionMenuTe()
{
echo Menu::widget([
//public $linkTemplate = '<a href="{url}">{label}</a>';
'linkTemplate' => '<a href="{url}"><span>{label}</span></a>',
'labelTemplate' =>'<i>{label}</i>',
'items' => [
['label' => 'AAA', 'url' => 'http://www.baidu.com'],
['label' => 'BBB', 'url' => ['site/index', 'tag'=>'xx']],
['label' => 'CCC', 'url' => ['widget/menu-ta']],
[
'label' => 'DDD',
'url' => ['widget/menu-tb'],
'template' => '<a href="{url}" >{label}<i class="fa fa-angle-left pull-right"></i></a>',
],
['label' => 'EEE'],
]
]);
}
///////////////////////////////////////////////////////////////////
public function actionMenuTf()
{
echo Menu::widget([
'encodeLabels' => false, // 全局参数 默认为 true
'items' => [
[
'label' => '<i>AAA</i>',
'encode' => true, // 全局参数 默认为 false
'url' => 'http://www.baidu.com'
],
['label' => 'BBB', 'url' => ['site/index', 'tag'=>'xx']],
['label' => 'CCC', 'url' => ['widget/menu-ta']],
['label' => 'DDD', 'url' => ['widget/menu-tb']],
['label' => 'EEE'],
]
]);
}
///////////////////////////////////////////////////////////////////
public function actionMenuTg()
{
echo Menu::widget([
'activateParents' => true, //默认为 false
'items' => [
['label' => 'AAA', 'url' => 'http://www.baidu.com'],
['label' => 'BBB', 'url' => ['site/index', 'tag'=>'xx']],
['label' => 'CCC', 'url' => ['widget/menu-ta']],
[
'label' => 'DDD',
'url' => ['widget/menu-tb'],
'items' => [
[
'label' => 'ddd1',
'url' => '#',
'items' => [
['label' => 'ss'],
],
],
['label' => 'ddd2', 'url' => ['widget/menu-tg']],
['label' => 'ddd3', 'url' => '#'],
]
],
['label' => 'EEE'],
]
]);
}
///////////////////////////////////////////////////////////////////
public function actionMenuTh()
{
echo Menu::widget([
'itemOptions'=> [
'class'=>'item_class',
'style'=>'font-weight:bold'
],
'items' => [
['label' => 'AAA', 'url' => 'http://www.baidu.com'],
['label' => 'BBB', 'url' => ['site/index', 'tag'=>'xx']],
['label' => 'CCC', 'url' => ['widget/menu-ta']],
[
'label' => 'DDD',
'url' => ['widget/menu-tb'],
'items' => [
['label' => 'ddd1', 'url' => '#'],
['label' => 'ddd2', 'url' => ['widget/menu-tg']],
['label' => 'ddd3', 'url' => '#'],
]
],
['label' => 'EEE'],
],
]);
}
///////////////////////////////////////////////////////////////////
public function actionMenuTi()
{
echo Menu::widget([
'items' => [
['label' => 'AAA', 'url' => 'http://www.baidu.com'],
['label' => 'BBB', 'url' => ['site/index', 'tag'=>'xx']],
['label' => 'CCC', 'url' => ['widget/menu-ta']],
[
'label' => 'DDD',
'url' => ['widget/menu-tb'],
'options' => [
'class' => 'sxx',
'id' =>'navbar-id2',
'style'=>'font-size: 12px;',
'data-tag'=>'yii2-menu',
],
'items' => [
['label' => 'ddd1', 'url' => '#'],
['label' => 'ddd2', 'url' => '#'],
['label' => 'ddd3', 'url' => '#'],
]
],
['label' => 'EEE'],
],
]);
}
///////////////////////////////////////////////////////////////////
public function actionMenuTj()
{
echo Menu::widget([
'submenuTemplate' => "\n<ul class='treeview-menu' role='menu'>\n{items}\n</ul>\n",
'items' => [
['label' => 'AAA', 'url' => 'http://www.baidu.com'],
['label' => 'BBB', 'url' => ['site/index', 'tag'=>'xx']],
['label' => 'CCC', 'url' => ['widget/menu-ta']],
[
'label' => 'DDD',
'url' => ['widget/menu-tb'],
'options' => [
'class' => 'sxx',
'id' => 'navbar-id2',
'style' => 'font-size: 12px;',
'data-tag' => 'yii2-menu',
],
'items' => [
['label' => 'ddd1', 'url' => '#'],
['label' => 'ddd2', 'url' => '#'],
['label' => 'ddd3', 'url' => '#'],
]
],
['label' => 'EEE'],
],
]);
}
///////////////////////////////////////////////////////////////////
public function actionMenuTk()
{
echo Menu::widget(
[
'activateItems' => true,
'route' => 'widget/menu-tk',
'params' => [
'tag' => 'ss'
],
'items' =>[
[
'label' => 'Action',
'url' => ['widget/menu-tk', 'tag' => 'ss'],
],
['label' => 'Another action','url' => '/index.php'],
['label' => 'Something else here', 'url' => '#'],
],
]
);
}
}
共 1 条回复
qiuxis
注册时间:2016-10-31
最后登录:2017-10-25
在线时长:22小时6分
最后登录:2017-10-25
在线时长:22小时6分
- 粉丝21
- 金钱215
- 威望40
- 积分835