Skip to content

Commit dd0d61d

Browse files
author
Eugene Tupikov
committed
Added allowEmptyList option
1 parent c4587f9 commit dd0d61d

File tree

7 files changed

+26
-3
lines changed

7 files changed

+26
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Widget support the following options that are additionally recognized over and a
4747
- `removeButtonOptions` *array*: the HTML options for `add` button. Can contains `class` and `label` keys
4848
- `data` *array*: array of values in case you use widget without model
4949
- `models` *array*: the list of models. Required in case you use `TabularInput` widget
50+
- `allowEmptyList` *boolean*: whether to allow the empty list
5051
- `columns` *array*: the row columns configuration where you can set the following properties:
5152
- `name` *string*: input name. *Required options*
5253
- `type` *string*: type of the input. If not set will default to `textInput`. Read more about the types described below

examples/views/multiple-input.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<?= $form->field($model, 'schedule')->widget(MultipleInput::className(), [
3434
'id' => 'schedule-wrapper',
3535
'limit' => 4,
36+
'allowEmptyList' => true,
3637
'columns' => [
3738
[
3839
'name' => 'user_id',

src/MultipleInput.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ class MultipleInput extends InputWidget
5454
*/
5555
public $addButtonOptions;
5656

57+
/**
58+
* @var bool whether to allow the empty list
59+
*/
60+
public $allowEmptyList = false;
61+
5762

5863
/**
5964
* Initialization.
@@ -115,6 +120,7 @@ private function createRenderer()
115120
'attributeOptions' => $this->attributeOptions,
116121
'data' => $this->data,
117122
'columnClass' => MultipleInputColumn::className(),
123+
'allowEmptyList' => $this->allowEmptyList,
118124
'context' => $this
119125
];
120126

src/TabularInput.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ class TabularInput extends Widget
4848
*/
4949
public $addButtonOptions;
5050

51+
/**
52+
* @var bool whether to allow the empty list
53+
*/
54+
public $allowEmptyList = false;
55+
5156
/**
5257
* @var Model[]|ActiveRecord[]
5358
*/
@@ -94,6 +99,7 @@ private function createRenderer()
9499
'attributeOptions' => $this->attributeOptions,
95100
'data' => $this->models,
96101
'columnClass' => TabularColumn::className(),
102+
'allowEmptyList' => $this->allowEmptyList,
97103
'context' => $this
98104
];
99105

src/assets/src/css/multiple-input.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ table.multiple-input-list tr > th {
3434
display: block;
3535
font-size: 13px;
3636
}
37-
.multiple-input-list__item .list-cell__button {
37+
.multiple-input-list .list-cell__button {
3838
width: 40px;
3939
}
4040
.multiple-input-list__item .radio,

src/components/BaseRenderer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,17 @@ abstract class BaseRenderer extends Object
6464
*/
6565
public $addButtonOptions;
6666

67+
/**
68+
* @var bool whether to allow the empty list
69+
*/
70+
public $allowEmptyList = false;
71+
6772
/**
6873
* @var string
6974
*/
7075
public $columnClass;
7176

77+
7278
/**
7379
* @var TabularInput|MultipleInput
7480
*/

src/renderers/TableRenderer.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public function renderHeader()
5656
$cells[] = $this->renderHeaderCell($column);
5757
}
5858
if (is_null($this->limit) || $this->limit > 1) {
59-
$cells[] = Html::tag('th', '', [
59+
$button = $this->allowEmptyList ? $this->renderAddButton() : '';
60+
$cells[] = Html::tag('th', $button, [
6061
'class' => 'list-cell__button'
6162
]);
6263
}
@@ -204,8 +205,10 @@ private function renderActionColumn($index = null)
204205
{
205206
if (is_null($index)) {
206207
$button = $this->renderRemoveButton();
208+
} elseif ($index == 0) {
209+
$button = $this->allowEmptyList ? $this->renderRemoveButton() : $this->renderAddButton();
207210
} else {
208-
$button = $index == 0 ? $this->renderAddButton() : $this->renderRemoveButton();
211+
$button = $this->renderRemoveButton();
209212
}
210213

211214
return Html::tag('td', $button, [

0 commit comments

Comments
 (0)