如何在没有小部件工厂的情况下为 Yii2 中的 GridView 设置默认配置? [英] How do I set a default configuration for GridView in Yii2 without the widget factory?

查看:20
本文介绍了如何在没有小部件工厂的情况下为 Yii2 中的 GridView 设置默认配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是 Yii2 中 gridview 的样子:

This is what a gridview looks like in Yii2:

<?php echo GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        ...
        ['class' => 'yii\grid\ActionColumn'],
    ],
]); ?>

我想稍微改变我的网格,所以我添加了这一行:

I want change my grids a little bit, so I add this line:

tableOptions'=>['class'=>'table table-condensed'], 

这很好用,但是...

在 Yii 1 中,这看起来像这样:

In Yii 1, this would look like this:

'widgetFactory' => array(
    'widgets' => array(
        'CGridView' => array(
            'htmlOptions' => ['class'=>'table table-condensed']
        ),
    ),
),

然而在 Yii2 中没有小部件工厂.相反,查看主配置,我希望它可以工作:

In Yii2 however there is no widget factory. Instead, looking at the main config, I would expect this to work:

'grid'=>[
    'class' => 'yii\grid\GridView',
    'tableOptions'=>['class'=>'table table-condensed'],
],

但事实并非如此.那么我做错了什么?任何提示非常感谢.谢谢!

But it does not. So what am I doing wrong? Any hints much appreciated. Thanks!

推荐答案

你可以使用 Yii::$container->set().

You can use Yii::$container->set().

例如:

// add following line in config/web.php and config/console.php
require __DIR__ . '/container.php';

// creates a config/container.php file and add following
\Yii::$container->set('yii\grid\GridView', [
    'tableOptions' => [
        'class' => 'table table-condensed',
    ],
]);

更多信息:依赖注入容器实际使用

和 Yii::$objectConfig 已在 Yii 2.0.0-beta 中删除.

and Yii::$objectConfig has been removed in Yii 2.0.0-beta.

例如(从 2.0.11 版开始):

For example (Since version 2.0.11):

$config = [
    'id' => 'basic',
    // ...
    'container' => [
        'definitions' => [
            yii\grid\GridView::class => [
                'tableOptions' => [
                    'class' => 'table table-condensed',
                ],
            ],
        ],
    ],
];

更多信息:应用配置

这篇关于如何在没有小部件工厂的情况下为 Yii2 中的 GridView 设置默认配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆