From 76ced661b22864ff931af3ee2a7fcac1ce9324a6 Mon Sep 17 00:00:00 2001 From: githubjeka Date: Tue, 4 Oct 2016 16:53:05 +0300 Subject: [PATCH] DetailView captionOptions & contentOptions attributes --- docs/guide-ru/output-data-widgets.md | 12 +++++----- docs/guide/output-data-widgets.md | 12 +++++----- framework/CHANGELOG.md | 4 ++++ framework/assets/yii.js | 4 +++- framework/widgets/DetailView.php | 10 ++++++++- tests/framework/widgets/DetailViewTest.php | 26 +++++++++++++++++++++- 6 files changed, 55 insertions(+), 13 deletions(-) diff --git a/docs/guide-ru/output-data-widgets.md b/docs/guide-ru/output-data-widgets.md index 10e81c5ff1a..ff64a8b730f 100644 --- a/docs/guide-ru/output-data-widgets.md +++ b/docs/guide-ru/output-data-widgets.md @@ -26,13 +26,15 @@ DetailView использует свойство [[yii\widgets\DetailView::$attr echo DetailView::widget([ 'model' => $model, 'attributes' => [ - 'title', // title свойство (обычный текст) - 'description:html', // description свойство, как HTML - [ // name свойство зависимой модели owner + 'title', // title свойство (обычный текст) + 'description:html', // description свойство, как HTML + [ // name свойство зависимой модели 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 ], ]); ``` diff --git a/docs/guide/output-data-widgets.md b/docs/guide/output-data-widgets.md index 0589af74fdb..81e721b61ab 100644 --- a/docs/guide/output-data-widgets.md +++ b/docs/guide/output-data-widgets.md @@ -24,13 +24,15 @@ A typical usage of DetailView is as follows: echo DetailView::widget([ 'model' => $model, 'attributes' => [ - 'title', // title attribute (in plain text) - 'description:html', // description attribute formatted as HTML - [ // the owner name of the model + 'title', // title attribute (in plain text) + 'description:html', // description attribute formatted as HTML + [ // the owner name of the model '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 ], ]); ``` diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index e988cdfde73..733312a8a32 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -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 #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 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 #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) diff --git a/framework/assets/yii.js b/framework/assets/yii.js index b3f3be9bee9..403c73b04b1 100644 --- a/framework/assets/yii.js +++ b/framework/assets/yii.js @@ -152,7 +152,9 @@ window.yii = (function ($) { var $form = $e.attr('data-form') ? $('#' + $e.attr('data-form')) : $e.closest('form'), method = !$e.data('method') && $form ? $form.attr('method') : $e.data('method'), 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'), pjaxPushState = !!$e.data('pjax-push-state'), pjaxReplaceState = !!$e.data('pjax-replace-state'), diff --git a/framework/widgets/DetailView.php b/framework/widgets/DetailView.php index f9e423e30e9..55a8069579e 100644 --- a/framework/widgets/DetailView.php +++ b/framework/widgets/DetailView.php @@ -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. * Please refer to [[Formatter]] for supported types. * - 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; /** @@ -89,7 +93,7 @@ class DetailView extends 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. */ - public $template = '{label}{value}'; + public $template = '{label}{value}'; /** * @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. @@ -154,9 +158,13 @@ public function run() protected function renderAttribute($attribute, $index) { if (is_string($this->template)) { + $captionOptions = Html::renderTagAttributes(ArrayHelper::getValue($attribute, 'captionOptions', [])); + $contentOptions = Html::renderTagAttributes(ArrayHelper::getValue($attribute, 'contentOptions', [])); return strtr($this->template, [ '{label}' => $attribute['label'], '{value}' => $this->formatter->format($attribute['value'], $attribute['format']), + '{captionOptions}' => $captionOptions, + '{contentOptions}' => $contentOptions, ]); } else { return call_user_func($this->template, $attribute, $index, $this); diff --git a/tests/framework/widgets/DetailViewTest.php b/tests/framework/widgets/DetailViewTest.php index 6d8ede5efe4..8c298faf901 100644 --- a/tests/framework/widgets/DetailViewTest.php +++ b/tests/framework/widgets/DetailViewTest.php @@ -144,6 +144,30 @@ public function testArrayModel() $this->assertEquals($expectedValue, $this->detailView->attributes); } + + public function testOptionsTags() + { + $expectedValue = 'TextI`m an array'; + + $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 @@ public function renderAttribute($attribute, $index) { return parent::renderAttribute($attribute, $index); } -} \ No newline at end of file +}