组/合并列Yii2 Gridview

时间:2016-03-01 20:32:32

标签: gridview yii2

如何在gridview中合并一个或多个列?

我目前正在使用Kartikv Gridview,但不想创建额外的页脚/标题。

使用普通表格,这将通过colspan

实现

我想将第2列中的下方“OD”与右侧的4列合并,只是“OD”居中

grdiview

当前GridView:

  <?= GridView::widget([
    'dataProvider'=> $fixtures,
    'columns' => [
        [
            'header' => 'Match Date',
            'value' => function($data){
                return date("d M Y", strtotime($data['fixtureDate']));
            }
        ],
        [
            'header' => 'Home / Away',
            'value' => function($data){
                return $data['homeAway']
            }
        ],
        [
            'contentOptions' => function($data){
                return $style['class'];
            },
            'value' => function($data){
                if($data['homeAway'] == 1 || $data['homeAway'] == 0){
                    return $data['homeScore'];  
                }
            },
        ],
        [
            'value' => function(){
                return 'vs';    
            }
        ],
        [
            'value' => function($data){
                return $data['awayScore'];
            }
        ],
        [
            'header' => 'Against',
            'value' => function($data){
                return $data['awayTeamName'];
            }
        ]
    ],
    'summary' => false,
    'responsive'=>true,
    'hover'=>true
]); ?>

2 个答案:

答案 0 :(得分:1)

这个主题有点老了,但是您可以像这样合并标题单元格:

<?= GridView::widget([
'dataProvider'=> $fixtures,
'columns' => [
    [
        'header' => 'Match Date',
        'value' => function($data){
            return date("d M Y", strtotime($data['fixtureDate']));
        }
    ],
    [
        'header' => 'Home / Away',
        'value' => function($data){
            return $data['homeAway']
        },
        'headerOptions' => [
            'colspan' => 3,
        ]
    ],
    [
        'contentOptions' => function($data){
            return $style['class'];
        },
        'value' => function($data){
            if($data['homeAway'] == 1 || $data['homeAway'] == 0){
                return $data['homeScore'];  
            }
        },
    ],
    [
        'value' => function(){
            return 'vs';    
        }
    ],
    [
        'value' => function($data){
            return $data['awayScore'];
        }
    ],
    [
        'header' => 'Against',
        'value' => function($data){
            return $data['awayTeamName'];
        }
    ]
],
'summary' => false,
'responsive'=>true,
'hover'=>true
]); ?>

在headerOptions参数中,如果您提供任何html标记属性,它将被应用,例如colspan,class,style等...

我希望这对发现此问题的人有所帮助。

答案 1 :(得分:0)

可能是你需要这个

<?= GridView::widget([
  'dataProvider'=> $fixtures,
  'columns' => [
      [
          'header' => 'Match Date',
          'value' => function($data){
              return date("d M Y", strtotime($data['fixtureDate']));
          }
      ],
      [
          'header' => 'Home / Away',
          'value' => function($data){
              return $data['homeAway']
          }
      ],
      [
          'header' => 'Home / Away',
          'value' => function($model){

             if($model->homeAway == 1 || $model->homeAway == 0){
                  return $model->homeAway . ' ' . $model->homeScore . ' vs ' . $model->awayScore . ' ' . $model->awayTeamName;  
              }  else {
                  return $model->homeAway . ' '  . ' vs ' . $model->awayScore . ' ' . $model->awayTeamName; 
              }
          }
      ],      

  ],
  'summary' => false,
  'responsive'=>true,
  'hover'=>true
]); ?>