Repeater should populate with existing dataset
This commit is contained in:
parent
842252b556
commit
fe5deb68e7
|
|
@ -19,6 +19,16 @@ class Repeater extends FormWidgetBase
|
|||
*/
|
||||
public $form;
|
||||
|
||||
/**
|
||||
* @var string Prompt text for adding new items.
|
||||
*/
|
||||
public $prompt = 'Add new item';
|
||||
|
||||
/**
|
||||
* @var bool Items can be sorted.
|
||||
*/
|
||||
public $sortable = false;
|
||||
|
||||
//
|
||||
// Object properties
|
||||
//
|
||||
|
|
@ -39,6 +49,8 @@ class Repeater extends FormWidgetBase
|
|||
{
|
||||
$this->fillFromConfig([
|
||||
'form',
|
||||
'prompt',
|
||||
'sortable',
|
||||
]);
|
||||
|
||||
$this->processExistingItems();
|
||||
|
|
@ -59,6 +71,8 @@ class Repeater extends FormWidgetBase
|
|||
public function prepareVars()
|
||||
{
|
||||
$this->vars['indexName'] = self::INDEX_PREFIX.$this->formField->getName(false).'[]';
|
||||
$this->vars['prompt'] = $this->prompt;
|
||||
$this->vars['formWidgets'] = $this->formWidgets;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -75,7 +89,7 @@ class Repeater extends FormWidgetBase
|
|||
*/
|
||||
public function getSaveValue($value)
|
||||
{
|
||||
return array_values($value);
|
||||
return (array) $value;
|
||||
}
|
||||
|
||||
protected function processExistingItems()
|
||||
|
|
|
|||
|
|
@ -1,6 +1,12 @@
|
|||
<div class="field-repeater">
|
||||
|
||||
<div id="<?= $this->getId('items') ?>" class="field-repeater-items">
|
||||
<?php foreach ($this->formWidgets as $index => $widget): ?>
|
||||
<?= $this->makePartial('repeater_item', [
|
||||
'widget' => $widget,
|
||||
'indexValue' => $index
|
||||
]) ?>
|
||||
<?php endforeach ?>
|
||||
</div>
|
||||
|
||||
<div class="field-repeater-add-item loading-indicator-container indicator-center">
|
||||
|
|
@ -8,8 +14,9 @@
|
|||
href="javascript:;"
|
||||
data-load-indicator
|
||||
data-request="<?= $this->getEventHandler('onAddItem') ?>">
|
||||
Add new item
|
||||
<?= e(trans($prompt)) ?>
|
||||
</a>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue