Update drop down option list on change of another field


(Pawan Nagar) #1

I have two dropdown fields in the form. for example, I have car manufacture name in one dropdown and another drop down we have all the cars according to the car manufacturer and both the fields are multiple select options.

code

Company

echo $form->field($model, 'company')->widget(Select2::classname(), [
                                'data' => $company,
                                'options' => ['placeholder' => 'Select Clinical signs Category'],
                                'pluginOptions' => [
                                    'allowClear' => true,
                                    'multiple' => true
                                ],
                                'pluginEvents' => [
                                    'change' => 'function() 
                                        { 
                                            var data_id = $(this).val();
                                            setOnSelectData("signs_symptoms-cars", data_id,"' . $url . '");
                                       // above function update the dropdown option in cars-dropdown

                                        }',
                                ],
                            ])->label(false);


cars:

echo $form->field($model, 'cars')->label(false)->widget(Select2::classname(), [
                                'data' => $cars,
                                'options' => ['placeholder' => 'Cars Name'],
                                'pluginOptions' => [
                                    'allowClear' => true,
                                    'multiple' => true,
                                ],
                                'pluginEvents' => [
                                    'change' => 'function() 
                                                                                { 
                                              var data_id = $(this).val();
                                            console.log(data_id);
                                          
                                           }',
                                ],
                            ]);

Currently when I change the company name cars drop down clear all its value.But i want cars option update without clear the previous selected value