Yii Framework Forum: Add class active to menu item - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Add class active to menu item Rate Topic: -----

#1 User is offline   dony 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 28-May 12
  • Location:Ukraine. Kiev

Posted 20 August 2012 - 04:57 AM

Hello.
How to add a class active menu which has the form and cycle output from the database
<ul class="menu">
<li><a href="#">One Item</a></li>
<li><a href="#">Two Item</a></li>
<li><a href="#">Three Item</a></li>
</ul>

Thank you
0

#2 User is offline   Paresh Tokekar Kabira 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 09-June 11
  • Location:India

Posted 20 August 2012 - 05:27 AM

Suppose your controller is 'mycontroller' and action is 'myform' (i.e. for url like /index.php?r=mycontroller/myform), try following code:

<ul class="menu">
<?php if(Yii::app()->controller->id == 'mycontroller'  && Yii::app()->controller->action->id == 'myform'):  ?>
      	<li class='active'><a href="#">One Item</a></li>
 <?php else: ?>
      	<li><a href="#">One Item</a></li>
 <?php endif; ?>
<?php if(Yii::app()->controller->id == 'mycontroller'  && Yii::app()->controller->action->id == 'myform'):  ?>
 		<li class='active'><a href="#">One Item</a></li>
 <?php else: ?>
      	<li><a href="#">Two Item</a></li>
 <?php endif; ?>
...
...
</ul>

1

#3 User is offline   dony 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 28-May 12
  • Location:Ukraine. Kiev

Posted 20 August 2012 - 05:50 AM

1. when controller - site and action - index, menu always highlighted
if(Yii::app()->controller->id == 'site'  && Yii::app()->controller->action->id == 'index')

2. Each controller will have to register separately? and if they are 20 pieces or more?

isItemActive does not help in my case?
0

#4 User is offline   alirz23 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 504
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 20 August 2012 - 06:06 AM

Hi you can make use of the CMenu widget it gives you that functionality out of the box


http://www.yiiframew.../api/1.1/CMenu/
0

#5 User is offline   Paresh Tokekar Kabira 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 09-June 11
  • Location:India

Posted 20 August 2012 - 06:10 AM

View Postdony, on 20 August 2012 - 05:50 AM, said:

1. when controller - site and action - index, menu always highlighted
if(Yii::app()->controller->id == 'site'  && Yii::app()->controller->action->id == 'index')

2. Each controller will have to register separately? and if they are 20 pieces or more?

isItemActive does not help in my case?


Please check wiki, may be helpful for you.
One another solution is extending CMenu and creating custom menu... But how could you create menu, Please provide some code...
0

#6 User is offline   dony 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 28-May 12
  • Location:Ukraine. Kiev

Posted 20 August 2012 - 06:29 AM

Here is my menu. What advise do in this case? About CMenu of course I know, but it's not my variant
function menu($value) {
    echo '<ul>';
    foreach ($menus as $menu) {
        echo '<li><a href=' . $menu[link] . '>' . $menu[text] . '</a></li>';
    }
    echo '</ul>';
}

0

#7 User is offline   Paresh Tokekar Kabira 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 09-June 11
  • Location:India

Posted 20 August 2012 - 06:39 AM

View Postdony, on 20 August 2012 - 06:29 AM, said:

Here is my menu. What advise do in this case? About CMenu of course I know, but it's not my variant
function menu($value) {
    echo '<ul>';
    foreach ($menus as $menu) {
        echo '<li><a href=' . $menu[link] . '>' . $menu[text] . '</a></li>';
    }
    echo '</ul>';
}



Please provide your '$menu[link]' data also...
0

#8 User is offline   dony 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 28-May 12
  • Location:Ukraine. Kiev

Posted 20 August 2012 - 06:45 AM

View PostParesh Tokekar Kabira, on 20 August 2012 - 06:39 AM, said:

Please provide your '$menu[link]' data also...

links as Yii
http://site.com/index.php?r=site/contact 

or normal
http://www.google.com/

0

#9 User is offline   Paresh Tokekar Kabira 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 09-June 11
  • Location:India

Posted 20 August 2012 - 07:08 AM

View Postdony, on 20 August 2012 - 06:45 AM, said:

links as Yii
http://site.com/index.php?r=site/contact 

or normal
http://www.google.com/



For links as YII use following code:

/*
 * for http://site.com/index.php?r=site/contact 
 * Yii::app()->controller->route gives us "site/contact"
 */
function menu($value) {
	echo '<ul>';
	foreach ($menus as $menu) {
    	if(strpos($menu[link], Yii::app()->controller->route) === false){
        	echo '<li><a href=' . $menu[link] . '>' . $menu[text] . '</a></li>';        	
    	}
    	else{
        	echo '<li class="active"><a href=' . $menu[link] . '>' . $menu[text] . '</a></li>';        	
    	}
	}
	echo '</ul>';
}


For external links (like www.goolge.com) we can't do anything, because there are nothing to showing for this menu item.

But I thinks that using CMenu is best way of creating menus in YII.

Hope it will help you...
1

#10 User is offline   dony 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 28-May 12
  • Location:Ukraine. Kiev

Posted 20 August 2012 - 07:29 AM

View PostParesh Tokekar Kabira, on 20 August 2012 - 07:08 AM, said:

Hope it will help you...

Thanks, it's work!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users