Class yii\widgets\DetailView

继承yii\widgets\DetailView » yii\base\Widget » yii\base\Component » yii\base\BaseObject
实现yii\base\Configurable, yii\base\ViewContextInterface
可用自版本2.0
源代码 https://github.com/yiisoft/yii2/blob/master/framework/widgets/DetailView.php

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 yii\base\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.

公共属性

隐藏继承的属性

属性类型描述定义在
$attributes array A list of attributes to be displayed in the detail view. yii\widgets\DetailView
$autoIdPrefix string The prefix to the automatically generated widget IDs. yii\base\Widget
$behaviors yii\base\Behavior[] 附加到此组件的行为列表. yii\base\Component
$counter integer A counter used to generate $id for widgets. yii\base\Widget
$formatter array|yii\i18n\Formatter The formatter used to format model attribute values into displayable texts. yii\widgets\DetailView
$id string ID of the widget. yii\base\Widget
$model array|object The data model whose details are to be displayed. yii\widgets\DetailView
$options array The HTML attributes for the container tag of this widget. yii\widgets\DetailView
$stack yii\base\Widget[] The widgets that are currently being rendered (not ended). yii\base\Widget
$template string|callable The template used to render a single attribute. yii\widgets\DetailView
$view yii\web\View The view object that can be used to render views or view files. yii\base\Widget
$viewPath string The directory containing the view files for this widget. yii\base\Widget

公共方法

隐藏继承的方法

方法描述定义在
__call() 调用不是类方法的指定的方法. yii\base\Component
__clone() 在通过克隆现有对象创建对象之后调用此方法. yii\base\Component
__construct() 构造函数. yii\base\BaseObject
__get() 返回组件属性的值. yii\base\Component
__isset() 检查属性是否已设置, 即已定义且不为null. yii\base\Component
__set() 设置组件属性的值. yii\base\Component
__unset() 将组件属性设置为null. yii\base\Component
afterRun() This method is invoked right after a widget is executed. yii\base\Widget
attachBehavior() 将行为附加到此组件. yii\base\Component
attachBehaviors() 将行为列表附加到组件. yii\base\Component
beforeRun() This method is invoked right before the widget is executed. yii\base\Widget
begin() Begins a widget. yii\base\Widget
behaviors() 返回此组件应该具有的行为列表. yii\base\Component
canGetProperty() 返回一个值, 指示属性是否可读取. yii\base\Component
canSetProperty() 返回一个值, 指示属性是否可设置. yii\base\Component
className() 返回此类的完全限定名称. yii\base\BaseObject
detachBehavior() 从组件中分离一个行为. yii\base\Component
detachBehaviors() 分离组件中的所有行为. yii\base\Component
end() Ends a widget. yii\base\Widget
ensureBehaviors() 确保在 behaviors() 中声明的行为已附加到此组件. yii\base\Component
getBehavior() 返回指定的行为对象. yii\base\Component
getBehaviors() 返回附加到此组件的所有行为. yii\base\Component
getId() Returns the ID of the widget. yii\base\Widget
getView() Returns the view object that can be used to render views or view files. yii\base\Widget
getViewPath() Returns the directory containing the view files for this widget. yii\base\Widget
hasEventHandlers() 返回一个值, 指示是否有任何处理程序附加到指定的事件. yii\base\Component
hasMethod() 返回一个值, 指示方法是否已定义. yii\base\Component
hasProperty() 返回一个值, 指示属性是否已定义. yii\base\Component
init() Initializes the detail view. yii\widgets\DetailView
off() 从该组件分离现有的事件处理程序. yii\base\Component
on() 将事件处理程序附加到事件. yii\base\Component
render() Renders a view. yii\base\Widget
renderFile() Renders a view file. yii\base\Widget
run() Renders the detail view. yii\widgets\DetailView
setId() Sets the ID of the widget. yii\base\Widget
setView() Sets the view object to be used by this widget. yii\base\Widget
trigger() 触发事件. yii\base\Component
widget() Creates a widget instance and runs it. yii\base\Widget

受保护的方法

隐藏继承的方法

方法描述定义在
normalizeAttributes() Normalizes the attribute specifications. yii\widgets\DetailView
renderAttribute() Renders a single attribute. yii\widgets\DetailView

事件

隐藏继承的事件

事件类型描述定义在
EVENT_AFTER_RUN yii\base\WidgetEvent An event raised right after executing a widget. (available since version 2.0.11) yii\base\Widget
EVENT_BEFORE_RUN yii\base\WidgetEvent An event raised right before executing a widget. (available since version 2.0.11) yii\base\Widget
EVENT_INIT yii\base\Event An event that is triggered when the widget is initialized via init(). (available since version 2.0.11) yii\base\Widget

属性详情

$attributes public 属性

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 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 yii\i18n\Formatter::format() method to format an attribute value into a displayable text. Please refer to yii\i18n\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 either label or value is 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 $model using the attribute name by calling yii\helpers\ArrayHelper::getValue(). Note that this value will be formatted into a displayable text according to the format option. Since version 2.0.11 it can be defined as closure with the following parameters:

    function ($model, $widget)
    

    $model refers to displayed model and $widget is an instance of DetailView widget.

  • format: the type of the value that determines how the value would be formatted into a displayable text. Please refer to yii\i18n\Formatter for supported types and yii\i18n\Formatter::format() on how to specify this value.
  • visible: whether the attribute is visible. If set to false, the attribute will NOT be displayed.
  • contentOptions: the HTML attributes to customize value tag. For example: ['class' => 'bg-red']. Please refer to yii\helpers\BaseHtml::renderTagAttributes() for the supported syntax.
  • captionOptions: the HTML attributes to customize label tag. For example: ['class' => 'bg-red']. Please refer to yii\helpers\BaseHtml::renderTagAttributes() for the supported syntax.
public array $attributes null
$formatter public 属性

The formatter used to format model attribute values into displayable texts. This can be either an instance of yii\i18n\Formatter or an configuration array for creating the yii\i18n\Formatter instance. If this property is not set, the formatter application component will be used.

$model public 属性

The data model whose details are to be displayed. This can be a yii\base\Model instance, an associative array, an object that implements yii\base\Arrayable interface or simply an object with defined public accessible non-static properties.

public array|object $model null
$options public 属性

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.

参见 yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.

public array $options = ['class' => 'table table-striped table-bordered detail-view']
$template public 属性

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.

public string|callable $template '<tr><th{captionOptions}>{label}</th><td{contentOptions}>{value}</td></tr>'

方法详情

init() public 方法

Initializes the detail view.

This method will initialize required property values.

public void init ( )
normalizeAttributes() protected 方法

Normalizes the attribute specifications.

protected void normalizeAttributes ( )
throws yii\base\InvalidConfigException
renderAttribute() protected 方法

Renders a single attribute.

protected string renderAttribute ( $attribute, $index )
$attribute array

The specification of the attribute to be rendered.

$index integer

The zero-based index of the attribute in the $attributes array

return string

The rendering result

run() public 方法

Renders the detail view.

This is the main entry of the whole detail view rendering.

public void run ( )