This is what a gridview looks like in Yii2:

这就是Yii2中gridview的样子:

<?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'], 

This works great, but...

这很棒,但......

I want this to be the default value for all my grids!

In Yii 1, this would look like this:

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

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

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

但是在Yii2中没有小部件工厂。相反,看看主配置,我希望这可以工作:

'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!

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

2 个解决方案

#1


17

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

您可以使用Yii :: $ container-> set()。

For example:

// 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',
    ],
]);

For more information: Dependency Injection Container and Practical Usage

有关更多信息:依赖注入容器和实际用法

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

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

For example (Since version 2.0.11):

例如(从2.0.11版开始):

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

For more information: Application Configurations

有关更多信息:应用程序配置

更多相关文章

  1. 具有固定宽度网格的引导程序和应跨越窗口宽度的图像
  2. 使用javascript访问网格内的文本框
  3. Angularjs指令ng-hide在单元格模板中不对网格数据的更改进行绑定
  4. 如何在角度js中每5秒重新加载网格?
  5. python编程之一:使用网格索引算法进行空间数据查询

随机推荐

  1. [Android]如何导入已有的外部数据库
  2. [置顶] 浅谈Android五大布局——L
  3. Android 启动系统相机,相册,裁剪图片及6.0
  4. Android App性能信息获取方法
  5. 物流货运移动APP解决方案
  6. Android开发重要参考资料
  7. Android调用百度地图Web端接口,实现百度定
  8. 解决Android Studio运行编译时间久的最有
  9. 直接拿来用!十大Material Design开源项目
  10. Android Studio获取数字签名(SHA1)的方法