Drop down menu Yii 4 Dependent dropdownlist,带有从控制器传递的预选值

Drop down menu Yii 4 Dependent dropdownlist,带有从控制器传递的预选值,drop-down-menu,yii,Drop Down Menu,Yii,有4个dropdownlist,它们像charm一样工作,除非我像这样从控制器传递了预选值 public function actionBodyTrim(){ $trim = new CarTrim; $spares = new Spares; $spares->makeid = $_REQUEST['mid']; $spares->modelid = $_REQUEST['moid']; $spares->bodyid = $_R

有4个dropdownlist,它们像charm一样工作,除非我像这样从控制器传递了预选值

public function actionBodyTrim(){
    $trim = new CarTrim;
    $spares = new Spares;

    $spares->makeid  = $_REQUEST['mid'];
    $spares->modelid = $_REQUEST['moid'];
    $spares->bodyid  = $_REQUEST['bid'];
    $spares->trimid  = $_REQUEST['tid'];

    $this->render('bodytrim', array(
                  'trim'=>$trim,
                  'spares'=>$spares
                  )
    );
}
呈现页面时,第一个下拉列表显示预选值。我已经用下面的代码测试了第二个dropdownlist,但它没有触发Ajax调用。结果如下所示

<div class="span5 leftpull">
    <div class="row">
     <div class="width125"><?php echo $form->labelEx($model,'makeid'); ?></div>
     <div class="widthtxt"><?php echo $form->dropDownList($model,'makeid', CHtml::listData(Makes::model()->findAll(array('order' => 'makename ASC')), 'makeid', 'makename'),
                                     array('prompt'=>'Select Makes',
                                           'ajax'=>array('url'=>CController::createUrl('CarModels'),
                                            'beforeSend' => 'function(){$("#carmodels").addClass("loading");}',
                                            'complete' => 'function(){$("#carmodels").removeClass("loading");}',
                                           'type' =>'POST',
                                           'update'=>'#'. CHtml::activeId($model, 'modelid'),
                                           array('class'=>'ajaxlink'),

                                        ))); ?>


                <?php echo $form->error($model,'makeid'); ?>
      </div>
</div>

<div class="row" id="carmodels">
     <div class="width125"><?php echo $form->labelEx($model,'modelid'); ?></div>
     <div class="widthtxt"><?php echo $form->dropDownList($model, 'modelid', empty($model->modelid) ? array('prompt' => 'Select the country first') :
                                      array('ajax'=>array('type'=>'POST',
                                                          'url'=>CController::createUrl('BodyType'),
                                                          'update'=>'#'. CHtml::activeId($model, 'bodyid'),
                                                          'prompt'=>'Models',
                                                          array('class'=>'ajaxlink'),

                                               ))); 
        ?>
        <?php echo $form->error($model,'modelid'); ?>
    </div>
</div>


看起来您可能混淆了
bodyid
表单字段中的一些参数。如果查看生成的HTML,您的ajax请求将被错误地呈现

<optgroup label="ajax">
<option value="type">POST</option>
<option value="url">/spareparts/index.php?r=spares/parts/BodyType</option>
<option value="update">#Spares_bodyid</option>
<option value="prompt">Models</option>
<optgroup label="0">
<option value="class">ajaxlink</option>
</optgroup>
</optgroup>   

邮递
/spareparts/index.php?r=备件/零件/车身类型
#备件/车身ID
模型
ajaxlink

我使用(if语句)成功地使它在脏工作循环中工作。我只是希望有人有一个比这更干净的代码

 <?php
             if (isset($model->makeid) && !isset($model->modelid)  ) 
             {              
                            $list = CHtml::listData(CarModels::model()->findAll('makeid=:mid',
                                                                                                                array(':mid'=>$model->makeid)), 'modelid', 'model');
                            echo $form->dropDownList($model,'modelid', $list, 
                                                                          array( 'prompt'=>'Select Models',
                                                                                             'ajax'=>array('type'=>'POST',
                                                                                                                      'url'=>CController::createUrl('BodyType'),
                                                                                                                      'update'=>'#'. CHtml::activeId($model, 'bodyid'),

                                             ))); 

             }

             else (isset($model->makeid) && isset($model->modelid) ){
                      $list = CHtml::listData(CarModels::model()->findAll('makeid=:mid  AND  modelid=:mod',
                                                                                                                array(':mid'=>$model->makeid, ':mod'=>$model->modelid )), 'modelid', 'model');

                    echo $form->dropDownList($model,'modelid', $list, array('prompt'=>'Select Models'));

              }

嗨,特拉维斯,你能说得更具体、更详细些吗。我有几个下拉列表,这就是为什么我有#备件#bodyid在那里,它将在模型更改或选择时呈现车身样式。
 <?php
             if (isset($model->makeid) && !isset($model->modelid)  ) 
             {              
                            $list = CHtml::listData(CarModels::model()->findAll('makeid=:mid',
                                                                                                                array(':mid'=>$model->makeid)), 'modelid', 'model');
                            echo $form->dropDownList($model,'modelid', $list, 
                                                                          array( 'prompt'=>'Select Models',
                                                                                             'ajax'=>array('type'=>'POST',
                                                                                                                      'url'=>CController::createUrl('BodyType'),
                                                                                                                      'update'=>'#'. CHtml::activeId($model, 'bodyid'),

                                             ))); 

             }

             else (isset($model->makeid) && isset($model->modelid) ){
                      $list = CHtml::listData(CarModels::model()->findAll('makeid=:mid  AND  modelid=:mod',
                                                                                                                array(':mid'=>$model->makeid, ':mod'=>$model->modelid )), 'modelid', 'model');

                    echo $form->dropDownList($model,'modelid', $list, array('prompt'=>'Select Models'));

              }