【问题标题】:Yii2: GridView's column is too wideYii2:GridView的列太宽
【发布时间】:2017-09-28 11:59:01
【问题描述】:

我有 index.php 由创建 CRUD 的 Gii 工具创建。我也使用 AdminLTE 模板。

它的 GridView 应该显示两列:“Phrase”和“Author”,但“Phrase”列有一些行长文本(大约 350 个字符),所以只需 a显示了此列的一部分。而且“作者”栏根本没有显示出来。

没有水平滚动条(它应该对我有帮助):

或者问题可能出在 string 数据类型上:

public function rules()
{
    return [
        [['phrase'], 'required'],
        [['phrase'], 'string'],
        [['author'], 'string', 'max' => 50],
    ];
}

这是我的index.php 视图:

<div class="box box-primary">
    <?php Pjax::begin(); ?>                
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            'phrase',
            'author',
        ],
    ]); ?>
    <?php Pjax::end(); ?>
</div>

【问题讨论】:

    标签: gridview yii2 scrollbar adminlte gii


    【解决方案1】:

    我将table-responsive 类添加到&lt;div class="box box-primary"&gt;

    <div class="box box-primary table-responsive">
        <?php Pjax::begin(); ?>
        <?= GridView::widget([
            'dataProvider' => $dataProvider,
            'filterModel' => $searchModel,
            'columns' => [
                'frase',
                'autor',
                [
                    'class' => 'yii\grid\ActionColumn',
                    'template' => '{update} {delete}'
                ],
            ],
        ]); ?>
        <?php Pjax::end(); ?>
    </div>
    

    【讨论】:

      【解决方案2】:

      你也可以使用contentOptions

      [
         'attribute' => 'phrase',
         'contentOptions' => [
             'style' => [
                 'max-width' => '600px',
                 'white-space' => 'normal',
             ],
         ],
      ],
      

      【讨论】:

        【解决方案3】:

        试试这个:

        [
            'attribute'=>'phrase',
            'value'=>function($data){
                return strlen($data->phrase) > 100 ? Html::encode(substr($data->phrase, 0, 100)) . ". . ." : $data->phrase;
            }
        ]
        

        如果字符串大小大于 100,它将显示字符串的前 100 个字符。

        根据您想要的宽度设置值。

        【讨论】:

        • OP 不想切断字符串。
        • 使用php的自动换行功能 wordwrap($data->phrase, 8, "\n", true);
        • 再一次,OP不想切断字符串。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-07
        • 2015-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多