CMenu

优质
小牛编辑
137浏览
2023-12-01
所有包 | 属性 | 方法
zii.widgets
继承class CMenu » CWidget » CBaseController » CComponent
源自1.1
版本$Id: CMenu.php 3520 2011-12-29 09:54:22Z mdomba $
源码framework/zii/widgets/CMenu.php
CMenu使用嵌套的HTML列表显示多级菜单。

The main property of CMenu is items, which specifies the possible items in the menu. A menu item has three main properties: visible, active and items. The "visible" property specifies whether the menu item is currently visible. The "active" property specifies whether the menu item is currently selected. And the "items" property specifies the child menu items.

The following example shows how to use CMenu:
$this->widget('zii.widgets.CMenu', array(  'items'=>array(      // Important: you need to specify url as 'controller/action',      // not just as 'controller' even if default acion is used.      array('label'=>'Home', 'url'=>array('site/index')),      // 'Products' menu item will be selected no matter which tag parameter value is since it's not specified.      array('label'=>'Products', 'url'=>array('product/index'), 'items'=>array(          array('label'=>'New Arrivals', 'url'=>array('product/new', 'tag'=>'new')),          array('label'=>'Most Popular', 'url'=>array('product/index', 'tag'=>'popular')),      )),      array('label'=>'Login', 'url'=>array('site/login'), 'visible'=>Yii::app()->user->isGuest),  ),
));

公共属性

隐藏继承属性

属性类型描述定义在
actionPrefixstringactions的ID的前缀。 当微件在CController::actions中声明了 动作提供者,可以为其动作的ID指定前缀以区别 于别的微件或控制器。当微件用于控制器 的视图中时,必须配置同样的前缀。CWidget
activateItemsbooleanwhether to automatically activate items according to whether their route setting matches the currently requested route.CMenu
activateParentsbooleanwhether to activate parent menu items when one of the corresponding child menu items is active.CMenu
activeCssClassstringthe CSS class to be appended to the active menu item.CMenu
controllerCController返回此微件所属的控制器。CWidget
encodeLabelbooleanwhether the labels for menu items should be HTML-encoded.CMenu
firstItemCssClassstringthe CSS class that will be assigned to the first item in the main menu or each submenu.CMenu
hideEmptyItemsbooleanwhether to hide empty menu items.CMenu
htmlOptionsarrayHTML attributes for the menu's root container tagCMenu
idstring返回此微件的ID。如果需要的话,将生产一个新的ID并将其返回。CWidget
itemCssClassstringthe CSS class that will be assigned to every item.CMenu
itemTemplatestringthe template used to render an individual menu item.CMenu
itemsarraylist of menu items.CMenu
lastItemCssClassstringthe CSS class that will be assigned to the last item in the main menu or each submenu.CMenu
linkLabelWrapperstringthe HTML element name that will be used to wrap the label of all menu links.CMenu
ownerCBaseController返回此微件的所有者或创建者。CWidget
skinmixed微件使用的皮肤的名称。默认为“default”。 如果此属性设置为false,微件将不会有皮肤被使用。CWidget
submenuHtmlOptionsarrayHTML attributes for the submenu's container tag.CMenu
viewPathstring返回包含此微件所需的视图文件的路径。CWidget

公共方法

隐藏继承方法

方法描述定义在
__call()如果类中没有调的方法名,则调用这个方法。CComponent
__construct()构造器。CWidget
__get()返回一个属性值、一个事件处理程序列表或一个行为名称。CComponent
__isset()检查一个属性是否为null。CComponent
__set()设置一个组件的属性值。CComponent
__unset()设置一个组件的属性为null。CComponent
actions()返回此widget使用的动作的列表。CWidget
asa()返回这个名字的行为对象。CComponent
attachBehavior()附加一个行为到组件。CComponent
attachBehaviors()附加一个行为列表到组件。CComponent
attachEventHandler()为事件附加一个事件处理程序。CComponent
beginCache()Begins fragment caching.CBaseController
beginClip()Begins recording a clip.CBaseController
beginContent()Begins the rendering of content that is to be decorated by the specified view.CBaseController
beginWidget()Creates a widget and executes it.CBaseController
canGetProperty()确定属性是否可读。CComponent
canSetProperty()确定属性是否可写。CComponent
createWidget()Creates a widget and initializes it.CBaseController
detachBehavior()从组件中分离一个行为。CComponent
detachBehaviors()从组件中分离所有行为。CComponent
detachEventHandler()分离一个存在的事件处理程序。CComponent
disableBehavior()禁用一个附加行为。CComponent
disableBehaviors()禁用组件附加的所有行为。CComponent
enableBehavior()启用一个附加行为。CComponent
enableBehaviors()启用组件附加的所有行为。CComponent
endCache()Ends fragment caching.CBaseController
endClip()Ends recording a clip.CBaseController
endContent()Ends the rendering of content.CBaseController
endWidget()Ends the execution of the named widget.CBaseController
evaluateExpression()计算一个PHP表达式,或根据组件上下文执行回调。CComponent
getController()返回此微件所属的控制器。CWidget
getEventHandlers()返回一个事件的附加处理程序列表。CComponent
getId()返回此微件的ID。如果需要的话,将生产一个新的ID并将其返回。CWidget
getOwner()返回此微件的所有者或创建者。CWidget
getViewFile()根据视图名查找视图文件。CWidget
getViewPath()返回包含此微件所需的视图文件的路径。CWidget
hasEvent()确定一个事件是否定义。CComponent
hasEventHandler()检查事件是否有附加的处理程序。CComponent
hasProperty()确定属性是否被定义。CComponent
init()Initializes the menu widget.CMenu
raiseEvent()发起一个事件。CComponent
render()渲染一个视图。CWidget
renderFile()Renders a view file.CBaseController
renderInternal()Renders a view file.CBaseController
run()Calls renderMenu to render the menu.CMenu
setId()设置此微件的ID。CWidget
widget()Creates a widget and executes it.CBaseController

受保护方法

隐藏继承方法

方法描述定义在
isItemActive()Checks whether a menu item is active.CMenu
normalizeItems()Normalizes the items property so that the 'active' state is properly identified for every menu item.CMenu
renderMenu()Renders the menu items.CMenu
renderMenuItem()Renders the content of a menu item.CMenu
renderMenuRecursive()Recursively renders the menu items.CMenu

属性详细

activateItems 属性 (可用自 v1.1.3) public boolean $activateItems;

whether to automatically activate items according to whether their route setting matches the currently requested route. Defaults to true.

activateParents 属性 public boolean $activateParents;

whether to activate parent menu items when one of the corresponding child menu items is active. The activated parent menu items will also have its CSS classes appended with activeCssClass. Defaults to false.

activeCssClass 属性 public string $activeCssClass;

the CSS class to be appended to the active menu item. Defaults to 'active'. If empty, the CSS class of menu items will not be changed.

encodeLabel 属性 public boolean $encodeLabel;

whether the labels for menu items should be HTML-encoded. Defaults to true.

firstItemCssClass 属性 (可用自 v1.1.4) public string $firstItemCssClass;

the CSS class that will be assigned to the first item in the main menu or each submenu. Defaults to null, meaning no such CSS class will be assigned.

hideEmptyItems 属性 public boolean $hideEmptyItems;

whether to hide empty menu items. An empty menu item is one whose 'url' option is not set and which doesn't contain visible child menu items. Defaults to true.

htmlOptions 属性 public array $htmlOptions;

HTML attributes for the menu's root container tag

itemCssClass 属性 (可用自 v1.1.9) public string $itemCssClass;

the CSS class that will be assigned to every item. Defaults to null, meaning no such CSS class will be assigned.

itemTemplate 属性 (可用自 v1.1.1) public string $itemTemplate;

the template used to render an individual menu item. In this template, the token "{menu}" will be replaced with the corresponding menu link or text. If this property is not set, each menu will be rendered without any decoration. This property will be overridden by the 'template' option set in individual menu items via {@items}.

items 属性 public array $items;

list of menu items. Each menu item is specified as an array of name-value pairs. Possible option names include the following:

  • label: string, optional, specifies the menu item label. When encodeLabel is true, the label will be HTML-encoded. If the label is not specified, it defaults to an empty string.
  • url: string or array, optional, specifies the URL of the menu item. It is passed to CHtml::normalizeUrl to generate a valid URL. If this is not set, the menu item will be rendered as a span text.
  • visible: boolean, optional, whether this menu item is visible. Defaults to true. This can be used to control the visibility of menu items based on user permissions.
  • items: array, optional, specifies the sub-menu items. Its format is the same as the parent items.
  • active: boolean, optional, whether this menu item is in active state (currently selected). If a menu item is active and activeClass is not empty, its CSS class will be appended with activeClass. If this option is not set, the menu item will be set active automatically when the current request is triggered by url. Note that the GET parameters not specified in the 'url' option will be ignored.
  • template: string, optional, the template used to render this menu item. When this option is set, it will override the global setting itemTemplate. Please see itemTemplate for more details. This option has been available since version 1.1.1.
  • linkOptions: array, optional, additional HTML attributes to be rendered for the link or span tag of the menu item.
  • itemOptions: array, optional, additional HTML attributes to be rendered for the container tag of the menu item.
  • submenuOptions: array, optional, additional HTML attributes to be rendered for the container of the submenu if this menu item has one. When this option is set, the submenuHtmlOptions property will be ignored for this particular submenu. This option has been available since version 1.1.6.

lastItemCssClass 属性 (可用自 v1.1.4) public string $lastItemCssClass;

the CSS class that will be assigned to the last item in the main menu or each submenu. Defaults to null, meaning no such CSS class will be assigned.

linkLabelWrapper 属性 (可用自 v1.1.4) public string $linkLabelWrapper;

the HTML element name that will be used to wrap the label of all menu links. For example, if this property is set as 'span', a menu item may be rendered as <li><a href="url"><span>label</span></a></li> This is useful when implementing menu items using the sliding window technique. Defaults to null, meaning no wrapper tag will be generated.

submenuHtmlOptions 属性 public array $submenuHtmlOptions;

HTML attributes for the submenu's container tag.

方法详细

init() 方法
public void init()
源码: framework/zii/widgets/CMenu.php#147 (显示) publicfunctioninit()
{
$this->htmlOptions['id']=$this->getId();
$route=$this->getController()->getRoute();
$this->items=$this->normalizeItems($this->items,$route,$hasActiveChild);
}

Initializes the menu widget. This method mainly normalizes the items property. If this method is overridden, make sure the parent implementation is invoked.

isItemActive() 方法
protected boolean isItemActive(array $item, string $route)
$itemarraythe menu item to be checked
$routestringthe route of the current request
{return}booleanwhether the menu item is active
源码: framework/zii/widgets/CMenu.php#301 (显示) protectedfunctionisItemActive($item,$route)
{
if(isset($item['url'])&&is_array($item['url'])&&!strcasecmp(trim($item['url'][0],'/'),$route))
{
if(count($item['url'])>1)
{
foreach(array_splice($item['url'],1)as$name=>$value)
{
if(!isset($_GET[$name])||$_GET[$name]!=$value)
returnfalse;
}
}
returntrue;
}
returnfalse;
}

Checks whether a menu item is active. This is done by checking if the currently requested URL is generated by the 'url' option of the menu item. Note that the GET parameters not specified in the 'url' option will be ignored.

normalizeItems() 方法
protected array normalizeItems(array $items, string $route, boolean &$active)
$itemsarraythe items to be normalized.
$routestringthe route of the current request.
$activebooleanwhether there is an active child menu item.
{return}arraythe normalized menu items
源码: framework/zii/widgets/CMenu.php#253 (显示) protectedfunctionnormalizeItems($items,$route,&$active)
{
foreach($itemsas$i=>$item)
{
if(isset($item['visible'])&&!$item['visible'])
{
unset($items[$i]);
continue;
}
if(!isset($item['label']))
$item['label']='';
if($this->encodeLabel)
$items[$i]['label']=CHtml::encode($item['label']);
$hasActiveChild=false;
if(isset($item['items']))
{
$items[$i]['items']=$this->normalizeItems($item['items'],$route,$hasActiveChild);
if(empty($items[$i]['items'])&&$this->hideEmptyItems)
{
unset($items[$i]['items']);
if(!isset($item['url']))
{
unset($items[$i]);
continue;
}
}
}
if(!isset($item['active']))
{
if($this->activateParents&&$hasActiveChild||$this->activateItems&&$this->isItemActive($item,$route))
$active=$items[$i]['active']=true;
else
$items[$i]['active']=false;
}
elseif($item['active'])
$active=true;
}
returnarray_values($items);
}

Normalizes the items property so that the 'active' state is properly identified for every menu item.

renderMenu() 方法
protected void renderMenu(array $items)
$itemsarraymenu items. Each menu item will be an array with at least two elements: 'label' and 'active'. It may have three other optional elements: 'items', 'linkOptions' and 'itemOptions'.
源码: framework/zii/widgets/CMenu.php#167 (显示) protectedfunctionrenderMenu($items)
{
if(count($items))
{
echoCHtml::openTag('ul',$this->htmlOptions)."n";
$this->renderMenuRecursive($items);
echoCHtml::closeTag('ul');
}
}

Renders the menu items.

renderMenuItem() 方法 (可用自 v1.1.6)
protected string renderMenuItem(array $item)
$itemarraythe menu item to be rendered. Please see items on what data might be in the item.
{return}string
源码: framework/zii/widgets/CMenu.php#235 (显示) protectedfunctionrenderMenuItem($item)
{
if(isset($item['url']))
{
$label=$this->linkLabelWrapper===null?$item['label']:'<'.$this->linkLabelWrapper.'>'.$item['label'].'</'.$this->linkLabelWrapper.'>';
returnCHtml::link($label,$item['url'],isset($item['linkOptions'])?$item['linkOptions']:array());
}
else
returnCHtml::tag('span',isset($item['linkOptions'])?$item['linkOptions']:array(),$item['label']);
}

Renders the content of a menu item. Note that the container and the sub-menus are not rendered here.

renderMenuRecursive() 方法
protected void renderMenuRecursive(array $items)
$itemsarraythe menu items to be rendered recursively
源码: framework/zii/widgets/CMenu.php#181 (显示) protectedfunctionrenderMenuRecursive($items)
{
$count=0;
$n=count($items);
foreach($itemsas$item)
{
$count++;
$options=isset($item['itemOptions'])?$item['itemOptions']:array();
$class=array();
if($item['active']&&$this->activeCssClass!='')
$class[]=$this->activeCssClass;
if($count===1&&$this->firstItemCssClass!==null)
$class[]=$this->firstItemCssClass;
if($count===$n&&$this->lastItemCssClass!==null)
$class[]=$this->lastItemCssClass;
if($this->itemCssClass!==null)
$class[]=$this->itemCssClass;
if($class!==array())
{
if(empty($options['class']))
$options['class']=implode('',$class);
else
$options['class'].=''.implode('',$class);
}

echoCHtml::openTag('li',$options);

$menu=$this->renderMenuItem($item);
if(isset($this->itemTemplate)||isset($item['template']))
{
$template=isset($item['template'])?$item['template']:$this->itemTemplate;
echostrtr($template,array('{menu}'=>$menu));
}
else
echo$menu;

if(isset($item['items'])&&count($item['items']))
{
echo"n".CHtml::openTag('ul',isset($item['submenuOptions'])?$item['submenuOptions']:$this->submenuHtmlOptions)."n";
$this->renderMenuRecursive($item['items']);
echoCHtml::closeTag('ul')."n";
}

echoCHtml::closeTag('li')."n";
}
}

Recursively renders the menu items.

run() 方法
public void run()
源码: framework/zii/widgets/CMenu.php#157 (显示) publicfunctionrun()
{
$this->renderMenu($this->items);
}

Calls renderMenu to render the menu.