yii - 对 CGridView 的列使用 class 显示 php 警告

yii - use of class for CGridView's column shows php warning

我需要将 CSS class 应用于一个 CGridView 的列,但我收到 PHP 通知错误

这是代码

$this->widget('zii.widgets.grid.CGridView', array(
            'htmlOptions'=>array('class'=>'table table-striped table-bordered table-condensed'),
            'dataProvider'=>new CArrayDataProvider( getArray() ),
            'template'=>"{items}",
            'columns'=>array(
                array('name'=>'title', 'header'=>'Title', 'cssClassExpression'=>'span3'),
                array('name'=>'url', 'header'=>'url'),
            ),
        ));

这是我收到的通知:

PHP notice
Use of undefined constant span3 - assumed 'span3'

如果我禁用 PHP 通知,我会正确地看到 css class 应用于我的专栏。

有人知道为什么会这样吗?

谢谢

我使用 CGridView 列的不同语法解决了这个问题:

array('name'=>'title', 'header'=>'Title', 'htmlOptions'=>array('class'=>'span3')),

为了完整...

这个错误来自事实,即 cssClassExpression 值必须是 php callable,或者如果它是字符串,它是 eval'uated,根据 evaluateExpression 的来源:

    if(is_string($_expression_))
    {
        extract($_data_);
        return eval('return '.$_expression_.';');
    }
    else
    {
        $_data_[]=$this;
        return call_user_func_array($_expression_, $_data_);
    }

这可能用于有条件地设置 css class。对于 css class 的简单设置,请使用 htmlOptions,如 Marco 所发现的,并在答案中发布。