Added key mode to taglist form widget
Alternative implementation #2958 Fixes #2958
This commit is contained in:
parent
6ff6ab2996
commit
45f1714a03
|
|
@ -42,6 +42,11 @@ class TagList extends FormWidgetBase
|
|||
*/
|
||||
public $nameFrom = 'name';
|
||||
|
||||
/**
|
||||
* @var bool Use the key instead of value for saving and reading data.
|
||||
*/
|
||||
public $useKey = false;
|
||||
|
||||
//
|
||||
// Object properties
|
||||
//
|
||||
|
|
@ -62,6 +67,7 @@ class TagList extends FormWidgetBase
|
|||
'options',
|
||||
'mode',
|
||||
'nameFrom',
|
||||
'useKey',
|
||||
]);
|
||||
}
|
||||
|
||||
|
|
@ -71,6 +77,7 @@ class TagList extends FormWidgetBase
|
|||
public function render()
|
||||
{
|
||||
$this->prepareVars();
|
||||
|
||||
return $this->makePartial('taglist');
|
||||
}
|
||||
|
||||
|
|
@ -79,6 +86,7 @@ class TagList extends FormWidgetBase
|
|||
*/
|
||||
public function prepareVars()
|
||||
{
|
||||
$this->vars['useKey'] = $this->useKey;
|
||||
$this->vars['field'] = $this->formField;
|
||||
$this->vars['fieldOptions'] = $this->getFieldOptions();
|
||||
$this->vars['selectedValues'] = $this->getLoadValue();
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
$selectedValues = is_array($selectedValues) ? $selectedValues : [];
|
||||
$availableOptions = array_unique(array_merge($selectedValues, $fieldOptions));
|
||||
$availableOptions = $useKey ? $fieldOptions : array_unique(array_merge($selectedValues, $fieldOptions));
|
||||
?>
|
||||
<!-- Tag List -->
|
||||
<select
|
||||
|
|
@ -10,8 +10,12 @@
|
|||
<?php if ($customSeparators): ?>data-token-separators="<?= $customSeparators ?>"<?php endif ?>
|
||||
multiple
|
||||
<?= $field->getAttributes() ?>>
|
||||
<?php foreach ($availableOptions as $option): ?>
|
||||
<?php foreach ($availableOptions as $key => $option): ?>
|
||||
<?php if (!strlen($option)) continue ?>
|
||||
<option value="<?= e($option) ?>" <?= in_array($option, $selectedValues) ? 'selected="selected"' : '' ?>><?= e(trans($option)) ?></option>
|
||||
<?php if ($useKey): ?>
|
||||
<option value="<?= e($key) ?>" <?= in_array($key, $selectedValues) ? 'selected="selected"' : '' ?>><?= e(trans($option)) ?></option>
|
||||
<?php else: ?>
|
||||
<option value="<?= e($option) ?>" <?= in_array($option, $selectedValues) ? 'selected="selected"' : '' ?>><?= e(trans($option)) ?></option>
|
||||
<?php endif ?>
|
||||
<?php endforeach ?>
|
||||
</select>
|
||||
|
|
|
|||
Loading…
Reference in New Issue