Skip to content

Commit

Permalink
DetailView captionOptions & contentOptions attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
githubjeka authored and SilverFire committed Oct 5, 2016
1 parent 483e09d commit 76ced66
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 13 deletions.
12 changes: 7 additions & 5 deletions docs/guide-ru/output-data-widgets.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
],
]);
```
Expand Down
12 changes: 7 additions & 5 deletions docs/guide/output-data-widgets.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
],
]);
```
Expand Down
4 changes: 4 additions & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion framework/assets/yii.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
10 changes: 9 additions & 1 deletion framework/widgets/DetailView.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
/**
Expand All @@ -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 = '<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
* what container tag should be used. It defaults to "table" if not set.
Expand Down Expand Up @@ -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);
Expand Down
26 changes: 25 additions & 1 deletion tests/framework/widgets/DetailViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,30 @@ public function testArrayModel()

$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);
}
}
}

/**
Expand Down Expand Up @@ -185,4 +209,4 @@ public function renderAttribute($attribute, $index)
{
return parent::renderAttribute($attribute, $index);
}
}
}

0 comments on commit 76ced66

Please sign in to comment.