2013-10-12 154 views
0

具有4下拉列表其工作像魅力除了當頁面被呈現在第一下拉列表是當我有從控制器傳遞這樣Yii的4 depedent下拉列表從控制器傳遞

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> 

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> 

我在哪裏弄錯了?

回答

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

       } 

?>

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> 
+0

特拉維斯嗨,你能更具體和詳細。我有幾個dropDown列表,這就是爲什麼我有#Spares_bodyid在那裏將模型更改或選擇時,將呈現bodystyles。 – user2208410

相關問題