Skip to content

Commit

Permalink
Load label translations from model attribute labels (#68)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Radon8472 authored Aug 16, 2022
1 parent 54da8ed commit 3645c37
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## next release
### Added
- Support for bootstrap 4 (autodetect required bootstrap version)
- Add `dataProvider` property to `DataTable`
- if set, property `data` is auto filled with models from dataProvider
- if models are found either in `dataProvider` or in `data`, column labels are loaded from
`Model::attributes()`

## v1.1.1
### Added
- Add `extraColumns` property to `DataTable`, `DataTableColumn`, `DataTableAction`
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ to the require section of your `composer.json` file.

## Basic Usage

```php
<?= \nullref\datatable\DataTable::widget([
'dataProvider' => $dataProvider,
'columns' => [
'id',
'name',
'email'
],
]) ?>
```

For backwards compatibility the old usage via `data` is still supported
```php
<?= \nullref\datatable\DataTable::widget([
'data' => $dataProvider->getModels(),
Expand All @@ -31,6 +43,8 @@ to the require section of your `composer.json` file.
]) ?>
```



## DataTable options
Also you can use all [Datatables options](https://datatables.net/reference/option/)

Expand Down
59 changes: 58 additions & 1 deletion src/DataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
namespace nullref\datatable;

use nullref\datatable\assets\DataTableAsset;
use yii\base\Model;
use yii\base\Widget;
use yii\data\ActiveDataProvider;
use yii\data\ArrayDataProvider;
use yii\db\ActiveQueryInterface;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Inflector;
Expand Down Expand Up @@ -104,26 +108,44 @@ class DataTable extends Widget
public $globalVariable = false;
protected $_options = [];

/**
* @var \yii\data\DataProviderInterface the data provider for the view.
*/
protected $_dataProvider;

protected $_extraColumns = [];

/**
* @throws \yii\base\InvalidConfigException
* @throws \Exception if ArrayHelper::getValue()
*/
public function init()
{
parent::init();
if ($this->data === null) {
$this->data = is_null($this->_dataProvider) ? [] : $this->_dataProvider->getModels();
}
DataTableAsset::register($this->getView());
$this->initColumns();
$this->initData();
}

/**
* @throws \yii\base\InvalidConfigException
*/
protected function initColumns()
{
$this->_extraColumns = $this->extraColumns;
if (isset($this->_options['columns'])) {
$demoObject = $this->getModel();
foreach ($this->_options['columns'] as $key => $value) {
if (!is_array($value)) {
$value = [
'class' => DataTableColumn::class,
'attribute' => $value,
'label' => Inflector::camel2words($value)
'label' => $demoObject instanceof \yii\base\Model
? $demoObject->getAttributeLabel($value)
: Inflector::camel2words($value)
];
}
if (isset($value['type'])) {
Expand All @@ -147,6 +169,35 @@ protected function initColumns()

}

/**
* Detect a model class from `dataProvider` or `data` attributes
*
* @see \yii\grid\DataColumn::getHeaderCellLabel()
*
* return Model|null NULL is returned when only property $data is defined, and is either empty or first entry is not of type model
*/
protected function getModel()
{
$provider = $this->_dataProvider;
if ($provider instanceof ActiveDataProvider && $provider->query instanceof ActiveQueryInterface) {
/* @var $modelClass Model */
$modelClass = $provider->query->modelClass;
$model = $modelClass::instance();
} elseif ($provider instanceof ArrayDataProvider && $provider->modelClass !== null) {
/* @var $modelClass Model */
$modelClass = $provider->modelClass;
$model = $modelClass::instance();
} else {
$models = $this->data;
//$model = (count($models)) ? $models[0] : null;
$model = reset($models);
}
return $model instanceof Model ? $model : null;
}

/**
* @throws \Exception if ArrayHelper::getValue() throws
*/
private function initData()
{
$this->_extraColumns = array_unique($this->_extraColumns);
Expand Down Expand Up @@ -242,11 +293,17 @@ protected function getParams()

public function __get($name)
{
if ($name == 'dataProvider') {
return $this->_dataProvider;
}
return isset($this->_options[$name]) ? $this->_options[$name] : null;
}

public function __set($name, $value)
{
if ($name == 'dataProvider') {
return $this->_dataProvider = $value;
}
return $this->_options[$name] = $value;
}

Expand Down
2 changes: 1 addition & 1 deletion src/DataTableColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function init()
throw new InvalidConfigException("Either 'data' or 'render' properties must be specified.");
}

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

Expand Down

0 comments on commit 3645c37

Please sign in to comment.