Yii中下拉列表中的每个选项的样式


Styling each option in dropdown in Yii

我有一个下拉

    $Customer=Customer::getDecisionMakingCompany();
    $div_topic.= CHtml::dropDownList('picks_customer_id',"picks_customer_id",$Customer,array('multiple'=>true));

列表来自型号

public static function getDecisionMakingCompany() {
    $condition = 'status=  "Y" AND type=1';
    $model = self::model()->findAll(array('condition' => $condition,'order'=>'customer_name'));
    return CHtml::listData($model, 'id', 'customer_name');
}

每个客户在数据库中都有一种颜色。我怎样才能给选项文本赋予这种颜色。请帮忙。

请在模型和视图文件中进行以下更改:

查看文件

$Customer = Customer::getDecisionMakingCompany();
echo CHtml::tag('select', array('id' => 'picks_customer_id', 'name' => 'picks_customer_id[]', 'multiple' => true));
if (!empty($Customer)) {
        foreach ($Customer as $index => $cust) {        
                echo CHtml::tag('option', array('value' => $cust->id, 'style' => 'color:' . $cust->color), $CHtml::encode($cust->customer_name), true);
        }
}

模型文件

public static function getDecisionMakingCompany() {
    $condition = 'status=""Y"" AND type=1';
    $model = self::model()->findAll(array('condition' => $condition, 'order'=>'customer_name'));
    return $model;
}

希望这能帮助你!如果对此有任何担忧,请告诉我。

我认为您可以通过以下方式为每个值设置htmlOption:

$Customer=Customer::getDecisionMakingCompany();
$div_topic.= CHtml::dropDownList('picks_customer_id',"picks_customer_id",
      $Customer,array('multiple'=>true, 
          array( 'value1'=>array('style'=>'color: yourColor1', ),   
                 'value2'=>array('style'=>'color: yourColor2'),
           ));