Yii 1.0 联级菜单,子菜单的prompt默认项在update时的相关问题

    出现的问题是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]='&nbsp;&nbsp;&nbsp;&nbsp;'.$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时会将上方的&nbsp;&nbsp;&nbsp;&nbsp;来转义,所以不加会正确显示空格

            //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>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章