Skip to content

Commit 957710d

Browse files
committed
added extra buttons
1 parent a5a9fcc commit 957710d

File tree

4 files changed

+69
-6
lines changed

4 files changed

+69
-6
lines changed

src/MultipleInput.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,20 @@ class MultipleInput extends InputWidget
146146
*/
147147
public $cloneButton = false;
148148

149+
/**
150+
* @var string|\Closure the HTML content that will be rendered after the buttons.
151+
*
152+
* ```php
153+
* function ($model, $index, $context)
154+
* ```
155+
*
156+
* - `$model`: the current data model being rendered
157+
* - `$index`: the zero-based index of the data model in the model array
158+
* - `$context`: the MultipleInput widget object
159+
*
160+
*/
161+
public $extraButtons;
162+
149163
/**
150164
* Initialization.
151165
*
@@ -258,6 +272,7 @@ private function createRenderer()
258272
'sortable' => $this->sortable,
259273
'enableError' => $this->enableError,
260274
'cloneButton' => $this->cloneButton,
275+
'extraButtons' => $this->extraButtons,
261276
];
262277

263278
if ($this->removeButtonOptions !== null) {

src/renderers/BaseRenderer.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use yii\helpers\Html;
1414
use yii\helpers\Json;
1515
use yii\base\InvalidConfigException;
16+
use yii\base\InvalidParamException;
1617
use yii\base\Model;
1718
use yii\base\NotSupportedException;
1819
use yii\base\BaseObject;
@@ -142,6 +143,20 @@ abstract class BaseRenderer extends BaseObject implements RendererInterface
142143
*/
143144
public $cloneButton = false;
144145

146+
/**
147+
* @var string|\Closure the HTML content that will be rendered after the buttons.
148+
*
149+
* ```php
150+
* function ($model, $index, $context)
151+
* ```
152+
*
153+
* - `$model`: the current data model being rendered
154+
* - `$index`: the zero-based index of the data model in the model array
155+
* - `$context`: the MultipleInput widget object
156+
*
157+
*/
158+
public $extraButtons;
159+
145160
/**
146161
* @inheritdoc
147162
*/
@@ -275,6 +290,33 @@ protected function initColumns()
275290
}
276291
}
277292

293+
/**
294+
* Render extra content in action column.
295+
*
296+
* @param $index
297+
* @param $item
298+
*
299+
* @return string
300+
*/
301+
protected function getExtraButtons($index, $item)
302+
{
303+
if (!$this->extraButtons) {
304+
return '';
305+
}
306+
307+
if (is_callable($this->extraButtons)) {
308+
$content = call_user_func($this->extraButtons, $item, $index, $this->context);
309+
} else {
310+
$content = $this->extraButtons;
311+
}
312+
313+
if (!is_string($content)) {
314+
throw new InvalidParamException('Property "extraButtons" must return string.');
315+
}
316+
317+
return $content;
318+
}
319+
278320
public function render()
279321
{
280322
$this->initColumns();

src/renderers/ListRenderer.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,15 @@ public function renderCellContent($column, $index)
228228
* Renders the action column.
229229
*
230230
* @param null|int $index
231+
* @param null|ActiveRecordInterface|array $item
231232
* @return string
232233
* @throws \Exception
233234
*/
234-
private function renderActionColumn($index = null)
235+
private function renderActionColumn($index = null, $item = null)
235236
{
236-
return Html::tag('td', $this->getActionButton($index), [
237+
$content = $this->getActionButton($index) . $this->getExtraButtons($index, $item);
238+
239+
return Html::tag('td', $content, [
237240
'class' => 'list-cell__button',
238241
]);
239242
}

src/renderers/TableRenderer.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ private function renderRowContent($index = null, $item = null)
188188
$hiddenInputs = [];
189189
$isLastRow = $this->max === $this->min;
190190
if (!$isLastRow && $this->isAddButtonPositionRowBegin()) {
191-
$cells[] = $this->renderActionColumn($index, true);
191+
$cells[] = $this->renderActionColumn($index, $item, true);
192192
}
193193

194194
foreach ($this->columns as $column) {
@@ -205,7 +205,7 @@ private function renderRowContent($index = null, $item = null)
205205
}
206206

207207
if (!$isLastRow) {
208-
$cells[] = $this->renderActionColumn($index);
208+
$cells[] = $this->renderActionColumn($index, $item);
209209
}
210210

211211
if ($hiddenInputs) {
@@ -293,12 +293,15 @@ public function renderCellContent($column, $index)
293293
* Renders the action column.
294294
*
295295
* @param null|int $index
296+
* @param null|ActiveRecordInterface|array $item
296297
* @param bool $isFirstColumn
297298
* @return string
298299
*/
299-
private function renderActionColumn($index = null, $isFirstColumn = false)
300+
private function renderActionColumn($index = null, $item = null, $isFirstColumn = false)
300301
{
301-
return Html::tag('td', $this->getActionButton($index, $isFirstColumn), [
302+
$content = $this->getActionButton($index, $isFirstColumn) . $this->getExtraButtons($index, $item);
303+
304+
return Html::tag('td', $content, [
302305
'class' => 'list-cell__button',
303306
]);
304307
}

0 commit comments

Comments
 (0)