A
A
Alexey2019-12-04 12:02:21
JavaScript
Alexey, 2019-12-04 12:02:21

How to pass data in Yii2 when select2 is selected to dependent select2 using AJAX?

Good afternoon everyone.
Colleagues tell me where in the code I'm doing something wrong.
There is a form, on the form yes Select2 from Kartik . Selects are dependent, in one brand of cars in another model. By default, the dependent select is blocked from being selected until the value of the first one is selected.

The problem is how to insert data into the second select2 ?

Actually a piece of form code with a widget:

<?= $form->field($model, 'auto')->widget(Select2::classname(), [
        'data' => $model->getMarksList(),
        'language' => 'ru',
        'options' => ['placeholder' => 'Выберите марку авто ...'],
        'pluginOptions' => [
            'allowClear' => false,

        ],
        'pluginEvents' => [
            "select2:select" => "function() {
                var json = {\"id\":1,\"name\":\"alex\"};
                console.log(json)
                $('#orderform-modelcar').prop('disabled', false);
                $('#orderform-modelcar').select2({data: json}); 
            }"
        ],
        
    ])->label(null, ['class' => 'label']) ?>

    <?= $form->field($model, 'modelcar')->widget(Select2::classname(), [
        'data' => null,
        'language' => 'ru',
        'options' => ['placeholder' => 'Выберите модель авто ...', 'disabled' => true],
        'pluginOptions' => [
            'allowClear' => false,
        ],        
    ])->label(null, ['class' => 'label']) ?>

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