Yii2 如何在Yi2中显示带有activeDataProvider标题的节中的记录

Yii2 如何在Yi2中显示带有activeDataProvider标题的节中的记录,yii2,cactivedataprovider,yii-widgets,Yii2,Cactivedataprovider,Yii Widgets,我的数据库结构如下 id : item : name : price 1 : framework : bootstrap : 90 1 : framework : zend : 100 1 : framework : drupal : 150 1 : responsive : no : 0 1 : responsive : yes : 50 我希望将结果呈现为具有相同项的行,这些行必须具有该项名称作为节标题,并且该节下的其余数据应

我的数据库结构如下

id :    item    :    name   : price
1  : framework  : bootstrap : 90
1  : framework  : zend : 100
1  : framework  : drupal : 150
1  : responsive : no        : 0
1  : responsive : yes       : 50
我希望将结果呈现为具有相同项的行,这些行必须具有该项名称作为节标题,并且该节下的其余数据应显示为

框架

Name      : Price
Bootstrap : 90
Zend      : 100
Drupal    : 150
响应迅速

Name    : Price
None    : 0
Yes     : 50

如何使用活动数据提供程序、数据小部件或其他方法来实现这一点

我建议您覆盖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
])

?>

是要在同一页中显示所有表,还是选择一个项目(框架、响应等)并仅显示该项目?实际上,这只是一个表,我要显示所有项目(框架、响应等)在同一个页面上,但有不同的部分来区分它们。然后我不知道如何使用sindle dataProvider/GridView。最简单的方法是按项目对记录进行排序,并在标题中使用下拉列表进行过滤。否则,我会使用不同的DataProviders/GridViews。我只是重新定义,我认为这正是您想要的