How to create a row span in yii2 gridview?

92 Views Asked by At

My gridview wants to implement rowsapn to two columns, Offical MOU sites and Official MOU sites > 2700. I want some logic to bring value to this two columns.

What is the best way to implement this in Yii2?

enter image description here

I try this code

echo GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        'id',
        [
            'attribute' => 'category',
            'value' => function ($model) use ($dataProvider) {
                $count = $dataProvider->query->where(['category' => $model->category])->count();
                return $model->category . ' (Count: ' . $count . ')';
            },
        ],
        'name',
        'price',
    ],
]);

But this is not what I really want.

Thanks!

1

There are 1 best solutions below

0
Chan Kar Fong On

If you want to implement rowspan for two columns, Official MOU sites and Official MOU sites > 2700, you can achieve this by customizing the GridView in Yii2. You need to modify the columns array and use the contentOptions property to set the rowspan based on your logic.

use yii\grid\GridView;

echo GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        [
            'attribute' => 'id',
            'contentOptions' => ['class' => 'text-center'], // Adjust styling as needed
        ],
        [
            'attribute' => 'category',
            'value' => function ($model) use ($dataProvider) {
                return $model->category;
            },
            'contentOptions' => function ($model, $key, $index, $column) use ($dataProvider) {
                $count = $dataProvider->query->where(['category' => $model->category])->count();
                return ['rowspan' => $count + 1]; // Add 1 for the main row
            },
        ],
        [
            'attribute' => 'name',
            'contentOptions' => ['class' => 'text-center'], // Adjust styling as needed
        ],
        [
            'attribute' => 'price',
            'contentOptions' => ['class' => 'text-center'], // Adjust styling as needed
        ],
    ],
]);