我建议你重写GridView的renderTableRow。如果您在要分组的列上检测到值更改,则只需插入一个自定义组标题行。
<?php
class GroupGridView extends \yii\grid\GridView
{
public $groupingColumn = null;
public $groupingText = null;
public function renderTableRow ($model, $key, $index)
{
if ($this->groupingColumn == null)
return parent::renderTableRow($model, $key, $index);
$models = $this->dataProvider->models;
$result = '';
if ($index < count($models) - 1 && ($index == 0 || $models[$index][$this->groupingColumn] != $models[$index + 1][$this->groupingColumn])) {
$result = sprintf('<tr class="grouping"><td colspan="%d">%s</td></tr>', count($this->columns), ($this->groupingText == null ? $models[$index]->{$this->groupingColumn} : (is_callable($this->groupingText) ? call_user_func($this->groupingText, $model, $key, $index) : $this->groupingText)));
}
return $result . parent::renderTableRow($model, $key, $index);
}
}
$dataProvider = new \yii\data\ActiveDataProvider([
'query' => \common\models\Task::find()->orderBy(['customer_id' => SORT_ASC]),
'pagination' => [
'pageSize' => 100,
],
]);
echo GroupGridView::widget([
'groupingColumn' => 'item',
'groupingText' => function($model, $key, $index) {
return sprintf('--- %s --- %s --- %d', $model->item, $key, $index);
},
'dataProvider' => $dataProvider
])
?>
请记住,您必须按分组栏排序。
你想在同一个页面显示所有的表格,或者你会选择一个项目(框架,响应,...)并只显示那个? – gmc
其实这只是一个表,我想在同一页面显示所有项目(框架,responsie等),但部分区分它们。 –
然后,我不知道如何使用主轴dataProvider/GridView来做到这一点。最简单的事情是按项目排序记录,并用标题中的下拉列表对它们进行过滤。否则,我会使用不同的dataProvders/GridViews – gmc