Browse Source

Updated `yii\grid\DataColumn::getHeaderCellLabel()` to extract attribute label from the `filterModel` of Grid

Closes #9950
ar-bug
SilverFire - Dmitry Naumenko 9 years ago
parent
commit
53e1018648
  1. 3
      framework/CHANGELOG.md
  2. 2
      framework/grid/DataColumn.php
  3. 24
      tests/framework/grid/DataColumnTest.php

3
framework/CHANGELOG.md

@ -5,7 +5,8 @@ Yii Framework 2 Change Log
2.0.9 under development 2.0.9 under development
----------------------- -----------------------
- Enh #4146: Added `yii\data\ArrayDataProvider::$modelClass` property to specify a model used to provide column labels even when data array is empty (PowerGamer1) - Enh #11490: Added `yii\data\ArrayDataProvider::$modelClass` property to specify a model used to provide column labels even when data array is empty (PowerGamer1)
- Bug #9950: Updated `yii\grid\DataColumn::getHeaderCellLabel()` to extract attribute label from the `filterModel` of Grid (silverfire)
- Enh #11428: Speedup SQL query in `yii\db\oci\Schema::findColumns()` (SSiwek) - Enh #11428: Speedup SQL query in `yii\db\oci\Schema::findColumns()` (SSiwek)
- Enh #11414: Files specified as `null` in `yii\web\AssetBundle` won't be registered (Razzwan) - Enh #11414: Files specified as `null` in `yii\web\AssetBundle` won't be registered (Razzwan)
- Enh #11432: Added HTTP status 421 "Misdirected Request" to list of statuses in `yii\web\Response` (dasmfm) - Enh #11432: Added HTTP status 421 "Misdirected Request" to list of statuses in `yii\web\Response` (dasmfm)

2
framework/grid/DataColumn.php

@ -148,6 +148,8 @@ class DataColumn extends Column
/* @var $model Model */ /* @var $model Model */
$model = new $provider->modelClass; $model = new $provider->modelClass;
$label = $model->getAttributeLabel($this->attribute); $label = $model->getAttributeLabel($this->attribute);
} else if ($this->grid->filterModel !== null && $this->grid->filterModel instanceof Model) {
$label = $this->grid->filterModel->getAttributeLabel($this->attribute);
} else { } else {
$models = $provider->getModels(); $models = $provider->getModels();
if (($model = reset($models)) instanceof Model) { if (($model = reset($models)) instanceof Model) {

24
tests/framework/grid/DataColumnTest.php

@ -20,7 +20,7 @@ class DataColumnTest extends \yiiunit\TestCase
$this->mockApplication(); $this->mockApplication();
} }
public function testColumnLabelsOnEmptyProvider() public function testColumnLabelsOnEmptyArrayProvider()
{ {
$grid = new GridView([ $grid = new GridView([
'dataProvider' => new ArrayDataProvider([ 'dataProvider' => new ArrayDataProvider([
@ -41,4 +41,26 @@ class DataColumnTest extends \yiiunit\TestCase
$this->assertEquals(['Customer', 'Invoice Total'], $labels); $this->assertEquals(['Customer', 'Invoice Total'], $labels);
} }
public function testColumnLabelsOnEmptyArrayProviderWithFilterModel()
{
$grid = new GridView([
'dataProvider' => new ArrayDataProvider([
'allModels' => [],
'totalCount' => 0,
]),
'columns' => ['customer_id', 'total'],
'filterModel' => new Order
]);
$labels = [];
foreach ($grid->columns as $column) {
$method = new \ReflectionMethod($column, 'getHeaderCellLabel');
$method->setAccessible(true);
$labels[] = $method->invoke($column);
$method->setAccessible(false);
}
$this->assertEquals(['Customer', 'Invoice Total'], $labels);
}
} }

Loading…
Cancel
Save