Skip to content

Commit b483b48

Browse files
author
Eugene Tupikov
committed
Added option enableGuessTitle for MultipleInput
1 parent cfe18cb commit b483b48

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ Yii2 multiple input change log
44
1.2.3 in development
55
--------------------
66

7-
- Enh #34: Added options `allowEmptyList` (unclead)
7+
- Enh #34: Added option `allowEmptyList` (unclead)
8+
- Enh #35: Added option `enableGuessTitle` for MultipleInput (unclead)
89

910
1.2.2
1011
-----

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,22 @@ In some cases you need to have the ability to delete all rows in the list. For t
280280

281281
```
282282

283+
### Guess column title
284+
285+
Sometimes you can use the widget without defining columns but you want to have the column header of the table.
286+
In this case you can use `enableGuessColumn` option like in the example below:
287+
288+
```php
289+
290+
echo $form->field($model, 'emails')->widget(MultipleInput::className(), [
291+
'limit' => 5,
292+
'allowEmptyList' => true,
293+
'enableGuessTitle' => true
294+
])
295+
->label(false);
296+
297+
```
298+
283299
## JavaScript events
284300
This widget has following events:
285301
- `afterInit`: triggered after initialization

examples/views/multiple-input.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
<?php
2626
echo $form->field($model, 'emails')->widget(MultipleInput::className(), [
2727
'limit' => 5,
28-
'allowEmptyList' => true
28+
'allowEmptyList' => true,
29+
'enableGuessTitle' => true
2930
])
3031
->label(false);
3132
?>

src/MultipleInput.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ class MultipleInput extends InputWidget
5959
*/
6060
public $allowEmptyList = false;
6161

62+
/**
63+
* @var bool whether to guess column title in case if there is no definition of columns
64+
*/
65+
public $enableGuessTitle = false;
66+
6267

6368
/**
6469
* Initialization.
@@ -91,12 +96,15 @@ protected function initData()
9196
protected function guessColumns()
9297
{
9398
if (empty($this->columns) && $this->hasModel()) {
94-
$this->columns = [
95-
[
96-
'name' => $this->attribute,
97-
'type' => MultipleInputColumn::TYPE_TEXT_INPUT
98-
]
99+
$column = [
100+
'name' => $this->attribute,
101+
'type' => MultipleInputColumn::TYPE_TEXT_INPUT
99102
];
103+
104+
if ($this->enableGuessTitle && $this->hasModel()) {
105+
$column['title'] = $this->model->getAttributeLabel($this->attribute);
106+
}
107+
$this->columns[] = $column;
100108
}
101109
}
102110

0 commit comments

Comments
 (0)