GridView with several lines per model

Hi,
How can I add an extra row to the model to the GridView? I would like to have several separate columns for the main row and for some models an additional row with some description. I need this for renderPartial to generate PDF so I can not use JS.

You may define a function for afterRow to render additional rows for the same model:

https://www.yiiframework.com/doc/api/2.0/yii-grid-gridview#$afterRow-detail

1 Like

In this way I made rows with data. Unfortunately, I do not see the possibility of adding an extra colspan column to header.

The result of afterRow should be a set of

tags, you are completely free in the HTML you return there, so you should be able to add colspan tags as well.

Using afterRow I have this (without red):


I need to add an extra row in the header, which is what I have marked in the picture by red color. Unfortunately, afterRow does not work on header.

1 Like

Thanks for the visualization! Now it’s clear what you want to do.

This is not possible by configuring the GridView. You need to extend the class.

The header of GridView is built in renderTableHeader():

extending this method is a bit complicated but as you see above, there is another method which adds content to the header, so you may instead extend renderFilters() and append the row you want:

class MyGridView extends \yii\grid\GridView
{
    /**
     * Renders the filter.
     * @return string the rendering result.
     */
    public function renderFilters()
    {
        $yourRows = '<tr><td>...</td>... </tr>';
        return parent::renderFilters() . $yourRows;
    }
}
1 Like

Thanks! I thought that maybe someone predicted such an option in this class. If not, I will write my own extension of this class.