CFormElementCollection

CDbHttpSession

Yii Framework v1.1.10 类参考

CFormElementCollection

system.web.form
继承 class CFormElementCollection » CMap » CComponent
实现 Countable, ArrayAccess, Traversable, IteratorAggregate
源自 1.1
版本 $Id: CFormElementCollection.php 3054 2011-03-12 21:30:21Z qiang.xue $
源码 framework/web/form/CFormElementCollection.php
CFormElementCollection 实现存储表单元素的集合。

因为CFormElementCollection继承自CMap,所以它可以向一个关联数组那样来使用。 例如,
$element=$collection['username'];
$collection['username']=array('type'=>'text', 'maxlength'=>128);
$collection['password']=new CFormInputElement(array('type'=>'password'),$form);
$collection[]='some string';


CFormElementCollection 可以存储三种类型的值: 一种是配置数组,一种是CFormElement对象,一种是字符串, 正如上面例子中显示的那样,在内部,这些值将被转换为CFormElement对象。

公共属性

隐藏继承属性

属性类型描述定义在
count integer 返回map中的项目数。 CMap
iterator CMapIterator 返回遍历这个列表的项目的迭代器。 CMap
keys array 返回键名列表 CMap
readOnly boolean 返回值说明这个列表是否为只读。默认为false。 CMap

公共方法

隐藏继承方法

方法描述定义在
__call() 如果类中没有调的方法名,则调用这个方法。 CComponent
__construct() 构造方法。 CFormElementCollection
__get() 返回一个属性值、一个事件处理程序列表或一个行为名称。 CComponent
__isset() 检查一个属性是否为null。 CComponent
__set() 设置一个组件的属性值。 CComponent
__unset() 设置一个组件的属性为null。 CComponent
add() 增加一个项目到集合。 CFormElementCollection
asa() 返回这个名字的行为对象。 CComponent
attachBehavior() 附加一个行为到组件。 CComponent
attachBehaviors() 附加一个行为列表到组件。 CComponent
attachEventHandler() 为事件附加一个事件处理程序。 CComponent
canGetProperty() 确定属性是否可读。 CComponent
canSetProperty() 确定属性是否可写。 CComponent
clear() 删除map中所有项目。 CMap
contains() CMap
copyFrom() 将迭代器中的数据复制到map。 CMap
count() 返回map中的项目数。 CMap
detachBehavior() 从组件中分离一个行为。 CComponent
detachBehaviors() 从组件中分离所有行为。 CComponent
detachEventHandler() 分离一个存在的事件处理程序。 CComponent
disableBehavior() 禁用一个附加行为。 CComponent
disableBehaviors() 禁用组件附加的所有行为。 CComponent
enableBehavior() 启用一个附加行为。 CComponent
enableBehaviors() 启用组件附加的所有行为。 CComponent
evaluateExpression() 计算一个PHP表达式,或根据组件上下文执行回调。 CComponent
getCount() 返回map中的项目数。 CMap
getEventHandlers() 返回一个事件的附加处理程序列表。 CComponent
getIterator() 返回遍历这个列表的项目的迭代器。 CMap
getKeys() 返回返回键名列表 CMap
getReadOnly() 返回返回值说明这个列表是否为只读。默认为false。 CMap
hasEvent() 确定一个事件是否定义。 CComponent
hasEventHandler() 检查事件是否有附加的处理程序。 CComponent
hasProperty() 确定属性是否被定义。 CComponent
itemAt() 返回指定位置的项目。 CMap
mergeArray() 递归合并两个或多个数组。 CMap
mergeWith() 将迭代器的数据整合到map。 CMap
offsetExists() 返回值说明指定位置是否存在元素。 CMap
offsetGet() 返回值指定位置的元素。 CMap
offsetSet() 设置指定位置的元素。 CMap
offsetUnset() 删除指定位置的元素。 CMap
raiseEvent() 发起一个事件。 CComponent
remove() 根据键名移除指定的元素。 CFormElementCollection
toArray() CMap

受保护方法

隐藏继承方法

方法描述定义在
setReadOnly() 设置设置这个列表是否为只读 CMap

方法详细

__construct() 方法
public void __construct(CForm $form, boolean $forButtons=false)
$form CForm 拥有这个集合的表单对象
$forButtons boolean 这个集合是否用来存储按钮。
源码: framework/web/form/CFormElementCollection.php#42 (显示)
public function __construct($form,$forButtons=false)
{
    
parent::__construct();
    
$this->_form=$form;
    
$this->_forButtons=$forButtons;
}

构造方法。

add() 方法
public void add(mixed $key, mixed $value)
$key mixed 键名
$value mixed 键值
源码: framework/web/form/CFormElementCollection.php#58 (显示)
public function add($key,$value)
{
    if(
is_array($value))
    {
        if(
is_string($key))
            
$value['name']=$key;

        if(
$this->_forButtons)
        {
            
$class=$this->_form->buttonElementClass;
            
$element=new $class($value,$this->_form);
        }
        else
        {
            if(!isset(
$value['type']))
                
$value['type']='text';
            if(
$value['type']==='string')
            {
                unset(
$value['type'],$value['name']);
                
$element=new CFormStringElement($value,$this->_form);
            }
            else if(!
strcasecmp(substr($value['type'],-4),'form'))    // a form
            
{
                
$class=$value['type']==='form' get_class($this->_form) : Yii::import($value['type']);
                
$element=new $class($value,null,$this->_form);
            }
            else
            {
                
$class=$this->_form->inputElementClass;
                
$element=new $class($value,$this->_form);
            }
        }
    }
    else if(
$value instanceof CFormElement)
    {
        if(
property_exists($value,'name') && is_string($key))
            
$value->name=$key;
        
$element=$value;
    }
    else
        
$element=new CFormStringElement(array('content'=>$value),$this->_form);
    
parent::add($key,$element);
    
$this->_form->addedElement($key,$element,$this->_forButtons);
}

增加一个项目到集合。 这个方法重写父类的的方法 来确保只有配置数组,字符串,或CFormElement对象 会被存储到集合中。

remove() 方法
public void remove(string $key)
$key string 将要从集合中移除的元素的键名。
源码: framework/web/form/CFormElementCollection.php#107 (显示)
public function remove($key)
{
    if((
$item=parent::remove($key))!==null)
        
$this->_form->removedElement($key,$item,$this->_forButtons);
}

根据键名移除指定的元素。

Copyright © 2008-2011 by Yii Software LLC
All Rights Reserved.