如何使Yii CButtonColumn自定义按钮全局?

时间:2014-11-03 09:40:49

标签: php yii cgridview

我正在使用Yii的CGridView(实际上它是yiistrap的TbGridView),我创建了一个带有监听按钮的自定义CButtonColumn模板,一切正常,直到我发现自己每次需要表格列表的监听按钮时都会复制此代码。

array(
        'class'=>'bootstrap.widgets.TbButtonColumn',
        'template'=>'{listen}{delete}',
        'buttons'=>array(
            'listen'=>array(
                'label'=>'listen',
                'options' => array('class'=>'view headphones'),
                'icon' => 'icon-headphones',
                'url' => '#',
                'visible' => '$data->filename_32',
            ),
        ),
    ),

我能做些什么来使这段代码全局配置?如:

array(
    'template'=>'{listen}{delete}',
    'buttons'=>array(
        'listen' => 'xxxx.widgets.buttons.Listen',
    )
)

像这样。

1 个答案:

答案 0 :(得分:1)

你可以!在config / main.php中,添加:

'components' => array(
    'widgetFactory' => array(
        'widgets' => array(
            'bootstrap.widgets.TbButtonColumn' => array(
                'template'  => '{listen}{delete}',
                'buttons'   => array(
                    'listen' => 'xxxx.widgets.buttons.Listen',
                )
            ),
        )
    )
),

此方法主要用于预先配置内部窗口小部件,在这种情况下,您无法控制要加载的窗口小部件的确切类。在您的情况下,看起来您手动指定了widget类名,因此简单地覆盖TbButtonColumn小部件可能是一个更简单,更清晰的解决方案:

class MyTbButtonColumn extends TbButtonColumn {
    public $template = '{listen}{delete}';

    public $buttons = array(
        'listen' => 'xxxx.widgets.buttons.Listen',
    );
}

array(
    'class' => 'MyTbButtonColumn',
),