如何将静态元素添加到yii dropDownList?

时间:2011-12-18 19:31:32

标签: yii

echo $form->dropDownList(
              $model,'categoryId', 
                      CHtml::listData(Category::model()->findAllBySql(
                      'SELECT * from category where isnull(parent_id)'), 
                      'id', 'name'), 
                      array(
                            'empty'=>Yii::t('fim','Search All'),
                             Yii::t('fim','Jobs'), 
                             Yii::t('fim','Training'), 
                             Yii::t('fim','Events'), 
                             Yii::t('fim','News')
                      )
            );

工作,培训,活动和新闻没有出现。

为了将这些值添加到选择框,我们应该/应该如何构建它?

谢谢

2 个答案:

答案 0 :(得分:13)

您无法使用$htmlOptions参数添加静态元素。我是这样做的:

$data = CHtml::listData(Category::model()->findAllBySql(
                        'SELECT * from category where isnull(parent_id)'), 
                        'id', 'name');
// Add extra options here: I am actually prepending with this syntax,
// but you are free to append or interleave instead. Array keys are the values.
$static = array(
    'jobs'     => Yii::t('fim','Jobs'), 
    'training' => Yii::t('fim','Training'), 
    'events'   => Yii::t('fim','Events'), 
    'news'     => Yii::t('fim','News'),
);

echo $form->dropDownList(
    $model,
    'categoryId',
    $static + $data,
    array('empty'=>Yii::t('fim','Search All')));

答案 1 :(得分:0)

对我而言,我所做的是添加了jquery代码,我附加了一个html选项

    $("#categoryId").append("<option value='0'>Additional Field</option>");

它不那么复杂,对我有用