使gridview的actioncolumn的图标不再换行
解决了一个很小的大问题,很好
先交待,参考资料在这里:prevent-gridview-actioncolumn-icons-from-wrapping-globally
1、先建一个文件container.php,内容如下:
<?php
\Yii::$container->set('yii\grid\ActionColumn', [
// 'contentOptions' => ['width' => '80px;'],
'contentOptions' => ['style' => ['white-space' => 'nowrap']],
]);
说明:第一句是直接定义宽度为80px,但默认好像就是这80px,应该是没用。
第二句是说不换行,这个应该可以,所以我用的是这个。
2、再把container.php添加到项目中去
require_once(__DIR__ . '/container.php');
资料原文复制在这里:
You should create file “container.php” in config folder. And put this code
<?php
\Yii::$container->set('yii\grid\ActionColumn', [
'contentOptions' => ['width' => '80px;'],
]);
Then, You should include this file in your “web/index.php”. Like that
<?php
// comment out the following two lines when deployed to production
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
$config = require(__DIR__ . '/../config/web.php');
require(__DIR__ . '/../config/container.php');
(new yii\web\Application($config))->run();
Building on top of @vitalik_74’s answer, I added the nowrap style for that column instead of the width.
\Yii::$container->set('yii\grid\ActionColumn', [
'contentOptions' => ['style' => ['white-space' => 'nowrap']],
]);
刚发现这里还有一个回答,内容一样,但更简洁。
You can use 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
这个连接的地址在这里: how-do-i-set-a-default-configuration-for-gridview-in-yii2-without-the-widget-fac
涉及到的YII2文档在这里
http://www.yiiframework.com/doc-2.0/guide-concept-di-container.html#practical-usage