Skip to content

Commit

Permalink
add init empty
Browse files Browse the repository at this point in the history
  • Loading branch information
sokrat committed Jul 24, 2015
1 parent a7806ef commit addf5cd
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions MultiInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class MultiInput extends InputWidget

public $rowView;

public $initEmpty = true;

public function init()
{
if (empty($this->form) || !($this->form instanceof ActiveForm)) {
Expand All @@ -28,7 +30,7 @@ public function run()
{
$this->registerClientScript();

if (count($this->model->{$this->attribute}) === 0) {
if (count($this->model->{$this->attribute}) === 0 && $this->initEmpty) {
$this->initEmpty();
}
return $this->renderRows();
Expand All @@ -47,19 +49,29 @@ protected function renderTemplateRow()
protected function renderRows()
{
$rows = [];
foreach((array)$this->model->{$this->attribute} as $index => $value) {
if (!count($this->model->{$this->attribute}) || !$this->initEmpty) {
$emptyAttribute = Html::activeHiddenInput($this->model, $this->attribute, [ 'value' => false]);
$rows[] = Html::tag('div', $emptyAttribute.$this->renderAddButton() , ['class' => 'row']);
}

foreach($this->model->{$this->attribute} as $index => $value) {
$rows[] = $this->renderRow($index, $value);
}

return Html::tag('div', implode(PHP_EOL, $rows), ['id' => $this->id, 'class' => 'multiply-input-rows']);
}

protected function renderAddButton()
{
return Html::tag('span', null, ['class' => 'glyphicon glyphicon-plus form-control-static col-lg-1 col-sm-1 add-row']);
}

protected function renderButton($index)
{
if ($index === 0) {
return Html::tag('span', null, ['class' => 'glyphicon glyphicon-plus form-control-static col-lg-1 col-sm-1 add-row']);
} else {
return Html::tag('span', null, ['class' => 'glyphicon glyphicon-trash form-control-static col-lg-1 col-sm-1 remove-row']);
if ($index === 0 && $this->initEmpty) {
return $this->renderAddButton();
}
return Html::tag('span', null, ['class' => 'glyphicon glyphicon-trash form-control-static col-lg-1 col-sm-1 remove-row']);
}

protected function renderRow($index, $value)
Expand Down

0 comments on commit addf5cd

Please sign in to comment.