O
O
Orbite2017-07-26 06:26:26
Yii
Orbite, 2017-07-26 06:26:26

How to make linked lists yii2?

Knowledgeable people tell me what I'm doing wrong? I just started learning yii2, I needed to make linked lists - choosing a region and cities, I do this (code example below), when choosing a region, the city field is not filled, it just becomes empty.
View:

<? $form = ActiveForm::begin(); ?>


  <?php
    
    echo $form->field($form_model, 'reg')->dropDownList(
      ArrayHelper::map(Region::find()->all(), 'reg_id', 'reg_name'),
      [
        'onchange' => '
          $.post(
            "' . Url::toRoute('chusovoi') . '", 
            {id: $(this).val()}, 
            function(data){
              $("#SelectCit").html(data); 
            }
          );
        ',
     
      ]
    );
    
    echo $form->field($form_model, 'cit')->dropDownList(ArrayHelper::map(City::find()->where(['cit_reg_id' => $id])->asArray()->all(), 'cit_id', 'cit_name'),
      [
        'prompt' => 'Выберите город',
        'id' => 'SelectCit'
      ]
    );
  
  ?>	
      
 	<?php ActiveForm::end(); ?>

Controller:
if ($id = Yii::$app->request->post('id')) {
        $operationPosts = City::find()
          ->where(['cit_reg_id' => $id])
          ->count();
        if ($operationPosts > 0) {
          $operations = City::find()
            ->where(['cit_reg_id' => $id])
            ->all();
          foreach ($operations as $operation){
            echo "<option value='" . $operation->cit_id . "'>" . $operation->cit_name . "</option>";
          }
          
        } else
          echo "<option>-</option>";

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ilya Karavaev, 2017-07-26
@Quieteroks

Instead: It would be better if you loaded the list into JSON and using the API of some Select2 created the necessary list.

M
Maxim Lagoysky, 2017-07-26
@lagoy

Yii2 dependent select what is wrong?
I asked a similar question yesterday, but I myself already found an error, everything is working there

A
Andrey Sanych, 2017-07-26
@mountpoint

DepDrop

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question