【问题标题】:Yii 2 - GridView doesn't fix in the screen. How to fix it?Yii 2 - GridView 无法在屏幕中修复。如何解决?
【发布时间】:2023-03-16 16:00:01
【问题描述】:

我有这个问题:我在视图中的GridView(如下图)太长了,不能完全放在屏幕上,可能是因为一个值参数太长,没有跟上文字下。这是 GridView 的屏幕截图:

这是 GridView 的代码

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

            'fecha',
            'nombreSesion',
            'objetivosPlanificacion:ntext',


            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); 
    ?>
<?php Pjax::end(); ?>

【问题讨论】:

    标签: gridview yii yii2


    【解决方案1】:

    您可以在gridview中获取值时修剪值,如下所示

    代替

    'objetivosPlanificacion:ntext',

    使用

    [
     'attribute' => 'objetivosPlanificacion',
     'value' => function($dataProvider){
        return trim($dataProvider->objetivosPlanificacion, ' ');
     }
    ],
    

    一切都会为你工作。

    【讨论】:

      【解决方案2】:

      如果您不介意不显示属性 objetivosPlanificacion 的完整值,您可以执行以下操作:

      use yii\helpers\StringHelper;
      ...
      <?php Pjax::begin(); ?>
          <?= GridView::widget([
              'dataProvider' => $dataProvider,
              'filterModel' => $searchModel,
              'columns' => [
                  ['class' => 'yii\grid\SerialColumn'],
      
                  'fecha',
                  'nombreSesion',
                  [
                      'attribute' => 'objetivosPlanificacion',
                      'value' => function($model, $key, $index, $column) {
                          return StringHelper::truncate($model->objetivosPlanificacion, 50);
                      },
                      'format' => 'ntext',
                  ],
      
                  ['class' => 'yii\grid\ActionColumn'],
              ],
          ]); 
          ?>
      <?php Pjax::end(); ?>
      

      基本上将属性值截断,格式为ntext。

      【讨论】:

        【解决方案3】:

        这是CSS的问题

        查看 /web/css/site.css 并搜索

        .grid-view td {
            white-space: nowrap;
        }
        

        并将其更改为

        .grid-view td {
            white-space: normal;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-09-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多