Skip to content

Commit 5b4d1c2

Browse files
author
Eugene Tupikov
committed
Add rowOptions property + prepare to release
1 parent 1151705 commit 5b4d1c2

File tree

8 files changed

+97
-6
lines changed

8 files changed

+97
-6
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
Yii2 multiple input change log
22
==============================
33

4-
1.2.9 in development
4+
1.2.10 in development
55
--------------------
66

7+
1.2.9
8+
-----
9+
10+
- Enh #56: add `rowOptions` property
11+
712
1.2.8
813
-----
914

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Yii2 widget for handle multiple inputs for an attribute of model
88
[![License](https://poser.pugx.org/unclead/yii2-multiple-input/license)](https://packagist.org/packages/unclead/yii2-multiple-input)
99

1010
##Latest release
11-
The latest version of the extension is v1.2.8. Follow the [instruction](./UPGRADE.md) for upgrading from previous versions
11+
The latest version of the extension is v1.2.9. Follow the [instruction](./UPGRADE.md) for upgrading from previous versions
1212

1313
Contents:
1414

@@ -63,6 +63,18 @@ Widget support the following options that are additionally recognized over and a
6363

6464
**columns** *array*: the row columns configuration where you can set the properties which is described below
6565

66+
**rowOptions** *array|\Closure*: the HTML attributes for the table body rows. This can be either an array
67+
specifying the common HTML attributes for all body rows, or an anonymous function that returns an array of the HTML attributes.
68+
It should have the following signature:
69+
70+
```php
71+
function ($model, $index, $context)
72+
```
73+
74+
- `$model`: the current data model being rendered
75+
- `$index`: the zero-based index of the data model in the model array
76+
- `$context`: the widget object
77+
6678
### Column options
6779

6880
**name** *string*: input name. *Required options*

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"yii2 tabular input"
1010
],
1111
"type": "yii2-extension",
12-
"version": "1.2.8",
12+
"version": "1.2.9",
1313
"license": "BSD-3-Clause",
1414
"support": {
1515
"issues": "https://github.com/unclead/yii2-multiple-input/issues?state=open",

examples/views/multiple-input.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@
3737
<?= $form->field($model, 'schedule')->widget(MultipleInput::className(), [
3838
'limit' => 4,
3939
'allowEmptyList' => true,
40+
'rowOptions' => function($model) {
41+
$options = [];
42+
43+
if ($model['priority'] > 1) {
44+
$options['class'] = 'danger';
45+
}
46+
return $options;
47+
},
4048
'columns' => [
4149
[
4250
'name' => 'user_id',

src/MultipleInput.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,21 @@ class MultipleInput extends InputWidget
7777
*/
7878
public $addButtonPosition = self::POS_ROW;
7979

80+
/**
81+
* @var array|\Closure the HTML attributes for the table body rows. This can be either an array
82+
* specifying the common HTML attributes for all body rows, or an anonymous function that
83+
* returns an array of the HTML attributes. It should have the following signature:
84+
*
85+
* ```php
86+
* function ($model, $index, $context)
87+
* ```
88+
*
89+
* - `$model`: the current data model being rendered
90+
* - `$index`: the zero-based index of the data model in the model array
91+
* - `$context`: the MultipleInput widget object
92+
*
93+
*/
94+
public $rowOptions = [];
8095

8196
/**
8297
* Initialization.
@@ -144,6 +159,7 @@ private function createRenderer()
144159
'allowEmptyList' => $this->allowEmptyList,
145160
'min' => $this->min,
146161
'addButtonPosition' => $this->addButtonPosition,
162+
'rowOptions' => $this->rowOptions,
147163
'context' => $this
148164
];
149165

src/TabularInput.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,21 @@ class TabularInput extends Widget
7171
*/
7272
public $addButtonPosition = self::POS_ROW;
7373

74+
/**
75+
* @var array|\Closure the HTML attributes for the table body rows. This can be either an array
76+
* specifying the common HTML attributes for all body rows, or an anonymous function that
77+
* returns an array of the HTML attributes. It should have the following signature:
78+
*
79+
* ```php
80+
* function ($model, $index, $context)
81+
* ```
82+
*
83+
* - `$model`: the current data model being rendered
84+
* - `$index`: the zero-based index of the data model in the model array
85+
* - `$context`: the TabularInput widget object
86+
*
87+
*/
88+
public $rowOptions = [];
7489

7590
/**
7691
* Initialization.
@@ -114,6 +129,7 @@ private function createRenderer()
114129
'columnClass' => TabularColumn::className(),
115130
'allowEmptyList' => $this->allowEmptyList,
116131
'min' => $this->min,
132+
'rowOptions' => $this->rowOptions,
117133
'addButtonPosition' => $this->addButtonPosition,
118134
'context' => $this
119135
];

src/components/BaseRenderer.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,22 @@ abstract class BaseRenderer extends Object
7878
*/
7979
public $allowEmptyList = false;
8080

81+
/**
82+
* @var array|\Closure the HTML attributes for the table body rows. This can be either an array
83+
* specifying the common HTML attributes for all body rows, or an anonymous function that
84+
* returns an array of the HTML attributes. It should have the following signature:
85+
*
86+
* ```php
87+
* function ($model, $index, $context)
88+
* ```
89+
*
90+
* - `$model`: the current data model being rendered
91+
* - `$index`: the zero-based index of the data model in the model array
92+
* - `$context`: the widget object
93+
*
94+
*/
95+
public $rowOptions = [];
96+
8197
/**
8298
* @var string
8399
*/

src/renderers/TableRenderer.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,31 @@ private function renderRowContent($index = null, $item = null)
151151
$cells[0] = preg_replace('/^(<td[^>]+>)(.*)(<\/td>)$/s', '${1}' . $hiddenInputs . '$2$3', $cells[0]);
152152
}
153153

154-
$content = Html::tag('tr', implode("\n", $cells), [
155-
'class' => 'multiple-input-list__item',
156-
]);
154+
155+
$content = Html::tag('tr', implode("\n", $cells), $this->prepareRowOptions($index, $item));
157156

158157
return $content;
159158
}
160159

160+
/**
161+
* Prepares the row options.
162+
*
163+
* @param int $index
164+
* @param ActiveRecord|array $item
165+
* @return array
166+
*/
167+
protected function prepareRowOptions($index, $item)
168+
{
169+
if (is_callable($this->rowOptions)) {
170+
$options =call_user_func($this->rowOptions, $item, $index, $this->context);
171+
} else {
172+
$options = $this->rowOptions;
173+
}
174+
175+
Html::addCssClass($options, 'multiple-input-list__item');
176+
return $options;
177+
}
178+
161179
/**
162180
* Renders the cell content.
163181
*

0 commit comments

Comments
 (0)