You are viewing revision #1 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.
The CMenu class provides some useful features for generating menus in your web application.
HTML friendly labels. ¶
You may want to add some HTML to your menu labels to display icons or images. You can do this by setting the encodeLabel
property:
'encodeLabel' => false,
Styling your Sub-menus. ¶
If your sub-menu is a drop down, then you may want to style it differently from the parent menu. You can do this using the submenuOptions
property:
'submenuHtmlOptions' => array(
'class' => 'dropdown-menu',
)
Link Options. ¶
If you're creating a drop down menu and your top item doesn't need a link, you can customize the behavior using the linkOptions
property:
'linkOptions'=> array(
'class' => 'dropdown-toggle',
'data-toggle' => 'dropdown',
),
Putting it All Together. ¶
I always find it helpful to see all these snippets together in action. Here is a full implementation of CMenu widget:
<?php $this->widget('zii.widgets.CMenu', array(
'items' => array(
array(
'label' => '<i class="icon-user"></i><span class="username">Admin</span> <i class="icon-angle-down"></i>',
'url' => '#',
'linkOptions'=> array(
'class' => 'dropdown-toggle',
'data-toggle' => 'dropdown',
),
'itemOptions' => array('class'=>'dropdown user'),
'items' => array(
array(
'label' => '<i class="icon-user"></i> My Profile',
'url' => '#'
),
array(
'label' => '<i class="icon-calendar"></i> My Calendar',
'url' => '#',
),
array(
'label' => '<i class="icon-tasks"></i> My Tasks</a>',
'url' => '#',
),
array(
'label' => '',
array(
'class' => 'divider',
)
),
array(
'label' => '<i class="icon-key"></i> Log Out',
'url' => array('site/logout'),
),
)
),
),
'encodeLabel' => false,
'htmlOptions' => array(
'class'=>'nav pull-right',
),
'submenuHtmlOptions' => array(
'class' => 'dropdown-menu',
)
));?>
Let me know if I missed anything.
--bhavik . . .
Doesn't Drop Down, Already expanded
I cut and pasted your code into layouts/main.php
The submenu is there but it is already expanded. How can I make the submenu drop down when I hover over the top menu?
Also, can you make it drop vertically instead of horizontally?
thanks!
Yes you have missed something which is important. The problem I am facing when I try to make the menu multilevel. I can add 'id' to the first 'items' array. But I want to add 'id' to the arrays inside the items. But I am unable to do it. I don't know if its possible but is there any way around add id to submenu ul? If I add 'id' in 'submenuHtmlOptions' then all of the submenu ul have the same id.
<?php $this->widget('zii.widgets.CMenu', array( 'items' => array( array( 'label' => '<i class="icon-green"></i> Services <span class="icon-chevron-down"></span>', 'url' => '#', 'linkOptions'=> array( 'class' => '',// <a> has no class so <a class=''> ), 'itemOptions' => array('class'=>'collapsed', 'data-toggle' => 'collapse','data-target'=>"#service", 'aria-expanded'=>"false"),//<li class='collapsed'> 'encodeLabel' => false, 'items' => array( array( 'label' => '<i class="icon-user"></i> My Profile', 'url' => '#' ), array( 'label' => '<i class="icon-calendar"></i> My Calendar', 'url' => '#', ), array( 'label' => '<i class="icon-tasks"></i> My Tasks</a>', 'url' => '#', ), ), 'id' => 'services',<!--This ID does not work--> ), ), 'id'=>"menu-content", <!--This ID works--> 'encodeLabel' => false, 'htmlOptions' => array( 'class'=>'menu-content collapse out', ), 'submenuHtmlOptions' => array( 'class' => 'sub-menu collapse',//<ul class="sub-menu collapse"> 'id'=>'service' ) ));?>
Here is the output of this code
<ul class="menu-content collapse out" id="menu-content"><!--The id="menu-content" came here--> <li class="collapsed" data-toggle="collapse" data-target="#service" aria-expanded="false"><a class="" href="#"><i class="icon-green"></i> Services <span class="icon-chevron-down"></span></a> <ul class="sub-menu collapse"> <!-- id="service" did not come here --> <li><a href="#"><i class="icon-user"></i> My Profile</a></li> <li><a href="#"><i class="icon-calendar"></i> My Calendar</a></li> <li><a href="#"><i class="icon-tasks"></i> My Tasks</a></li> </ul> </li> </ul>
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.