yii 在页面更新/编辑时,如何根据父下拉列表值显示子下拉列表作为默认值

rqqzpn5f  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(137)

我在Yii2中使用Kartik DepDrop。我有一个父下拉列表和一个子下拉列表。当它创建时,它工作得很好。但是在更新表单时,父下拉列表的值被正确选择。但是在子下拉列表中,基于父下拉列表的正确下拉列表没有显示。

表单.php为:

<?php  echo $form->field($model, 'type')->widget(Select2::classname())->dropDownList(\backend\models\Ratingsbe::getType(), ['id'=>'type-id']); ?>

<?php
  echo $form->field($model, 'relatedid')->widget(DepDrop::classname(), [
       'options'=>['id'=>'relatedid-id'],
        'type' => 2, //type select2
        'pluginOptions'=>[
        'depends'=>['type-id'],
         'placeholder'=>'Select...',
         'url'=>Url::to(['/ratings/finddata'])
          ],
          ]);
  ?>

控制器.php是:

public function actionFinddata()
    {
        $pagegroup = ['Centresbe','Spotinfobe','Sportsbe','Coachesbe','Clinicsbe','Holidaysbe','Eventsbe'];
        $category = '';
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            if (isset($_POST['depdrop_parents'])) {
                $parents = $_POST['depdrop_parents'];
                if ($parents != null) {
                    $type = $parents[0];
                    $dynamic_class = '\\backend\\models\\'.$type;
                    if(in_array($type, $pagegroup))
                    {
                          $catres = $dynamic_class::find()->where(['status' => 'Active'] )->one();
                            $category = $catres->category;
                    }

                     $data = $dynamic_class::getdatatype($category);

                    return ['output'=>$data, 'selected'=>''];
                }
            }
            return ['output'=>'', 'selected'=>''];
    }

图像输出为:


相关ID下拉列表应该根据编辑页面上选择的酒店显示。因为编辑页面时默认选择酒店。

yb3bgrhw

yb3bgrhw1#

Yii论坛中的thread以两种不同的方式回答您的问题- JavaScript和PHP。

相关问题