DetailView
API reference › yii › widgets › DetailView
DetailView displays the detail of a single data $model.
DetailView is best used for displaying a model in a regular format (e.g. each model attribute
is displayed as a row in a table.) The model can be either an instance of Model
or an associative array.
DetailView uses the $attributes property to determines which model attributes
should be displayed and how they should be formatted.
A typical usage of DetailView is as follows:
echo DetailView::widget([ 'model' => $model, 'attributes' => [ 'title', // title attribute (in plain text) 'description:html', // description attribute in HTML [ // the owner name of the model 'label' => 'Owner', 'value' => $model->owner->name, ], 'created_at:datetime', // creation date formatted as datetime ],]);For more details and usage information on DetailView, see the guide article on data widgets.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$attributes | array | A list of attributes to be displayed in the detail view. Default: null. | DetailView |
$autoIdPrefix static | string | The prefix to the automatically generated widget IDs. Default: 'w'. | yii\ |
$behaviors read-only | array<yii\<static>> | List of behaviors attached to this component. | yii\ |
$formatter | array|yii\| | The formatter used to format model attribute values into displayable texts. Default: null. | DetailView |
$id | string | Id of the widget. | yii\ |
$model | array| | The data model whose details are to be displayed. Default: null. | DetailView |
$options | array| | The HTML attributes for the container tag of this widget. Default: ['class' => 'detail-view']. | DetailView |
$template | string| | The template used to render a single attribute. Default: '<tr><th{captionOptions}>{label}</th><td{contentOptions}>{value}</td></tr>'. | DetailView |
$view | yii\ | The view object that can be used to render views or view files. | yii\ |
$viewPath read-only | string | The directory containing the view files for this widget. | yii\ |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__clone() | This method is called after the object is created by cloning an existing one. | yii\ |
__get() | Returns the value of a component property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets the value of a component property. | yii\ |
__unset() | Sets a component property to be null. | yii\ |
afterRun() | This method is invoked right after a widget is executed. | yii\ |
attachBehavior() | Attaches a behavior to this component. | yii\ |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\ |
beforeRun() | This method is invoked right before the widget is executed. | yii\ |
begin() static | Begins a widget. | yii\ |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
detachBehavior() | Detaches a behavior from the component. | yii\ |
detachBehaviors() | Detaches all behaviors from the component. | yii\ |
end() static | Ends a widget. | yii\ |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\ |
getBehavior() | Returns the named behavior object. | yii\ |
getBehaviors() | Returns all behaviors attached to this component. | yii\ |
getId() | Returns the ID of the widget. | yii\ |
getView() | Returns the view object that can be used to render views or view files. | yii\ |
getViewPath() | Returns the directory containing the view files for this widget. | yii\ |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\ |
init() | Initializes the detail view. | DetailView |
off() | Detaches an existing event handler from this component. | yii\ |
on() | Attaches an event handler to an event. | yii\ |
render() | Renders a view. | yii\ |
renderFile() | Renders a view file. | yii\ |
run() | Renders the detail view. | DetailView |
setId() | Sets the ID of the widget. | yii\ |
setView() | Sets the view object to be used by this widget. | yii\ |
trigger() | Triggers an event. | yii\ |
widget() static | Creates a widget instance and runs it. | yii\ |
normalizeAttributes() protected | Normalizes the attribute specifications. | DetailView |
renderAttribute() protected | Renders a single attribute. | DetailView |
Events
Section titled “Events”| Event | Type | Description | Defined by |
|---|---|---|---|
EVENT_INIT | yii\ | An event that is triggered when the widget is initialized via init(). Event name: 'init'. Available since 2.0.11. | yii\ |
EVENT_BEFORE_RUN | yii\ | An event raised right before executing a widget. Event name: 'beforeRun'. Available since 2.0.11. | yii\ |
EVENT_AFTER_RUN | yii\ | An event raised right after executing a widget. Event name: 'afterRun'. Available since 2.0.11. | yii\ |
Property details
Section titled “Property details”$attributes
Section titled “$attributes ”public array $attributes = nullA 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 attribute, attribute:format or attribute:format:label,
where attribute refers to the attribute name, and format represents the format of the attribute. The format
is passed to the Formatter::format() method to format an attribute value into a displayable text.
Please refer to Formatter for the supported types. Both format and label are optional.
They will take default values if absent.
An attribute can also be specified in terms of an array with the following elements:
-
attribute: the attribute name. This is required if eitherlabelorvalueis not specified. -
label: the label associated with the attribute. If this is not specified, it will be generated from the attribute name. -
value: the value to be displayed. If this is not specified, it will be retrieved from$modelusing the attribute name by callingArrayHelper::getValue(). Note that this value will be formatted into a displayable text according to theformatoption. Since version 2.0.11 it can be defined as closure with the following parameters:function ($model, $widget)$modelrefers to displayed model and$widgetis an instance ofDetailViewwidget. -
format: the type of the value that determines how the value would be formatted into a displayable text. Please refer toFormatterfor supported types andFormatter::format()on how to specify this value. -
visible: whether the attribute is visible. If set tofalse, the attribute will NOT be displayed. -
contentOptions: the HTML attributes to customize value tag. For example:['class' => 'bg-red']. Please refer toBaseHtml::renderTagAttributes()for the supported syntax. -
captionOptions: the HTML attributes to customize label tag. For example:['class' => 'bg-red']. Please refer toBaseHtml::renderTagAttributes()for the supported syntax.
$formatter
Section titled “$formatter ”public array|yii\i18n\Formatter|null $formatter = nullThe formatter used to format model attribute values into displayable texts.
This can be either an instance of Formatter or an configuration array for creating the Formatter
instance. If this property is not set, the formatter application component will be used.
$model
Section titled “$model ”public array|object $model = nullThe data model whose details are to be displayed.
This can be a Model instance,
an associative array, an object that implements Arrayable interface or simply an object with defined
public accessible non-static properties.
$options
Section titled “$options ”public array|null $options = ['class' => 'detail-view']The HTML attributes for the container tag of this widget.
The tag option specifies
what container tag should be used. It defaults to table if not set.
$template
Section titled “$template ”public string|callable $template = '<tr><th{captionOptions}>{label}</th><td{contentOptions}>{value}</td></tr>'The template used to render a single attribute.
If a string, the token {label}
and {value} will be replaced with the label and the value of the corresponding attribute.
If a callback (e.g. an anonymous function), the signature must be as follows:
function ($attribute, $index, $widget)where $attribute refer to the specification of the attribute being rendered, $index is the zero-based
index of the attribute in the $attributes array, and $widget refers to this widget instance.
Since Version 2.0.10, the tokens {captionOptions} and {contentOptions} are available, which will represent
HTML attributes of HTML container elements for the label and value.
Method details
Section titled “Method details”init()
Section titled “init() ”public function init(): mixedInitializes the detail view.
This method will initialize required property values.
Returns
mixed
run()
Section titled “run() ”public function run(): mixedRenders the detail view.
This is the main entry of the whole detail view rendering.
Returns
mixed
normalizeAttributes()
Section titled “normalizeAttributes() ”protected function normalizeAttributes(): mixedNormalizes the attribute specifications.
renderAttribute()
Section titled “renderAttribute() ”protected function renderAttribute(array $attribute, int $index): stringRenders a single attribute.
Parameters
| Parameter | Type | Description |
|---|---|---|
$attribute | array | The specification of the attribute to be rendered. |
$index | int | The zero-based index of the attribute in the $attributes array |
Returns
string: The rendering result