A
A
Alexander Verbitsky2021-07-26 02:42:36
Yii
Alexander Verbitsky, 2021-07-26 02:42:36

How to update information in select2 (kartik) when the list is opened, and not after entering in the search field?

There is a select2-kartik drop-down list whose values ​​are loaded using ajax. On the right side, I screwed the "+" button, with which, in the modal window, you can add a new value to the database.
60fdf57e75116031330881.png

The problem is that after adding a new position to the database, for some reason it is not shown in the list when it is expanded. But if you start typing something in the search field, then ajax updates it... The

question is, how to make it so that the update occurs when the list is opened, and not when text is entered in the search field?

my code:

<?= $form->field($ts_model, 'ts_voltage_class_id')->widget(
    Select2::classname(), [
        'options' => [
            'class' => 'form-control',
            'placeholder' => 'Выбрать...'
        ],
        'hideSearch' => true,
        'pluginOptions' => [
            'allowClear' => true,
            'selectOnClose' => true,
            'language'           => [
                'errorLoading' => new JsExpression("function () { return 'Произошла ошибка, перезагрузите страницу...'; }"),
            ],
            'ajax'               => [
                'url'      => \yii\helpers\Url::to(['transformer-substations/search-ts-voltage-class']),
                'dataType' => 'json'
            ],
        ],
        'addon' => [
            'append' => [
                'content' => Html::button('<i class="fa fa-plus"></i>', [
                    'class' => 'btn btn-default',
                    'title' => 'Добавить модель',
                    'data-toggle' => 'tooltip',
                    'id' => 'create-model',
                ]),
                'asButton' => true
            ]
        ],
    ]);
?>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question