在Yii的下拉列表中为每个选项设置样式

时间:2015-11-23 06:03:32

标签: php yii html-select

我有一个下拉列表

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

每个客户都有数据库中的颜色。如何将该颜色赋予选项文本。请帮忙。

2 个答案:

答案 0 :(得分:0)

我认为您可以通过这种方式为每个值设置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'),
           ));

答案 1 :(得分:0)

请在您的模型和查看文件中进行以下更改:

查看文件

$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;
}

希望这对你有所帮助!如果有任何疑虑,请告诉我。