2017-04-03 62 views
0

我的数据库结构如下如何显示在部分记录与activeDataProvider在yii2标题

id : item : name : price 
1 : framework : bootstrap : 90 
1 : framework : zend : 100 
1 : framework : drupal : 150 
1 : responsive : no  : 0 
1 : responsive : yes  : 50 

我要呈现的结果具有相同的项目行必须有一个项目名称为节标题和休息数据应根据该条被显示为

FRAMEWORK

Name  : Price 
Bootstrap : 90 
Zend  : 100 
Drupal : 150 

RESPONSIVE

Name : Price 
None : 0 
Yes  : 50 

如何与活跃的数据提供者,数据控件这样做还是有可能是另一种方法

+0

你想在同一个页面显示所有的表格,或者你会选择一个项目(框架,响应,...)并只显示那个? – gmc

+0

其实这只是一个表,我想在同一页面显示所有项目(框架,responsie等),但部分区分它们。 –

+0

然后,我不知道如何使用主轴dataProvider/GridView来做到这一点。最简单的事情是按项目排序记录,并用标题中的下拉列表对它们进行过滤。否则,我会使用不同的dataProvders/GridViews – gmc

回答

1

我建议你重写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 
]) 

?> 

请记住,您必须按分组栏排序。

相关问题