2016-11-20 142 views

回答

0

查看文件

<label><?= $model->getAttributeLabel('branch') ?></label> 
    <?php echo Select2::widget([ 
     'name' => 'branch', 
     'id' => 'branches', 
     'theme' =>Select2::THEME_BOOTSTRAP, 
     'value' => '', 
     'data' => $branchList, 
     'options' => [ 
      'placeholder' => Yii::t('app', 'Choose branch'), 
      'multiple' => true, 
     ], 
     'pluginOptions' => [ 
      'tags' => true, 
      'allowClear' => true, 
     ],]);?> 

    <label><?= $model->getAttributeLabel('Workers') ?></label> 
    <?php echo Select2::widget([ 
     'name' => 'worker', 
     'id' => 'workers', 
     'theme' =>Select2::THEME_BOOTSTRAP, 
     'value' => '', 
     'data' => [], 
     'options' => [ 
      'placeholder' => Yii::t('app', 'Choose workers'), 
      'multiple' => true, 
     ], 
     'pluginOptions' => [ 
      'tags' => true, 
      'allowClear' => true, 
     ],]); 
    ?> 

JS

$("#branches").change(function(){ 
     change();  
    }); 

    function change() {   
     var selectValue = $("#branches").val();    
     $("#workers").empty(); 

     $.post("'.Yii::$app->urlManager->createUrl('constructor/lists?id=').'"+selectValue, 
       function(data){ 
        $("#workers").html(data); 
       } 
      ); 

    }; 

ConstructorController

public function actionLists($id) 
    { 
     if ($id != null) { 
      $ids = explode(",", $id); 
      foreach ($ids as $id_branch) { 
       $workers = Report::getWorkers($id_branch); 
       if (count($workers) > 0) { 
        foreach ($workers as $worker) { 
         echo "<option value='" . $worker . "'>" . $worker . "</option>"; 
        } 
       } else { 
        echo "'<option>-</option>'"; 
       } 
      } 
     } 
    }