Yii2: Colspan in GridView footer

Created on 25 Apr 2014  路  12Comments  路  Source: yiisoft/yii2

It will be usefull to make methods/options in GridView which can generate one cell in table footer (with colspan="[columns count]").
For example, it let me to insert buttons for actions with checked rows, like 'Print checked objects' or 'Delete checked objects'.
Now if I add footer with colspan attr in one column, renderTableFooter() will generate unneeded cells for another columns.

under discussion feature

Most helpful comment

Late answer but just in case

...
[
  ...
  'footerOptions' => ['class' => 'grid-footer', 'colspan' => 2],
],
...,
[
  ...
  'footerOptions' => ['hidden' => true],
  ...
],

All 12 comments

Currently footer is consisting of each column's footer. I don't think it's a good idea to introduce another property for custom rendering because one can override GridView::renderTableFooter for the purpose.

I don't see the way to override GridView::renderTableFooter except extending this widget. So you are suggesting to make my own implementation of this class just to render colspaned footer row?

Yes. It is easy:

class SummaryGridView extends GridView
{
    public function renderTableFooter()
    {
        $content = ...
        return "<tfoot><tr><td>\n" . $content . "\n</td></tr></tfoot>";
    }
}

Yes, it is easy, but not flexible. Why not to add option with callable renderTableFooter to widget, for example?

@RomeroMsk Because in that case its better to extends GridView than in view have big horrible anonymous function which generates footer.

It is extremely flexible. You can do anything this way.

Ok, than why not to add option to make colspaned cell like this?

echo GridView::widget([
    'columns' => [
        [
            'attribute' => 'att1',
            'footer' => '<button>First</button> text <button>Second</button>',
            'colspan' => true, // This will render footer cell as <td colspan="4">...</td> (where 4 will be set by columns count)
        ],
        'att2',
        'att3',
        'att4',
    ],
]);

@RomeroMsk Because if we introduce your suggestion about colspanned footer, after 5 minutes i'll create issues about approach for colspanned header and body cells and we'll enter to the hell ))) Its joke ofcourse, but if we have feature for colspanned footer we need at least have one for header, for filter zone, etc.

Also look at your example:

        [
            'attribute' => 'att1',
            'footer' => '<button>First</button> text <button>Second</button>',
            'colspan' => true,
        ],

Ok, fine we have new cool colspan option here. Great. But why its new cool colspan option affect only footer? Why this new colspan option affect all columns? What if i want to colspan only 2? You got idea? If we introduce new option it should have better approach and better customizing.

Personally i like colspanning idea via that way. But you need think about approach better and ofcourse not forget about header/filters/body.

Colspanned footer visually/logically belongs to more than a single column and defining it at the column level seems to be wrong thing to do. Callback is OK but as I've noted it can be achieved by subclassing.

@creocoder it was just an example to show my idea. It can be footer_colspan => 4, header_colspan => 2 and cell_colspan => 3 - not a problem :) But GridView main goal is to show data set with sorting and filtering, and I just can't imagine an example for colspaned header and data cells.
@samdark yes, it belongs to more than column. But I think it can be a column option too, because you expanding one (specific) column to several.

Ok, I've got your mood, I'll do it myself by div aligned to the bottom of table or by extending widget class. Thanks.

Late answer but just in case

...
[
  ...
  'footerOptions' => ['class' => 'grid-footer', 'colspan' => 2],
],
...,
[
  ...
  'footerOptions' => ['hidden' => true],
  ...
],
Was this page helpful?
0 / 5 - 0 ratings