Skip to content

Commit 14dd721

Browse files
committed
Added ability to render add button at several positions
1 parent e87cede commit 14dd721

File tree

6 files changed

+32
-11
lines changed

6 files changed

+32
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Yii2 multiple input change log
77
- #79 Added support for embedded MultipleInput widget (unclead, execut)
88
- Enh: Added ability to render `add` button in the footer (unclead)
99
- Enh: Improving for better work without ActiveForm (unclead)
10+
- Enh: Added ability to render `add` button at several positions (unclead)
1011

1112
1.2.19
1213
------

examples/views/multiple-input.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@
3535
]
3636
],
3737
'min' => 2, // should be at least 2 rows
38-
'addButtonPosition' => MultipleInput::POS_HEADER // show add button in the header
38+
'addButtonPosition' => [
39+
MultipleInput::POS_HEADER,
40+
MultipleInput::POS_FOOTER,
41+
MultipleInput::POS_ROW
42+
]
3943
])
4044
->label(false);
4145
?>

src/MultipleInput.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class MultipleInput extends InputWidget
7474
public $min;
7575

7676
/**
77-
* @var string position of add button. By default button is rendered in the row.
77+
* @var string|array position of add button. By default button is rendered in the row.
7878
*/
7979
public $addButtonPosition = self::POS_ROW;
8080

src/TabularInput.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class TabularInput extends Widget
6868
public $models;
6969

7070
/**
71-
* @var string position of add button. By default button is rendered in the row.
71+
* @var string|array position of add button. By default button is rendered in the row.
7272
*/
7373
public $addButtonPosition = self::POS_ROW;
7474

src/components/BaseRenderer.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,8 @@ public function init()
135135
$this->prepareMinOption();
136136
$this->prepareLimit();
137137
$this->prepareColumnClass();
138-
$this->prepareButtonsOptions();
139-
140-
$this->uniqueHash = uniqid();
141-
$this->indexPlaceholder = 'multiple-index-' . $this->uniqueHash;
138+
$this->prepareButtons();
139+
$this->prepareIndexPlaceholder();
142140
}
143141

144142
private function prepareColumnClass()
@@ -190,8 +188,12 @@ private function prepareLimit()
190188
}
191189
}
192190

193-
private function prepareButtonsOptions()
191+
private function prepareButtons()
194192
{
193+
if (!is_array($this->addButtonPosition)) {
194+
$this->addButtonPosition = (array) $this->addButtonPosition;
195+
}
196+
195197
if (!array_key_exists('class', $this->removeButtonOptions)) {
196198
$this->removeButtonOptions['class'] = 'btn btn-danger';
197199
}
@@ -316,14 +318,28 @@ public function getUniqueHash()
316318
*/
317319
protected function isAddButtonPositionHeader()
318320
{
319-
return $this->addButtonPosition === self::POS_HEADER;
321+
return in_array(self::POS_HEADER, $this->addButtonPosition);
320322
}
321323

322324
/**
323325
* @return bool
324326
*/
325327
protected function isAddButtonPositionFooter()
326328
{
327-
return $this->addButtonPosition === self::POS_FOOTER;
329+
return in_array(self::POS_FOOTER, $this->addButtonPosition);
330+
}
331+
332+
/**
333+
* @return bool
334+
*/
335+
protected function isAddButtonPositionRow()
336+
{
337+
return in_array(self::POS_ROW, $this->addButtonPosition);
338+
}
339+
340+
private function prepareIndexPlaceholder()
341+
{
342+
$this->uniqueHash = uniqid();
343+
$this->indexPlaceholder = 'multiple-index-' . $this->uniqueHash;
328344
}
329345
}

src/renderers/TableRenderer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ private function getActionButton($index)
295295
if ($index < $this->min) {
296296
return '';
297297
} elseif ($index === $this->min) {
298-
return $this->addButtonPosition === self::POS_ROW ? $this->renderAddButton() : '';
298+
return $this->isAddButtonPositionRow() ? $this->renderAddButton() : '';
299299
} else {
300300
return $this->renderRemoveButton();
301301
}

0 commit comments

Comments
 (0)