11
4

3 に答える 3

22

You have to set encodeLabel attribute of CMenu to false

<?php
$this->widget('zii.widgets.CMenu', array(
    'encodeLabel' => false,
    'htmlOptions' => array('class' => 'nav'),
    'items' => array(
        array(
            'label' => 'Home',
            'url' => array('/site/index'),
        ),
        array(
            'label' => 'Dropdown <b class="caret"></b>',
            'url' => '#',
            'submenuOptions' => array('class' => 'dropdown-menu'),
            'items' => array(
                array(
                    'label' => 'Submenu Item 1',
                    'url' => array('/user/create'),
                ),
                array(
                    'label' => 'Submenu Item 1',
                    'url' => array('/user/list'),
                ),
            ),
            'itemOptions' => array('class' => 'dropdown'),
            'linkOptions' => array('class' => 'dropdown-toggle', 'data-toggle' => 'dropdown'),
        ),
    ),
));
?>
于 2013-03-04T16:17:36.233 に答える
1

My solution is creating CMenu extension:

layout/main.php

'submenuOptions'=>array('class'=>'dropdown-menu'),
'itemOptions'=>array('class'=>'dropdown'),
'linkOptions'=>array('class'=>'dropdown-toggle', 'data-toggle'=>'dropdown'),
// Dropdown arrow toggle
'dropdownArrow'=>true,

ext/widgets/BootstrapCMenu.php

class BootstrapCMenu extends CMenu {

protected function renderMenuItem($item)
{
    if(isset($item['url']))
    {
        $item['label'] .= ($item['dropdownArrow']) ? ' <b class="caret"></b>' : '';
        $label=$this->linkLabelWrapper===null ? $item['label'] : CHtml::tag($this->linkLabelWrapper, $this->linkLabelWrapperHtmlOptions, $item['label']);
        return CHtml::link($label,$item['url'],isset($item['linkOptions']) ? $item['linkOptions'] : array());
    }
    else
        return CHtml::tag('span',isset($item['linkOptions']) ? $item['linkOptions'] : array(), $item['label']);
}

}
于 2013-08-30T14:34:40.670 に答える
0

In Yii 2.0, for adding glyphicon in navbar menu you can follow the below info.

Edit in vendor\yiisoft\yii2-bootstrap\Nav.php under renderItem function the following code:

     if(isset($item['icons']))
        $label=Html::tag('span', '', ['class' => 'glyphicon glyphicon-'.$item['icons']]).$label;

Now, you can directly use any icon from your code with icons option as

    <?php 
$this->widget( 'zii.widgets.CMenu', array(
'items' => array(
    array(
        'label' => 'Home', 
        'url' => array( '/site/index' ), 
        'icons'=> 'home',
    ),
    array( 
        'label' => 'Dropdown <b class="caret"></b>', 
        'url' => '#',
        'submenuOptions' => array( 'class' => 'dropdown-menu' ),
        'items' => array( 
            array( 
                'label' => 'Submenu Item 1', 
                'url' => array( '/user/create' ), 
            ),
            array( 
                'label' => 'Submenu Item 1', 
                'url' => array( '/user/list' ), 
            ),
        ),
        'itemOptions' => array( 'class' => 'dropdown' ),
        'linkOptions' => array( 'class' => 'dropdown-toggle', 'data-toggle' => 'dropdown' ),
    ),
    'htmlOptions' => array( 'class' => 'nav' ),
)); ?>

You can make the corresponding changes even in older versions of yii.

于 2015-04-06T19:57:03.070 に答える