在 CGridView 的列中使用类会显示 PHP 警告


use of class for CGridView's column shows php warning

我需要将CSS类应用于一个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 类。

有谁知道为什么会这样?

谢谢

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

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

为了完整起见...

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

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

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