mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-03 13:58:55 +08:00
DetailView captionOptions & contentOptions attributes
This commit is contained in:
committed by
SilverFire - Dmitry Naumenko
parent
483e09dc4a
commit
76ced661b2
@ -26,13 +26,15 @@ DetailView использует свойство [[yii\widgets\DetailView::$attr
|
|||||||
echo DetailView::widget([
|
echo DetailView::widget([
|
||||||
'model' => $model,
|
'model' => $model,
|
||||||
'attributes' => [
|
'attributes' => [
|
||||||
'title', // title свойство (обычный текст)
|
'title', // title свойство (обычный текст)
|
||||||
'description:html', // description свойство, как HTML
|
'description:html', // description свойство, как HTML
|
||||||
[ // name свойство зависимой модели owner
|
[ // name свойство зависимой модели owner
|
||||||
'label' => 'Owner',
|
'label' => 'Owner',
|
||||||
'value' => $model->owner->name,
|
'value' => $model->owner->name,
|
||||||
|
'contentOptions' => ['class' => 'bg-red'], // настройка HTML атрибутов для тега, соответсвующего value
|
||||||
|
'captionOptions' => ['tooltip' => 'Tooltip'], // настройка HTML атрибутов для тега, соответсвующего label
|
||||||
],
|
],
|
||||||
'created_at:datetime', // дата создания в формате datetime
|
'created_at:datetime', // дата создания в формате datetime
|
||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
```
|
```
|
||||||
|
|||||||
@ -24,13 +24,15 @@ A typical usage of DetailView is as follows:
|
|||||||
echo DetailView::widget([
|
echo DetailView::widget([
|
||||||
'model' => $model,
|
'model' => $model,
|
||||||
'attributes' => [
|
'attributes' => [
|
||||||
'title', // title attribute (in plain text)
|
'title', // title attribute (in plain text)
|
||||||
'description:html', // description attribute formatted as HTML
|
'description:html', // description attribute formatted as HTML
|
||||||
[ // the owner name of the model
|
[ // the owner name of the model
|
||||||
'label' => 'Owner',
|
'label' => 'Owner',
|
||||||
'value' => $model->owner->name,
|
'value' => $model->owner->name,
|
||||||
|
'contentOptions' => ['class' => 'bg-red'], // to HTML customize attributes of value tag
|
||||||
|
'captionOptions' => ['tooltip' => 'Tooltip'], // to HTML customize attributes of label tag
|
||||||
],
|
],
|
||||||
'created_at:datetime', // creation date formatted as datetime
|
'created_at:datetime', // creation date formatted as datetime
|
||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
```
|
```
|
||||||
|
|||||||
@ -62,6 +62,10 @@ Yii Framework 2 Change Log
|
|||||||
- Enh #11804: Added `yii\behaviors\AttributeTypecastBehavior` for maintaining of strict ActiveRecord attribute types (klimov-paul)
|
- Enh #11804: Added `yii\behaviors\AttributeTypecastBehavior` for maintaining of strict ActiveRecord attribute types (klimov-paul)
|
||||||
- Enh #11835: Fixed NTEXT in `yii\db\mssql\QueryBuilder::$typeMap` that is being deprecated in MSSQL (githubjeka)
|
- Enh #11835: Fixed NTEXT in `yii\db\mssql\QueryBuilder::$typeMap` that is being deprecated in MSSQL (githubjeka)
|
||||||
- Enh #11835: Fixed `\yii\db\mssql\QueryBuilder::$typeMap[TYPE_TEXT]` - `NTEXT` data type was deprecated in MSSQL (githubjeka)
|
- Enh #11835: Fixed `\yii\db\mssql\QueryBuilder::$typeMap[TYPE_TEXT]` - `NTEXT` data type was deprecated in MSSQL (githubjeka)
|
||||||
|
- Enh #11835: Fixed NTEXT in `\yii\db\mssql\QueryBuilder::$typeMap` that is being deprecated in MSSQL (githubjeka)
|
||||||
|
- Enh #11844: Added the ability to customize html attributes of label and value tags in `\yii\widgets\DetailView` (githubjeka)
|
||||||
|
- Enh #11835: Fixed `\yii\db\mssql\QueryBuilder::$typeMap[TYPE_TEXT]` - `NTEXT` data type was deprecated in MSSQL (githubjeka)
|
||||||
|
- Enh #11844: Added the ability to customize HTML attributes of label and value tags in `\yii\widgets\DetailView` (githubjeka)
|
||||||
- Enh #11950: Improve `yii\helpers\BaseArrayHelper::keyExists()` speed (egorio)
|
- Enh #11950: Improve `yii\helpers\BaseArrayHelper::keyExists()` speed (egorio)
|
||||||
- Enh #11979: Added `yii\mutex\OracleMutex` which implements mutex "lock" mechanism via Oracle locks (zlakomanoff)
|
- Enh #11979: Added `yii\mutex\OracleMutex` which implements mutex "lock" mechanism via Oracle locks (zlakomanoff)
|
||||||
- Enh #12028: Add -h|--help option to console command to display help information (pana1990)
|
- Enh #12028: Add -h|--help option to console command to display help information (pana1990)
|
||||||
|
|||||||
@ -152,7 +152,9 @@ window.yii = (function ($) {
|
|||||||
var $form = $e.attr('data-form') ? $('#' + $e.attr('data-form')) : $e.closest('form'),
|
var $form = $e.attr('data-form') ? $('#' + $e.attr('data-form')) : $e.closest('form'),
|
||||||
method = !$e.data('method') && $form ? $form.attr('method') : $e.data('method'),
|
method = !$e.data('method') && $form ? $form.attr('method') : $e.data('method'),
|
||||||
action = $e.attr('href'),
|
action = $e.attr('href'),
|
||||||
params = $e.data('params'),
|
params = $e.data('params'),git checkout -b githubjeka-issue-11844-detail-view master
|
||||||
|
git pull https://github.com/githubjeka/yii2.git issue-11844-detail-view
|
||||||
|
|
||||||
pjax = $e.data('pjax'),
|
pjax = $e.data('pjax'),
|
||||||
pjaxPushState = !!$e.data('pjax-push-state'),
|
pjaxPushState = !!$e.data('pjax-push-state'),
|
||||||
pjaxReplaceState = !!$e.data('pjax-replace-state'),
|
pjaxReplaceState = !!$e.data('pjax-replace-state'),
|
||||||
|
|||||||
@ -75,6 +75,10 @@ class DetailView extends Widget
|
|||||||
* - format: the type of the value that determines how the value would be formatted into a displayable text.
|
* - format: the type of the value that determines how the value would be formatted into a displayable text.
|
||||||
* Please refer to [[Formatter]] for supported types.
|
* Please refer to [[Formatter]] for supported types.
|
||||||
* - visible: whether the attribute is visible. If set to `false`, the attribute will NOT be displayed.
|
* - 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 $attributes;
|
public $attributes;
|
||||||
/**
|
/**
|
||||||
@ -89,7 +93,7 @@ class DetailView extends Widget
|
|||||||
* where `$attribute` refer to the specification of the attribute being rendered, `$index` is the zero-based
|
* 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.
|
* index of the attribute in the [[attributes]] array, and `$widget` refers to this widget instance.
|
||||||
*/
|
*/
|
||||||
public $template = '<tr><th>{label}</th><td>{value}</td></tr>';
|
public $template = '<tr><th{captionOptions}>{label}</th><td{contentOptions}>{value}</td></tr>';
|
||||||
/**
|
/**
|
||||||
* @var array the HTML attributes for the container tag of this widget. The "tag" option specifies
|
* @var array 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.
|
* what container tag should be used. It defaults to "table" if not set.
|
||||||
@ -154,9 +158,13 @@ class DetailView extends Widget
|
|||||||
protected function renderAttribute($attribute, $index)
|
protected function renderAttribute($attribute, $index)
|
||||||
{
|
{
|
||||||
if (is_string($this->template)) {
|
if (is_string($this->template)) {
|
||||||
|
$captionOptions = Html::renderTagAttributes(ArrayHelper::getValue($attribute, 'captionOptions', []));
|
||||||
|
$contentOptions = Html::renderTagAttributes(ArrayHelper::getValue($attribute, 'contentOptions', []));
|
||||||
return strtr($this->template, [
|
return strtr($this->template, [
|
||||||
'{label}' => $attribute['label'],
|
'{label}' => $attribute['label'],
|
||||||
'{value}' => $this->formatter->format($attribute['value'], $attribute['format']),
|
'{value}' => $this->formatter->format($attribute['value'], $attribute['format']),
|
||||||
|
'{captionOptions}' => $captionOptions,
|
||||||
|
'{contentOptions}' => $contentOptions,
|
||||||
]);
|
]);
|
||||||
} else {
|
} else {
|
||||||
return call_user_func($this->template, $attribute, $index, $this);
|
return call_user_func($this->template, $attribute, $index, $this);
|
||||||
|
|||||||
@ -144,6 +144,30 @@ class DetailViewTest extends \yiiunit\TestCase
|
|||||||
|
|
||||||
$this->assertEquals($expectedValue, $this->detailView->attributes);
|
$this->assertEquals($expectedValue, $this->detailView->attributes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testOptionsTags()
|
||||||
|
{
|
||||||
|
$expectedValue = '<tr><th tooltip="Tooltip">Text</th><td class="bg-red">I`m an array</td></tr>';
|
||||||
|
|
||||||
|
$this->detailView = new PublicDetailView([
|
||||||
|
'model' => [
|
||||||
|
'text' => 'I`m an array',
|
||||||
|
],
|
||||||
|
'attributes' => [
|
||||||
|
[
|
||||||
|
'attribute' => 'text',
|
||||||
|
'label' => 'Text',
|
||||||
|
'contentOptions' => ['class' => 'bg-red'],
|
||||||
|
'captionOptions' => ['tooltip' => 'Tooltip'],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
foreach ($this->detailView->attributes as $index=>$attribute) {
|
||||||
|
$a = $this->detailView->renderAttribute($attribute, $index);
|
||||||
|
$this->assertEquals($expectedValue, $a);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -185,4 +209,4 @@ class PublicDetailView extends DetailView
|
|||||||
{
|
{
|
||||||
return parent::renderAttribute($attribute, $index);
|
return parent::renderAttribute($attribute, $index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user