Skip to content

Commit 3645c37

Browse files
authored
Load label translations from model attribute labels (#68)
* Load label translations from model attributes Rebased from - 22cd96b Add check for empty data - 0110c6d Improve label handling - de23a7a Load label translations from model attributes * gh-68 Add support for dataProvider / improve detection of labels by models
1 parent 54da8ed commit 3645c37

File tree

4 files changed

+81
-2
lines changed

4 files changed

+81
-2
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5+
## next release
6+
### Added
7+
- Support for bootstrap 4 (autodetect required bootstrap version)
8+
- Add `dataProvider` property to `DataTable`
9+
- if set, property `data` is auto filled with models from dataProvider
10+
- if models are found either in `dataProvider` or in `data`, column labels are loaded from
11+
`Model::attributes()`
12+
513
## v1.1.1
614
### Added
715
- Add `extraColumns` property to `DataTable`, `DataTableColumn`, `DataTableAction`

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ to the require section of your `composer.json` file.
2020

2121
## Basic Usage
2222

23+
```php
24+
<?= \nullref\datatable\DataTable::widget([
25+
'dataProvider' => $dataProvider,
26+
'columns' => [
27+
'id',
28+
'name',
29+
'email'
30+
],
31+
]) ?>
32+
```
33+
34+
For backwards compatibility the old usage via `data` is still supported
2335
```php
2436
<?= \nullref\datatable\DataTable::widget([
2537
'data' => $dataProvider->getModels(),
@@ -31,6 +43,8 @@ to the require section of your `composer.json` file.
3143
]) ?>
3244
```
3345

46+
47+
3448
## DataTable options
3549
Also you can use all [Datatables options](https://datatables.net/reference/option/)
3650

src/DataTable.php

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
namespace nullref\datatable;
99

1010
use nullref\datatable\assets\DataTableAsset;
11+
use yii\base\Model;
1112
use yii\base\Widget;
13+
use yii\data\ActiveDataProvider;
14+
use yii\data\ArrayDataProvider;
15+
use yii\db\ActiveQueryInterface;
1216
use yii\helpers\ArrayHelper;
1317
use yii\helpers\Html;
1418
use yii\helpers\Inflector;
@@ -104,26 +108,44 @@ class DataTable extends Widget
104108
public $globalVariable = false;
105109
protected $_options = [];
106110

111+
/**
112+
* @var \yii\data\DataProviderInterface the data provider for the view.
113+
*/
114+
protected $_dataProvider;
115+
107116
protected $_extraColumns = [];
108117

118+
/**
119+
* @throws \yii\base\InvalidConfigException
120+
* @throws \Exception if ArrayHelper::getValue()
121+
*/
109122
public function init()
110123
{
111124
parent::init();
125+
if ($this->data === null) {
126+
$this->data = is_null($this->_dataProvider) ? [] : $this->_dataProvider->getModels();
127+
}
112128
DataTableAsset::register($this->getView());
113129
$this->initColumns();
114130
$this->initData();
115131
}
116132

133+
/**
134+
* @throws \yii\base\InvalidConfigException
135+
*/
117136
protected function initColumns()
118137
{
119138
$this->_extraColumns = $this->extraColumns;
120139
if (isset($this->_options['columns'])) {
140+
$demoObject = $this->getModel();
121141
foreach ($this->_options['columns'] as $key => $value) {
122142
if (!is_array($value)) {
123143
$value = [
124144
'class' => DataTableColumn::class,
125145
'attribute' => $value,
126-
'label' => Inflector::camel2words($value)
146+
'label' => $demoObject instanceof \yii\base\Model
147+
? $demoObject->getAttributeLabel($value)
148+
: Inflector::camel2words($value)
127149
];
128150
}
129151
if (isset($value['type'])) {
@@ -147,6 +169,35 @@ protected function initColumns()
147169

148170
}
149171

172+
/**
173+
* Detect a model class from `dataProvider` or `data` attributes
174+
*
175+
* @see \yii\grid\DataColumn::getHeaderCellLabel()
176+
*
177+
* return Model|null NULL is returned when only property $data is defined, and is either empty or first entry is not of type model
178+
*/
179+
protected function getModel()
180+
{
181+
$provider = $this->_dataProvider;
182+
if ($provider instanceof ActiveDataProvider && $provider->query instanceof ActiveQueryInterface) {
183+
/* @var $modelClass Model */
184+
$modelClass = $provider->query->modelClass;
185+
$model = $modelClass::instance();
186+
} elseif ($provider instanceof ArrayDataProvider && $provider->modelClass !== null) {
187+
/* @var $modelClass Model */
188+
$modelClass = $provider->modelClass;
189+
$model = $modelClass::instance();
190+
} else {
191+
$models = $this->data;
192+
//$model = (count($models)) ? $models[0] : null;
193+
$model = reset($models);
194+
}
195+
return $model instanceof Model ? $model : null;
196+
}
197+
198+
/**
199+
* @throws \Exception if ArrayHelper::getValue() throws
200+
*/
150201
private function initData()
151202
{
152203
$this->_extraColumns = array_unique($this->_extraColumns);
@@ -242,11 +293,17 @@ protected function getParams()
242293

243294
public function __get($name)
244295
{
296+
if ($name == 'dataProvider') {
297+
return $this->_dataProvider;
298+
}
245299
return isset($this->_options[$name]) ? $this->_options[$name] : null;
246300
}
247301

248302
public function __set($name, $value)
249303
{
304+
if ($name == 'dataProvider') {
305+
return $this->_dataProvider = $value;
306+
}
250307
return $this->_options[$name] = $value;
251308
}
252309

src/DataTableColumn.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function init()
7979
throw new InvalidConfigException("Either 'data' or 'render' properties must be specified.");
8080
}
8181

82-
if ($this->title === null) {
82+
if ($this->title === null && !is_null($this->attribute)) {
8383
$this->title = Inflector::camel2words($this->attribute);
8484
}
8585

0 commit comments

Comments
 (0)