出現的問題是create時由於需要選擇,所以prompt會換掉,但是在update時,如果不選擇,會自動寫入prompt中的值。
解決方法:一種方法是將array('prompt'=>'請先選擇語言')去掉,但是這樣顯示不直觀,update時默認什麼都不顯示。另一種方式就是下方紅字,直接判斷是否是update,如果是update,直接將本身的"value"=>"name",放在array中,更新時重新寫入一次。雖然是多了數據寫入,但是在顯示上更直觀。
Views中:
<div class="row">
<?php echo $form->labelEx($model,'lan_type'); ?>
<?php echo $form->dropDownList($model,'lan_type',Language::model()->getLanguageList(),
array(
'prompt' => '選擇語言',
'ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('/admin/productstype/gettype'),
'update'=>'#ProductsType_type_pid',
))
); ?>
<?php echo $form->error($model,'lan_type'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'type_pid'); ?>
<?php if($model->isNewRecord)://判斷是否是新記錄?>
<?php //echo CHtml::dropDownList('type_pid','', array(), array('prompt'=>'請先選擇語言'));//教程中是這樣寫,但是這樣無法寫入數據,因爲沒和$model關聯 ?>
<?php echo $form->dropDownList($model,'type_pid',array(),array('prompt'=>'請先選擇語言')); ?>
<?php else:?>
<?php echo $form->dropDownList($model,'type_pid',array($model->type_pid=>ProductsType::model()->getProductsname($model->type_pid))); //create時是array()會根據上級菜單改變,update時將array()改成array('xx'=>'yy'),同時不要加dropDownList的第四個參數了,也就是不要prompt什麼的,這樣會在菜單上默認顯示“yy”,如此能在子菜單直觀顯示出當前是什麼,當更新時相當於重新把本身的數值又往數據庫寫了一次。?>
<?php endif;?>
<?php echo $form->error($model,'type_pid'); ?>
</div>
Controller中:
public function actionGettype()
{
$sproductstypes=ProductsType::model()->findAll('type_level=1 and status_id=1 and lan_type=:lan_type',array(':lan_type'=>(int)$_POST['ProductsType']['lan_type']));
$type=array();
$typeList=array();
foreach ($sproductstypes as $stype){
$data=ProductsType::model()->getItemsByType($stype);
foreach($data as $dataItem){
$type[$dataItem->id]=' '.$dataItem->type_name;
}
$typeList=$typeList+array($stype->id=>$stype->type_name)+$type;
}
echo CHtml::tag('option',array('value'=>0), '頂級目錄',true);
foreach($typeList as $value=>$name){
echo CHtml::tag('option',array('value'=>$value), $name,true);
//CHtml::encode時會將上方的 來轉義,所以不加會正確顯示空格
//echo CHtml::tag('option',array('value'=>$value), CHtml::encode($name),true);
}
}
-----------------------------------------------------------------------------------------------------
以上還是有問題的,修改時只顯示當前的,而不是列表。要選擇列表,需要先選英文再切換到中文才會顯示。下面是更好的解決方法:
在Controller/actionView($id)中,獲取 $languagetype=$model->lan_type;語言類型,將其傳值到update視圖中:$this->render('update',array(
'model'=>$model,
'languagetype'=>$languagetype,
));
在views/update中同樣傳值<?php $this->renderPartial('_form', array('model'=>$model,'languagetype'=>$languagetype)); ?>這樣在_form.php中:
<div class="row">
<?php echo $form->labelEx($model,'products_type'); ?>
<?php if($model->isNewRecord):?>
<?php echo $form->dropDownList($model,'products_type',array(),array('prompt'=>'請先選擇語言')); ?>
<?php else:?>
<?php echo $form->dropDownList($model,'products_type',ProductsType::model()->getProductsTypeListEn($languagetype)); ?>//getProductsTypeListEn方法在ProductsType中定義,根據$languagetype不同來列出不同的內容。和普通菜單基本相同。
<?php endif;?>
<?php echo $form->error($model,'trends_type'); ?>
</div>