Class yii\widgets\DetailView

Yii2 DetailView 组件详解
本文详细介绍了 Yii2 框架中的 DetailView 组件,包括其基本用途、属性配置及常见用法。通过示例展示了如何使用 $attributes 属性来定制数据展示方式,并解释了如何设置格式化程序来美化输出。

所有类 | 属性 | 方法

继承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更多细节和使用信息,请参阅指南文章数据窗口小部件。

公共属性

隐藏继承属性

属性类型描述定义在
$attributesarray详细的列表中显示的属性视图。yii\widgets\DetailView
$formatterarray|yii\i18n\Formatter格式化程序用于格式化模型属性值为可显示的文本。yii\widgets\DetailView
$modelarray|object细节是要显示的数据模型。yii\widgets\DetailView
$optionsarray这个小部件的HTML属性容器标记。yii\widgets\DetailView
$templatestring|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 attributeattribute: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 ( )
throwsyii\base\InvalidConfigException 

renderAttribute() protected 方法

Renders a single attribute.

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

属性的规范呈现。

$indexinteger

$attributes 的属性的从零开始的索引数组

returnstring

呈现的结果

run() public 方法

显示详细的视图

这是整个细节视图呈现的主要入口。

public void run ( )

转载于:https://my.oschina.net/u/2552765/blog/869814

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值