Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply individual classes for zii.widgets.CMenu labels?

I'm newbie to yii and I want to know:

How to apply individual classes(CSS) for zii.widgets.CMenu labels?

My code:

$this->widget('zii.widgets.CMenu',array(
                          'items'=>array(
                                 array('label'=>'Home', 
'url'=>array('/site/index')),<br>
                                 array('label'=>'About', 
'url'=>array('/site/page', 'view'=>'about')),<br>
                                 array('label'=>'Contact', 
'url'=>array('/site/contact')),<br>
                                 array('label'=>'Supplier', 
'url'=>array('/supplier/index')),<br>
                                 array('label'=>'Login', 
'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest),
                                 array('label'=>'Logout 
('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), <br>
'visible'=>!Yii::app()->user->isGuest)
                         ),<br>
                 ));
like image 990
rdanusha Avatar asked Mar 19 '23 02:03

rdanusha


2 Answers

You can supply itemOptions according to the official documentation that can be found here.

itemOptions, according to the documentation, are "Additional HTML attributes to be rendered for the container tag of the menu item."

This means they can be used to add HTML attributes to your items, as well as li elements like so:

$this->widget('zii.widgets.CMenu', array
(
    'items' => array
    (
        array
        (
            'itemOptions' => array('class' => 'class names here'),
            'label' => 'Home', 
            'url' => array('/site/index')),
        ),
    ),
));

Alternatively you can use linkOptions to add classes to the a href elements themselfs like so (note the change from itemOptions to linkOptions)

$this->widget('zii.widgets.CMenu', array
(
    'items' => array
    (
        array
        (
            'linkOptions' => array('class' => 'class names here'),
            'label' => 'Home', 
            'url' => array('/site/index')),
        ),
    ),
));
like image 127
Dennis Jamin Avatar answered Mar 24 '23 18:03

Dennis Jamin


For li use itemOptions and for a href use linkOptions

For a href

array('label'=>'Link Lable',
      'url'=>array('controller/action'),
      'linkOptions'=>array("id"=>"link-id","class"=>"your-class1 your-class2 your-class3"), 
      'visible'=>!Yii::app()->user->isGuest)

For li

'itemOptions' => array('class' => 'your-class1 your-class2'),
like image 22
Arslan Butt Avatar answered Mar 24 '23 20:03

Arslan Butt