Yii Framework v1.1.10 绫诲弬鑰

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鏄剧ず鍗曚釜鏁版嵁妯″瀷鐨勮缁嗗唴瀹广

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

灞炴ц缁

attributes 灞炴
public array $attributes;

瑕佽缁嗚鍥句腑瑕佹樉绀虹殑涓绯诲垪灞炴с 姣忎釜鏁扮粍鍏冪礌浠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.

涓涓睘鎬у湪鐩稿簲鐨勬暟缁勪腑鎸囧畾锛屽畠鏈夊涓嬪厓绱狅細

baseScriptUrl 灞炴
public string $baseScriptUrl;

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).

cssFile 灞炴
public string $cssFile;

杩欎釜璇︾粏瑙嗗浘浣跨敤鐨凜SS鏂囦欢鐨刄RL銆傞粯璁や负null锛 鎰忓懗鐫浣跨敤闆嗘垚鐨凜SS鏂囦欢銆傚鏋滆繖涓缃负false锛屼綘搴旇璐熻矗鐨勬槑纭殑鍖呭惈闇瑕佺殑CSS鏂囦欢鍦ㄤ綘鐨勯〉涓

data 灞炴
public mixed $data;

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.

formatter 灞炴
public CFormatter getFormatter()
public void setFormatter(CFormatter $value)

鏍煎紡鍖栧疄渚嬨傞粯璁も榝ormat鈥欏簲鐢ㄧ▼搴忕粍浠躲

htmlOptions 灞炴
public array $htmlOptions;

琚敤浜tagName鐨凥TML閫夐」

itemCssClass 灞炴
public array $itemCssClass;

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').

itemTemplate 灞炴
public string $itemTemplate;

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.

鍙傝

nullDisplay 灞炴
public string $nullDisplay;

the text to be displayed when an attribute value is null. Defaults to "Not set".

tagName 灞炴
public string $tagName;

the name of the tag for rendering the detail view. Defaults to 'table'.

鍙傝

鏂规硶璇︾粏

getFormatter() 鏂规硶
public CFormatter getFormatter()
{return} CFormatter 鏍煎紡鍖栧疄渚嬨傞粯璁も榝ormat鈥欏簲鐢ㄧ▼搴忕粍浠躲
婧愮爜锛 framework/zii/widgets/CDetailView.php#228 (鏄剧ず)
public function getFormatter()
{
    if(
$this->_formatter===null)
        
$this->_formatter=Yii::app()->format;
    return 
$this->_formatter;
}

init() 鏂规硶
public void init()
婧愮爜锛 framework/zii/widgets/CDetailView.php#134 (鏄剧ず)
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);
    }
}

鍒濆鍖栬缁嗚鍥俱 杩欎釜鏂规硶灏嗗垵濮嬪寲蹇呴渶鐨勫睘鎬у笺

run() 鏂规硶
public void run()
婧愮爜锛 framework/zii/widgets/CDetailView.php#166 (鏄剧ず)
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

setFormatter() 鏂规硶
public void setFormatter(CFormatter $value)
$value CFormatter 鏍煎紡鍖栧疄渚
婧愮爜锛 framework/zii/widgets/CDetailView.php#238 (鏄剧ず)
public function setFormatter($value)
{
    
$this->_formatter=$value;
}

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