I use GridView for data output. I want to have two in thead instead of one line. Can this be done using only the capabilities of Yii2 itself?

At once I will make a reservation that I consider the option with the yii2-grid module as extreme.

Example. In the output table, among other things, there are two columns with the headings "Nearest display" and "Start time" :

| Ближайшая дата показа | Время начала | +-----------------------+--------------+ | 2015-10-23 | 19:30:00 | | 2015-12-03 | 19:00:00 | 

I would like to add an extra line above the header to merge the columns into groups according to the meaning:

 | Ближайший показ | +------------+----------+ | Дата | Начало | +------------+----------+ | 2015-10-23 | 19:30:00 | | 2015-12-03 | 19:00:00 | 

How to add such a line in the header?

  • gitter.im/yiisoft/yii2/rus asked here? - des1roer
  • Th-deaf there. - Denis Khvorostin
  • and what is the extension is not suitable? updated 6 hours ago - des1roer
  • A closer look reveals that a lot of things work out of the box. No need to produce entities and bicycles. So I thought, isn’t Yii2 doing this? - Denis Khvorostin

1 answer 1

Standard GridView does not allow to make an additional header. In order not to drag a heavy extension, you can slightly add a standard one.

 <?php namespace common; use Yii; use yii\helpers\Html; class GridView extends \yii\grid\GridView { /** * @var string */ public $myHeaderTitle = 'My default header title'; /** * @var array */ public $myHeaderRowOptions = []; /** * @inheritdoc */ public function renderTableHeader() { $customHeader = $this->renderCustomHeader(); return $this->renderTableHeaderWithCustomHeader($customHeader); } /** * @return string */ private function renderCustomHeader() { return Html::tag('th', $this->myHeaderTitle, [ 'colspan' => count($this->columns) ]); } /** * @return string */ private function renderTableHeaderWithCustomHeader($customHeader) { $content = Html::tag('tr', $customHeader, $this->myHeaderRowOptions); $content = $content . parent::renderTableHeader(); return "<thead>\n" . $content . "\n</thead>"; } }