1

I have a question related to Yii DropDownList. How can we give some attribute values to each option. For Example I need to build a dropdownlist looks like below.

<select>
    <option value="a" myAttribute="xyz">Calendar</option>
    <option value="n" myAttribute="PQR">Shopping Cart</option>
    <option value="c" myAttribute="ABC">CD</option>
    <option value="d" myAttribute="HMN">Email</option>   
</select>

values and myAttributes's value are coming from table. How can I implement this by altering the following?

<?php echo $form->dropDownList($model,'media_ids',Media::getAllmedia(),array('?'=>'?')); ?>

in model Media I have function like this

public function getAllmedia() {    
        $condition = 'isActive =  "Y"';    
        $model = Media::model()->findAll(array('condition' => $condition));    
        return CHtml::listData($model, 'id', 'media_title');    
    }

EDIT: My Requirement is to show images along with each option, for that i have to give another attribute to each option which holds the image path

4

4 回答 4

3

CHtml::dropDownList() 不允许这样做..尝试创建一个像这样的其他函数:

媒体类文件:

    static function getAllmediaDDL($idSelected=null) {
                  $condition = 'isActive =  "Y"';
                  $model = Media::model()->findAll(array('condition' => $condition));

                  $combo = CHtml::tag('select', array('id' => 'mySelect'));
                  foreach ($model as $value){
                      $selected = $value->id == $idSelected? true : false;
                      $combo .= CHtml::tag('option', array('value' => $value->id, 'myattribute'=>$value->url_img, 'selected'=>$selected), CHtml::encode($value->media_title), true);
                  }

                  $combo .= CHtml::tag('select');
                  return $combo;

   }

并在视图中调用:

echo Media::getAllmediaDDL($model->id); // If you want to call in this way, you need to declare it as static function.
于 2013-08-27T05:16:47.563 回答
1

您在listOption details中得到了答案,并且您必须像对 options value 所做的那样实现另一个函数来映射每个 id,该 id 是具有图像路径的值: array(id=>array(image_path_attribute=>value), another_id=> (array(),...) 例如,以免假设函数名称是listImagePath()返回一个选项数组,但不要忘记每个值都映射到属性数组而不仅仅是一个

<?php echo $form->dropDownList($model,'media_ids',Media::getAllmedia(),array('options'=>Media::listImagePath())); ?>
于 2013-08-28T09:37:31.300 回答
1

这是工作:

<?php 
     echo $form->dropDownList($model,'media_ids',
               'data' => CHtml::listData(Media::model()->findAll(), 'id', 'name'),
                array(
                     'options' =>  CHtml::listData(Media::model()->findAll(), 'id', 'option_array')
                     )
     ); 
?>

在模型中:

    public function getOption_array() {
        return array('myAttribute' => $this->media_title);
    }
于 2015-10-05T16:36:10.300 回答
-2

对不起,我仍然是初学者,但这就是你要找的吗?

在你的_form中:

  <?php echo $form->dropDownListRow($model, 'status',array(0=>"Inactive",1=>"Active")
    ); ?>
于 2013-08-28T09:52:38.867 回答