2017-02-13 95 views
0

我已經使用來自Kartik的擴展,用於創建依賴下拉菜單,稱爲DepDrop。Yii2 - 如何在操作中顯示選定的值更新?

我的依賴模式Regencies->區 - >村

當創建行動,一切都還好吧,運行水井,但是當更新行動,在兒童下拉不顯示選定值。

這裏我查看代碼:

<?= $form->field($model, 'fk_regencies_id')->dropDownList(ArrayHelper::map(Regencies::find()->all(),'id','name'), ['id'=>'regency_id']);?> 

          <?= Html::hiddenInput($model->fk_districs_id, $model->fk_districs_id, ['id'=>$model->fk_districs_id]) ?> 

          <?= $form->field($model, 'fk_districs_id')->widget(DepDrop::classname(), [ 

           'options'=>['id'=>'district-id'], 
           'pluginOptions'=>[ 
            'depends'=>['regency_id'], 
            'placeholder'=>'Select...', 
            'url'=>Url::to(['/calonpegawai/district']), 
            'params'=>[$model->fk_districs_id] 
           ] 
          ]) ?> 

,在這裏我的控制器:

public function actionDistrict() { 
    $out = []; 
if (isset($_POST['depdrop_parents'])) { 
    $parents = $_POST['depdrop_parents']; 
    if ($parents != null) { 
     $regency_id = $parents[0]; 
     $param1 = null; 

     if (!empty($_POST['depdrop_params'])) { 
      $params = $_POST['depdrop_params']; 
      $param1 = $params[0]; // get the value of input-type-1 

     } 

     $out = Districts::getDistrictList($regency_id); 
     //$out[1] = ['id'=>$regency_id, 'name'=>$param1]; 
     $selected = Districts::getDefaultDistrict($param1); 
     //$selected[1] = ['id'=>$regency_id, 'name'=>$param1]; 
     // the getDefaultSubCat function will query the database 
     // and return the default sub cat for the cat_id 

     echo Json::encode(['output'=>$out, 'selected'=>$selected]); 
     return; 
    } 
} 
echo Json::encode(['output'=>'', 'selected'=>'']); 
} 

我的模型:

public function getDistrictList($regency_id) 
{ 
    $data=\backend\models\Districts::find() 
     ->where(['regency_id'=>$regency_id]) 
     ->select(['id','name' ])->asArray()->all(); 

    return $data; 

} 
public function getDefaultDistrict($param1) 
{ 
    $data=\backend\models\Districts::find() 
     ->where(['id'=>$param1]) 
     ->select(['id','name' ])->asArray()->all(); 

    return $data; 

} 

ajaxresponse

+0

你可以發佈檢查員的瀏覽器請求日誌嗎? – meysam

+0

@meysam我已添加圖片,請回復ajax –

+0

這是Yii2調試面板,請發送瀏覽器控制檯的日誌。在Firefox中按Ctrl + Shift + K,在Chrome中按F12顯示控制檯。 – meysam

回答

1

添加在您的看法:「數據「 => [$鍵=> $值]。

<?= $form->field($model, 'fk_regencies_id')->dropDownList(ArrayHelper::map(Regencies::find()->all(),'id','name'), ['id'=>'regency_id']);?> 

         <?= Html::hiddenInput($model->fk_districs_id, $model->fk_districs_id, ['id'=>$model->fk_districs_id]) ?> 

         <?= $form->field($model, 'fk_districs_id')->widget(DepDrop::classname(), [ 
          'data' => [$model->fk_regencies_id=>$model->fk_regencies_id], 
          'options'=>['id'=>'district-id'], 

          'pluginOptions'=>[ 
           'depends'=>['regency_id'], 
           'placeholder'=>'Select...', 
           'url'=>Url::to(['/calonpegawai/district']), 
           'params'=>[$model->fk_districs_id] 
          ] 
         ]) ?> 

在視圖中添加JavaScript:

$script = <<< JS 
    $("#regency_id").change(function(){ 
     $("#district-id").depdrop({ 
     depends: ['regency_id'], 
     url: '/calonpegawai/district' 
    }); 
    }).change(); 

JS; $ this-> registerJs($ script);

希望能幫到你。

相關問題