Package | zii.widgets |
---|---|
Inheritance | class CDetailView » CWidget » CBaseController » CComponent |
Since | 1.1 |
Source Code | framework/zii/widgets/CDetailView.php |
$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)), ), ), ));
Property | Type | Description | Defined By |
---|---|---|---|
actionPrefix | string | the prefix to the IDs of the actions. | CWidget |
attributes | array | a list of attributes to be displayed in the detail view. | CDetailView |
baseScriptUrl | string | the base script URL for all detail view resources (e.g. javascript, CSS file, images). | CDetailView |
controller | CController | Returns the controller that this widget belongs to. | CWidget |
cssFile | string | the URL of the CSS file used by this detail view. | CDetailView |
data | mixed | the data model whose details are to be displayed. | CDetailView |
formatter | CFormatter | the formatter instance. | CDetailView |
htmlOptions | array | the HTML options used for tagName | CDetailView |
id | string | Returns the ID of the widget or generates a new one if requested. | 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 | Returns the owner/creator of this widget. | CWidget |
skin | mixed | the name of the skin to be used by this widget. | CWidget |
tagName | string | the name of the tag for rendering the detail view. | CDetailView |
viewPath | string | Returns the directory containing the view files for this widget. | CWidget |
Method | Description | Defined By |
---|---|---|
__call() | Calls the named method which is not a class method. | CComponent |
__construct() | Constructor. | CWidget |
__get() | Returns a property value, an event handler list or a behavior based on its name. | CComponent |
__isset() | Checks if a property value is null. | CComponent |
__set() | Sets value of a component property. | CComponent |
__unset() | Sets a component property to be null. | CComponent |
actions() | Returns a list of actions that are used by this widget. | CWidget |
asa() | Returns the named behavior object. | CComponent |
attachBehavior() | Attaches a behavior to this component. | CComponent |
attachBehaviors() | Attaches a list of behaviors to the component. | CComponent |
attachEventHandler() | Attaches an event handler to an event. | 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() | Determines whether a property can be read. | CComponent |
canSetProperty() | Determines whether a property can be set. | CComponent |
createWidget() | Creates a widget and initializes it. | CBaseController |
detachBehavior() | Detaches a behavior from the component. | CComponent |
detachBehaviors() | Detaches all behaviors from the component. | CComponent |
detachEventHandler() | Detaches an existing event handler. | CComponent |
disableBehavior() | Disables an attached behavior. | CComponent |
disableBehaviors() | Disables all behaviors attached to this component. | CComponent |
enableBehavior() | Enables an attached behavior. | CComponent |
enableBehaviors() | Enables all behaviors attached to this component. | 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() | Evaluates a PHP expression or callback under the context of this component. | CComponent |
getController() | Returns the controller that this widget belongs to. | CWidget |
getEventHandlers() | Returns the list of attached event handlers for an event. | CComponent |
getFormatter() | Returns the formatter instance. Defaults to the 'format' application component. | CDetailView |
getId() | Returns the ID of the widget or generates a new one if requested. | CWidget |
getOwner() | Returns the owner/creator of this widget. | CWidget |
getViewFile() | Looks for the view script file according to the view name. | CWidget |
getViewPath() | Returns the directory containing the view files for this widget. | CWidget |
hasEvent() | Determines whether an event is defined. | CComponent |
hasEventHandler() | Checks whether the named event has attached handlers. | CComponent |
hasProperty() | Determines whether a property is defined. | CComponent |
init() | Initializes the detail view. | CDetailView |
raiseEvent() | Raises an event. | CComponent |
render() | Renders a view. | CWidget |
renderFile() | Renders a view file. | CBaseController |
renderInternal() | Renders a view file. | CBaseController |
run() | Renders the detail view. | CDetailView |
setFormatter() | Sets the formatter instance | CDetailView |
setId() | Sets the ID of the widget. | CWidget |
widget() | Creates a widget and executes it. | CBaseController |
Method | Description | Defined By |
---|---|---|
renderItem() | This method is used by run() to render item row | CDetailView |
a list of attributes to be displayed in the detail view. Each array element
represents the specification for displaying one particular attribute.
An attribute can be specified as a string in the format of "Name:Type:Label".
Both "Type" and "Label" are optional.
"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.
An attribute can also be specified in terms of an array with the following elements:
function($data)
where data refers to
the data property of the detail view widget.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).
the URL of the CSS file used by this detail view. Defaults to null, meaning using the integrated CSS file. If this is set false, you are responsible to explicitly include the necessary CSS file in your page.
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.
the formatter instance. Defaults to the 'format' application component.
the HTML options used for tagName
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'. If set to null, no tag will be rendered.
public CFormatter getFormatter()
| ||
{return} | CFormatter | the formatter instance. Defaults to the 'format' application component. |
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();
elseif(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>';
if(isset($this->htmlOptions['id']))
$this->id=$this->htmlOptions['id'];
else
$this->htmlOptions['id']=$this->id;
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);
}
}
Initializes the detail view. This method will initialize required property values.
protected void renderItem(array $options, string $templateData)
| ||
$options | array | config options for this item/attribute from attributes |
$templateData | string | data that will be inserted into itemTemplate |
protected function renderItem($options,$templateData)
{
echo strtr(isset($options['template']) ? $options['template'] : $this->itemTemplate,$templateData);
}
This method is used by run() to render item row
public void run()
|
public function run()
{
$formatter=$this->getFormatter();
if ($this->tagName!==null)
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'];
elseif(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=is_object($attribute['value']) && get_class($attribute['value']) === 'Closure' ? call_user_func($attribute['value'],$this->data) : $attribute['value'];
elseif(isset($attribute['name']))
$value=CHtml::value($this->data,$attribute['name']);
else
$value=null;
$tr['{value}']=$value===null ? $this->nullDisplay : $formatter->format($value,$attribute['type']);
$this->renderItem($attribute, $tr);
$i++;
}
if ($this->tagName!==null)
echo CHtml::closeTag($this->tagName);
}
Renders the detail view. This is the main entry of the whole detail view rendering.
public void setFormatter(CFormatter $value)
| ||
$value | CFormatter | the formatter instance |
public function setFormatter($value)
{
$this->_formatter=$value;
}
Foreign Key Relationship - Two
If you want to override the label, the attribute list entry needs to be changed to one of the form:
array('value' => $model->country_relation->name, 'label' => 'Country'
Foreign Key Relationship
If in a table you have a relationship of the form:
'country_relation' => array(self::BELONGS_TO, 'Country', 'country_id'),
to display the country name from the country table (rather than the country_id) all you need in the zii.widgets.CDetailView attribute list is:
...
'country_relation.name',
...
Hide Columns Based On Access Permissions
If you're using Role Based Access and want certain columns visible only to users with the right permissions, this is an easy way to accomplish that:
... 'attributes' => array( 'Column1Name', 'Column2Name', array( 'name'=>'Column3Name', 'visible'=>Yii::app()->user->checkAccess('RoleOrTaskOrUserName'), ), ...
Signup or Login in order to comment.