继承 | yii\widgets\DetailView » yii\base\Widget » yii\base\Component » yii\base\Object |
---|---|
可用自版本 | 2.0 |
DetailView显示单一数据$model.的细节。
DetailView最好是用来显示模型在一个常规格式(例如每个模型属性显示为表中的一行)。模型可以是一个实例,yii\base\Model或关联数组。
DetailView使用$attributes属性决定了哪些模型属性应该和他们应该如何格式化显示。
DetailView 典型用法如下所示︰
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
],
]);
在DetailView更多细节和使用信息,请参阅指南文章数据窗口小部件。
公共属性
属性 | 类型 | 描述 | 定义在 |
---|---|---|---|
$attributes | array | 详细的列表中显示的属性视图。 | yii\widgets\DetailView |
$formatter | array|yii\i18n\Formatter | 格式化程序用于格式化模型属性值为可显示的文本。 | yii\widgets\DetailView |
$model | array|object | 细节是要显示的数据模型。 | yii\widgets\DetailView |
$options | array | 这个小部件的HTML属性容器标记。 | yii\widgets\DetailView |
$template | string|callable | 模板用于呈现单一属性。 | yii\widgets\DetailView |
公共方法
方法 | 描述 | 定义在 |
---|---|---|
init() | 初始化细节视图。 | yii\widgets\DetailView |
run() | 显示详细的视图。 | yii\widgets\DetailView |
受保护的方法
方法 | 描述 | 定义在 |
---|---|---|
normalizeAttributes() | 规范化属性的规范 | yii\widgets\DetailView |
renderAttribute() | 呈现一个属性。 | yii\widgets\DetailView |
属性详情
$attributes public property
详细的列表中显示的属性视图。每个数组元素代表显示一个特定属性的规范。
An attribute can be specified as a string in the format of attribute
, attribute:format
or attribute:format:label
, whereattribute
refers to the attribute name, and format
represents the format of the attribute. The format
is passed to theyii\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.
一个属性可以指定为字符串格式的属性,属性:格式或属性:格式:标签,whereattribute指的是属性名称,格式表示的格式属性。的格式传递给theyii\i18n\Formatter::format()方法来将一个属性值格式化为一个可显示的文本。请参阅 yii\i18n\Formatter 程序所支持的类型。格式和标签都是可选的。如果没有他们将默认值。
还可以指定一个属性的数组使用以下元素:
attribute
: 属性名称。这是必需的,如果没有指定label
或value
。label
: t他与属性相关的标签。如果不指定这个值,它将生成的属性名称。-
value
: 要显示的值。如果不指定这个值,它将检索使用的属性名称从$model callingyii\helpers\ArrayHelper::getValue().。注意,这个值将被格式化为一个可显示的文本根据格式选项。自版本)可以被定义为闭包使用以下参数: -
function ($model, $widget)
$model
是指显示模型和$widget
是DetailView部件的实 -
format
: 值的类型决定了价值如何被格式化为一个可显示的文本。请参阅toyii\i18n\Formatter 程序支持的类型。 visible
: 属性是否可见。如果设置为false,则属性将不会显示。contentOptions
: 定制的HTML属性值标签。例如:['class' => 'bg-red']
.。请参阅toyii\helpers\BaseHtml::renderTagAttributes() 支持语法。captionOptions
: HTML属性自定义标签标记。例如:['class' => 'bg-red']
。请参阅toii\helpers\BaseHtml::renderTagAttributes()支持语法。
public array $attributes = null
$formatter public property
格式化程序用于格式化模型属性值为可显示的文本。这可以是一个实例,yii\i18n\Formatter程序或配置数组创建 yii\i18n\Formatter 程序实例。如果不设置这个属性,格式化程序将使用应用程序组件。
public array|yii\i18n\Formatter $formatter = null
$model public property
.细节是要显示的数据模型。这可能是一个yii \ \模型实例,一个关联数组,一个对象实现yii\base\Arrayable基础接口或只是一个对象定义公共访问非静态属性。
public array|object $model = null
$options public property
这个小部件的HTML属性容器标记。标签选项指定应该使用什么容器标签。如果没有设置默认totable。
参见 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 property
模板用于呈现单一属性。如果一个字符串,令牌{lable}和{ value }将替换标签和对应的属性的值。如果回调(例如一个匿名函数),签名必须如下:
function ($attribute, $index, $widget)
属性参考规范的$attribute
被呈现,美元指数是从零开始的 $index
属性的$attributes数组,和$widget
是指这个小部件实例。
版本2.0.10以来,令牌{ captionOptions }和{ contentOptions }可用,这将代表HTML容器元素的HTML属性标签和值。
public string|callable $template = '<tr><th{captionOptions}>{label}</th><td{contentOptions}>{value}</td></tr>'
方法详情
init() public 方法
初始化细节视图。
该方法将初始化所需的属性值。
public void init ( ) |
normalizeAttributes() protected 方法
规范化属性的规范。
protected void normalizeAttributes ( ) | ||
throws | yii\base\InvalidConfigException |
---|
renderAttribute() protected 方法
Renders a single attribute.
protected string renderAttribute ( $attribute, $index ) | ||
$attribute | array | 属性的规范呈现。 |
$index | integer | $attributes 的属性的从零开始的索引数组 |
return | string | :呈现的结果 |
---|
run() public 方法
显示详细的视图
这是整个细节视图呈现的主要入口。
public void run ( ) |