Yii 4 dependentdent下拉列表,其中包含从控制器传递的预选值

时间:2013-10-12 09:48:20

标签: drop-down-menu yii

有4个下拉列表,它们像魅力一样工作,除非我预先选择从控制器传递的值

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
                  )
    );
}

呈现页面时,第一个下拉列表显示预选值。我已经使用下面的代码测试了第二个下拉列表,但它没有触发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>

Ajax的HMTL代码未按要求触发。

 <div class="row" id="carmodels">
         <div class="width125"><label for="Spares_modelid" class="required">Model <span class="required">*</span></label></div>
         <div class="widthtxt"><select name="Spares[modelid]" id="Spares_modelid">
<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>
</select>                   </div>
    </div>

我在哪里弄错了?

2 个答案:

答案 0 :(得分:0)

看起来您可能混淆了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>   

答案 1 :(得分:0)

我已经设法使用(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'));

              }

&GT;