CDetailView
鍖 | zii.widgets |
---|---|
缁ф壙 | class CDetailView » CWidget » CBaseController » CComponent |
婧愯嚜 | 1.1 |
鐗堟湰 | $Id: CDetailView.php 3427 2011-10-25 00:03:52Z alexander.makarow $ |
婧愮爜 | framework/zii/widgets/CDetailView.php |
CDetailView闈炲父閫傚悎鐢ㄦ潵鐢ㄨ鍒欑殑鏍煎紡鏄剧ず涓涓ā鍨嬶紙渚嬪锛屾瘡涓涓ā鍨嬬殑灞炴ц鏄剧ず涓鸿〃鏍肩殑涓琛屻傦級 璇ユā鍨嬪彲浠ユ槸CModel 鐨勫疄渚嬫垨鐩稿叧鐨勬暟缁勩
CDetailView浣跨敤attributes灞炴х‘瀹氬摢涓ā鍨嬪睘鎬 搴旇琚樉绀哄拰瀹冧滑搴旇鎬庢牱琚牸寮忓寲銆
CDetailView鐨勪竴涓吀鍨嬬敤娉曞涓嬶細
$this->widget('zii.widgets.CDetailView', array( 'data'=>$model, 'attributes'=>array( 'title', // title attribute (in plain text) 'owner.name', // an attribute of the related object "owner" 'description:html', // description attribute in HTML array( // related city displayed as a link 'label'=>'City', 'type'=>'raw', 'value'=>CHtml::link(CHtml::encode($model->city->name), array('city/view','id'=>$model->city->id)), ), ), ));
鍏叡灞炴
灞炴 | 绫诲瀷 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|---|
actionPrefix | string | actions鐨処D鐨勫墠缂銆 褰撳井浠跺湪CController::actions涓0鏄庝簡 鍔ㄤ綔鎻愪緵鑰咃紝鍙互涓哄叾鍔ㄤ綔鐨処D鎸囧畾鍓嶇紑浠ュ尯鍒 浜庡埆鐨勫井浠舵垨鎺у埗鍣ㄣ傚綋寰欢鐢ㄤ簬鎺у埗鍣 鐨勮鍥句腑鏃讹紝蹇呴』閰嶇疆鍚屾牱鐨勫墠缂銆 | CWidget |
attributes | array | 瑕佽缁嗚鍥句腑瑕佹樉绀虹殑涓绯诲垪灞炴с
姣忎釜鏁扮粍鍏冪礌浠h〃鏄剧ず涓涓壒瀹氱殑灞炴ц鑼冦
涓涓睘鎬у彲浠ヨ鎸囧畾涓轰竴涓瓧绗︿覆锛屼娇鐢ㄦ牸寮"Name:Type:Label"銆 "Type"鍜"Label"鏄彲閫夌殑銆 "Name" refers to the attribute name. |
CDetailView |
baseScriptUrl | string | the base script URL for all detail view resources (e. | CDetailView |
controller | CController | 杩斿洖姝ゅ井浠舵墍灞炵殑鎺у埗鍣ㄣ | CWidget |
cssFile | string | 杩欎釜璇︾粏瑙嗗浘浣跨敤鐨凜SS鏂囦欢鐨刄RL銆傞粯璁や负null锛 鎰忓懗鐫浣跨敤闆嗘垚鐨凜SS鏂囦欢銆傚鏋滆繖涓缃负false锛屼綘搴旇璐熻矗鐨勬槑纭殑鍖呭惈闇瑕佺殑CSS鏂囦欢鍦ㄤ綘鐨勯〉涓 | CDetailView |
data | mixed | the data model whose details are to be displayed. | CDetailView |
formatter | CFormatter | 鏍煎紡鍖栧疄渚嬨傞粯璁も榝ormat鈥欏簲鐢ㄧ▼搴忕粍浠躲 | CDetailView |
htmlOptions | array | 琚敤浜tagName鐨凥TML閫夐」 | CDetailView |
id | string | 杩斿洖姝ゅ井浠剁殑ID銆傚鏋滈渶瑕佺殑璇濓紝灏嗙敓浜т竴涓柊鐨処D骞跺皢鍏惰繑鍥炪 | CWidget |
itemCssClass | array | the CSS class names for the items displaying attribute values. | CDetailView |
itemTemplate | string | the template used to render a single attribute. | CDetailView |
nullDisplay | string | the text to be displayed when an attribute value is null. | CDetailView |
owner | CBaseController | 杩斿洖姝ゅ井浠剁殑鎵鏈夎呮垨鍒涘缓鑰呫 | CWidget |
skin | mixed | 寰欢浣跨敤鐨勭毊鑲ょ殑鍚嶇О銆傞粯璁や负鈥渄efault鈥濄 濡傛灉姝ゅ睘鎬ц缃负false锛屽井浠跺皢涓嶄細鏈夌毊鑲よ浣跨敤銆 | CWidget |
tagName | string | the name of the tag for rendering the detail view. | CDetailView |
viewPath | string | 杩斿洖鍖呭惈姝ゅ井浠舵墍闇鐨勮鍥炬枃浠剁殑璺緞銆 | CWidget |
鍏叡鏂规硶
鏂规硶 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|
__call() | 濡傛灉绫讳腑娌℃湁璋冪殑鏂规硶鍚嶏紝鍒欒皟鐢ㄨ繖涓柟娉曘 | CComponent |
__construct() | 鏋勯犲櫒銆 | CWidget |
__get() | 杩斿洖涓涓睘鎬у笺佷竴涓簨浠跺鐞嗙▼搴忓垪琛ㄦ垨涓涓涓哄悕绉般 | CComponent |
__isset() | 妫鏌ヤ竴涓睘鎬ф槸鍚︿负null銆 | CComponent |
__set() | 璁剧疆涓涓粍浠剁殑灞炴у笺 | CComponent |
__unset() | 璁剧疆涓涓粍浠剁殑灞炴т负null銆 | CComponent |
actions() | 杩斿洖姝idget浣跨敤鐨勫姩浣滅殑鍒楄〃銆 | 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() | 璁$畻涓涓狿HP琛ㄨ揪寮忥紝鎴栨牴鎹粍浠朵笂涓嬫枃鎵ц鍥炶皟銆 | CComponent |
getController() | 杩斿洖姝ゅ井浠舵墍灞炵殑鎺у埗鍣ㄣ | CWidget |
getEventHandlers() | 杩斿洖涓涓簨浠剁殑闄勫姞澶勭悊绋嬪簭鍒楄〃銆 | CComponent |
getFormatter() | 杩斿洖鏍煎紡鍖栧疄渚嬨傞粯璁も榝ormat鈥欏簲鐢ㄧ▼搴忕粍浠躲 | CDetailView |
getId() | 杩斿洖姝ゅ井浠剁殑ID銆傚鏋滈渶瑕佺殑璇濓紝灏嗙敓浜т竴涓柊鐨処D骞跺皢鍏惰繑鍥炪 | CWidget |
getOwner() | 杩斿洖姝ゅ井浠剁殑鎵鏈夎呮垨鍒涘缓鑰呫 | CWidget |
getViewFile() | 鏍规嵁瑙嗗浘鍚嶆煡鎵捐鍥炬枃浠躲 | CWidget |
getViewPath() | 杩斿洖鍖呭惈姝ゅ井浠舵墍闇鐨勮鍥炬枃浠剁殑璺緞銆 | CWidget |
hasEvent() | 纭畾涓涓簨浠舵槸鍚﹀畾涔夈 | CComponent |
hasEventHandler() | 妫鏌ヤ簨浠舵槸鍚︽湁闄勫姞鐨勫鐞嗙▼搴忋 | CComponent |
hasProperty() | 纭畾灞炴ф槸鍚﹁瀹氫箟銆 | CComponent |
init() | 鍒濆鍖栬缁嗚鍥俱 | CDetailView |
raiseEvent() | 鍙戣捣涓涓簨浠躲 | CComponent |
render() | 娓叉煋涓涓鍥俱 | CWidget |
renderFile() | Renders a view file. | CBaseController |
renderInternal() | Renders a view file. | CBaseController |
run() | 娓叉煋璇︾粏瑙嗗浘銆 | CDetailView |
setFormatter() | 璁剧疆鏍煎紡鍖栧疄渚 | CDetailView |
setId() | 璁剧疆姝ゅ井浠剁殑ID銆 | CWidget |
widget() | Creates a widget and executes it. | CBaseController |
灞炴ц缁
瑕佽缁嗚鍥句腑瑕佹樉绀虹殑涓绯诲垪灞炴с
姣忎釜鏁扮粍鍏冪礌浠h〃鏄剧ず涓涓壒瀹氱殑灞炴ц鑼冦
涓涓睘鎬у彲浠ヨ鎸囧畾涓轰竴涓瓧绗︿覆锛屼娇鐢ㄦ牸寮"Name:Type:Label"銆
"Type"鍜"Label"鏄彲閫夌殑銆
"Name" refers to the attribute name. It can be either a property (e.g. "title") or a sub-property (e.g. "owner.username").
"Label" represents the label for the attribute display. If it is not given, "Name" will be used to generate the appropriate label.
"Type" represents the type of the attribute. It determines how the attribute value should be formatted and displayed.
It is defaulted to be 'text'.
"Type" should be recognizable by the formatter. In particular, if "Type" is "xyz", then the "formatXyz" method
of formatter will be invoked to format the attribute value for display. By default when CFormatter is used,
these "Type" values are valid: raw, text, ntext, html, date, time, datetime, boolean, number, email, image, url.
For more details about these types, please refer to CFormatter.
涓涓睘鎬у湪鐩稿簲鐨勬暟缁勪腑鎸囧畾锛屽畠鏈夊涓嬪厓绱狅細
- label: the label associated with the attribute. If this is not specified, the following "name" element will be used to generate an appropriate label.
- name: the name of the attribute. This can be either a property or a sub-property of the model. If the below "value" element is specified, this will be ignored.
- value: the value to be displayed. If this is not specified, the above "name" element will be used to retrieve the corresponding attribute value for display. Note that this value will be formatted according to the "type" option as described below.
- type: the type of the attribute that determines how the attribute value would be formatted. Please see above for possible values.
- cssClass: the CSS class to be used for this item. This option is available since version 1.1.3.
- template: the template used to render the attribute. If this is not specified, itemTemplate will be used instead. For more details on how to set this option, please refer to itemTemplate. This option is available since version 1.1.1.
- visible: whether the attribute is visible. If set to
false
, the table row for the attribute will not be rendered. This option is available since version 1.1.5.
the base script URL for all detail view resources (e.g. javascript, CSS file, images). Defaults to null, meaning using the integrated detail view resources (which are published as assets).
杩欎釜璇︾粏瑙嗗浘浣跨敤鐨凜SS鏂囦欢鐨刄RL銆傞粯璁や负null锛 鎰忓懗鐫浣跨敤闆嗘垚鐨凜SS鏂囦欢銆傚鏋滆繖涓缃负false锛屼綘搴旇璐熻矗鐨勬槑纭殑鍖呭惈闇瑕佺殑CSS鏂囦欢鍦ㄤ綘鐨勯〉涓
the data model whose details are to be displayed. This can be either a CModel instance (e.g. a CActiveRecord object or a CFormModel object) or an associative array.
鏍煎紡鍖栧疄渚嬨傞粯璁も榝ormat鈥欏簲鐢ㄧ▼搴忕粍浠躲
琚敤浜tagName鐨凥TML閫夐」
the CSS class names for the items displaying attribute values. If multiple CSS class names are given,
they will be assigned to the items sequentially and repeatedly.
Defaults to array('odd', 'even')
.
the template used to render a single attribute. Defaults to a table row. These tokens are recognized: "{class}", "{label}" and "{value}". They will be replaced with the CSS class name for the item, the label and the attribute value, respectively.
鍙傝
the text to be displayed when an attribute value is null. Defaults to "Not set".
the name of the tag for rendering the detail view. Defaults to 'table'.
鍙傝
鏂规硶璇︾粏
public CFormatter getFormatter()
| ||
{return} | CFormatter | 鏍煎紡鍖栧疄渚嬨傞粯璁も榝ormat鈥欏簲鐢ㄧ▼搴忕粍浠躲 |
public function getFormatter()
{
if($this->_formatter===null)
$this->_formatter=Yii::app()->format;
return $this->_formatter;
}
public void init()
|
public function init()
{
if($this->data===null)
throw new CException(Yii::t('zii','Please specify the "data" property.'));
if($this->attributes===null)
{
if($this->data instanceof CModel)
$this->attributes=$this->data->attributeNames();
else if(is_array($this->data))
$this->attributes=array_keys($this->data);
else
throw new CException(Yii::t('zii','Please specify the "attributes" property.'));
}
if($this->nullDisplay===null)
$this->nullDisplay='<span class="null">'.Yii::t('zii','Not set').'</span>';
$this->htmlOptions['id']=$this->getId();
if($this->baseScriptUrl===null)
$this->baseScriptUrl=Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('zii.widgets.assets')).'/detailview';
if($this->cssFile!==false)
{
if($this->cssFile===null)
$this->cssFile=$this->baseScriptUrl.'/styles.css';
Yii::app()->getClientScript()->registerCssFile($this->cssFile);
}
}
鍒濆鍖栬缁嗚鍥俱 杩欎釜鏂规硶灏嗗垵濮嬪寲蹇呴渶鐨勫睘鎬у笺
public void run()
|
public function run()
{
$formatter=$this->getFormatter();
echo CHtml::openTag($this->tagName,$this->htmlOptions);
$i=0;
$n=is_array($this->itemCssClass) ? count($this->itemCssClass) : 0;
foreach($this->attributes as $attribute)
{
if(is_string($attribute))
{
if(!preg_match('/^([\w\.]+)(:(\w*))?(:(.*))?$/',$attribute,$matches))
throw new CException(Yii::t('zii','The attribute must be specified in the format of "Name:Type:Label", where "Type" and "Label" are optional.'));
$attribute=array(
'name'=>$matches[1],
'type'=>isset($matches[3]) ? $matches[3] : 'text',
);
if(isset($matches[5]))
$attribute['label']=$matches[5];
}
if(isset($attribute['visible']) && !$attribute['visible'])
continue;
$tr=array('{label}'=>'', '{class}'=>$n ? $this->itemCssClass[$i%$n] : '');
if(isset($attribute['cssClass']))
$tr['{class}']=$attribute['cssClass'].' '.($n ? $tr['{class}'] : '');
if(isset($attribute['label']))
$tr['{label}']=$attribute['label'];
else if(isset($attribute['name']))
{
if($this->data instanceof CModel)
$tr['{label}']=$this->data->getAttributeLabel($attribute['name']);
else
$tr['{label}']=ucwords(trim(strtolower(str_replace(array('-','_','.'),' ',preg_replace('/(?<![A-Z])[A-Z]/', ' \0', $attribute['name'])))));
}
if(!isset($attribute['type']))
$attribute['type']='text';
if(isset($attribute['value']))
$value=$attribute['value'];
else if(isset($attribute['name']))
$value=CHtml::value($this->data,$attribute['name']);
else
$value=null;
$tr['{value}']=$value===null ? $this->nullDisplay : $formatter->format($value,$attribute['type']);
echo strtr(isset($attribute['template']) ? $attribute['template'] : $this->itemTemplate,$tr);
$i++;
}
echo CHtml::closeTag($this->tagName);
}
娓叉煋璇︾粏瑙嗗浘銆 杩欎釜鏁翠釜璇︾粏瑙嗗浘娓叉煋鐨勪富瑕佸叆鍙c
public void setFormatter(CFormatter $value)
| ||
$value | CFormatter | 鏍煎紡鍖栧疄渚 |
public function setFormatter($value)
{
$this->_formatter=$value;
}