Skip to content

Commit

Permalink
added select2 type
Browse files Browse the repository at this point in the history
  • Loading branch information
TonisOrmisson committed Mar 28, 2024
1 parent 2dccfb0 commit a8c511d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,36 @@ echo Html::textarea('my-credentials-input-field','', ['id' => 'my-credentials-in
```
![alt text](images/example-2.png)

### Example usage Select2

```
// ######################### example 3 select
$jsonData = '{"select2":3}';
$variables = [
'select2'=>[
'label' => Yii::t('app','A select 2 example'),
'type' => JsonForm::TYPE_SELECT2,
'select' => [
1 => "one",
2 => "two",
3 => "three"
],
'options' => ['placeholder' => 'Select a number ...'],
'pluginOptions' => [
'allowClear' => true
]
],
];
echo JsonForm::widget([
'id' => 'my-id-3',
'json' => $jsonData,
'jsonFieldId' => 'my-credentials-input-field-3',
'variables' => $variables,
]);
echo Html::textarea('my-credentials-input-field-3','', ['id' => 'my-credentials-input-field-3']);
```
![alt text](images/example-3.png)


Binary file added images/example-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions src/views/_field.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,21 @@
'name' => $id,
'value' => $value,
'type' => DatePicker::TYPE_COMPONENT_PREPEND,
'pluginOptions' => $options,
'pluginOptions' => $pluginOptions,
]); ?>
<?php elseif ($type == JsonForm::TYPE_DATETIME): ?>
<?= DateTimePicker::widget([
'id' => $id,
'name' => $id,
'value' => $value,
'type' => DateTimePicker::TYPE_COMPONENT_PREPEND,
'pluginOptions' => $options,
'pluginOptions' => $pluginOptions,
]); ?>
<?php elseif ($type == JsonForm::TYPE_SELECT2): ?>
<?= \kartik\select2\Select2::widget([
'id' => $id,
'name' => $id,
'value' => $value,
'data' => $select,
'options' => $options,
'pluginOptions' => $pluginOptions
Expand Down
22 changes: 15 additions & 7 deletions src/views/json-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,28 @@ function setOptionsValues(id,jsonFieldId,optionsArray) {
if (!$widget->isKeyed) {
$label = "";
}

$options['id'] = $id;
//$options['name'] = $widget->fieldName;

if (isset($variable['options'])) {
$options = array_merge($options, $variable['options']);
$select = $variable['select'] ?? [];
$pluginOptions = $variable['pluginOptions'] ?? [];
$options = $variable['options'] ?? [];


if(empty($pluginOptions)) {
$pluginOptions['id'] = $id;
// old config
if (isset($variable['options'])) {
$pluginOptions = array_merge($pluginOptions, $variable['options']);
}
$pluginOptions['class'] = "form-control values";
}


$value = (isset($currentData[$id]) ? $currentData[$id] : null);
$type = (isset($variable['type']) ? $variable['type'] : null);

if (!$widget->isKeyed) {
$id = $widget->nonKeyedId($id);
}

$options['class'] = "form-control values";
?>

<?= $this->render('_field', [
Expand All @@ -126,6 +132,8 @@ function setOptionsValues(id,jsonFieldId,optionsArray) {
'label' => $label,
'value' => $value,
'options' => $options,
'select' => $select,
'pluginOptions' => $pluginOptions,
]); ?>

<?php endforeach; ?>
Expand Down

0 comments on commit a8c511d

Please sign in to comment.