簡體   English   中英

Yii2-如何在動作更新中顯示depdrop子項中的選定值?

[英]Yii2 - How To show selected value in depdrop child on action Update?

我使用了來自Kartik的Extension來創建依賴的下拉菜單,稱為DepDrop。

我依賴的架構Regencies-> District-> Villages

創建操作時,一切正常,運行良好,但是當“更新操作”中的“子項下拉列表”中未顯示“選定值”。

這是我的查看代碼:

<?= $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;

}

ajax響應

在您的視圖中添加:'data'=> [$ key => $ value];

<?= $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);

希望對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM