Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability for ArrayDataProvider to generate column labels when data array is empty #11491

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@ Yii Framework 2 Change Log
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 #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 #11432: Added HTTP status 421 "Misdirected Request" to list of statuses in `yii\web\Response` (dasmfm)
Expand Down
6 changes: 5 additions & 1 deletion framework/data/ArrayDataProvider.php
Expand Up @@ -63,7 +63,11 @@ class ArrayDataProvider extends BaseDataProvider
* The array elements must use zero-based integer keys.
*/
public $allModels;

/**
* @var string the name of the \yii\base\Model based class used to provide column labels.
* @since 2.0.9
*/
public $modelClass;

/**
* @inheritdoc
Expand Down
5 changes: 5 additions & 0 deletions framework/grid/DataColumn.php
Expand Up @@ -9,6 +9,7 @@

use yii\base\Model;
use yii\data\ActiveDataProvider;
use yii\data\ArrayDataProvider;
use yii\db\ActiveQueryInterface;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
Expand Down Expand Up @@ -143,6 +144,10 @@ protected function getHeaderCellLabel()
/* @var $model Model */
$model = new $provider->query->modelClass;
$label = $model->getAttributeLabel($this->attribute);
} else if($provider instanceof ArrayDataProvider && $provider->modelClass !== null) {
/* @var $model Model */
$model = new $provider->modelClass;
$label = $model->getAttributeLabel($this->attribute);
} else {
$models = $provider->getModels();
if (($model = reset($models)) instanceof Model) {
Expand Down