如何在没有小部件工厂的情况下在Yii2中设置GridView的默认配置?
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
有关更多信息:应用程序配置
更多相关文章
- 具有固定宽度网格的引导程序和应跨越窗口宽度的图像
- 使用javascript访问网格内的文本框
- Angularjs指令ng-hide在单元格模板中不对网格数据的更改进行绑定
- 如何在角度js中每5秒重新加载网格?
- python编程之一:使用网格索引算法进行空间数据查询