Skip to content

Commit fbd9199

Browse files
committed
#53 add extraColumns property
1 parent 3efb0d8 commit fbd9199

File tree

6 files changed

+131
-26
lines changed

6 files changed

+131
-26
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
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+
## v1.1.1
6+
### Added
7+
- Add `extraColumns` property to `DataTable`, `DataTableColumn`, `DataTableAction`
8+
59
## v1.1.0
610
### Added
711
- Add `sClass` property for `DataTableColumn` class

README.md

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -214,18 +214,17 @@ It's possible to use custom styles and scripts:
214214
To enable server-side processing add `DataTableAction` to controller like this:
215215

216216
```php
217-
class SomeController extends Controller
218-
{
219-
public function actions()
220-
{
221-
return [
222-
'datatables' => [
223-
'class' => 'nullref\datatable\DataTableAction',
224-
'query' => Model::find(),
225-
],
226-
];
227-
}
228-
217+
class SomeController extends Controller
218+
{
219+
public function actions()
220+
{
221+
return [
222+
'datatables' => [
223+
'class' => 'nullref\datatable\DataTableAction',
224+
'query' => Model::find(),
225+
],
226+
];
227+
}
229228
}
230229
```
231230

@@ -260,17 +259,61 @@ public function actions()
260259
],
261260
];
262261
}
263-
264262
```
265263

266264
If you need to get some relation data you can call `join` or similar methods from `$query` in `applyFilter` closure.
267265

268266
And add options to widget:
269267

270268
```php
271-
<?= \nullref\datatable\DataTable::widget([
272-
/** ... */
273-
'serverSide' => true,
274-
'ajax' => '/site/datatables',
275-
]) ?>
269+
<?= \nullref\datatable\DataTable::widget([
270+
/** ... */
271+
'serverSide' => true,
272+
'ajax' => '/site/datatables',
273+
]) ?>
274+
```
275+
276+
277+
## Extra columns
278+
279+
If need to use some custom fields from your model at your render function at column you could pass `extraColumns` param.
280+
281+
It available at DataTable widget, column and server side action definition:
282+
283+
```php
284+
<?= \nullref\datatable\DataTable::widget([
285+
/** ... */
286+
'data' => $dataProvider->getModels(),
287+
'extraColumns' => ['customPrice'],
288+
'columns' => [
289+
[
290+
'title' => 'Custom column',
291+
'extraColumns' => ['customField'],
292+
'render' => new JsExpression($customColumnRender),
293+
],
294+
],
295+
]) ?>
296+
```
297+
298+
```php
299+
class SomeController extends Controller
300+
{
301+
public function actions()
302+
{
303+
return [
304+
'datatables' => [
305+
'class' => 'nullref\datatable\DataTableAction',
306+
'query' => Model::find(),
307+
'extraColumns' => ['customPrice'],
308+
],
309+
];
310+
}
311+
}
312+
```
313+
314+
```php
315+
<?= \nullref\datatable\DataTable::widget([
316+
/** ... */
317+
'extraColumns' => ['customPrice'],
318+
]) ?>
276319
```

src/DataTable.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,23 @@ class DataTable extends Widget
8888
const PAGING_FULL = 'full';
8989
const PAGING_FULL_NUMBERS = 'full_numbers';
9090
public $id;
91+
9192
/**
9293
* @var array Html options for table
9394
*/
9495
public $tableOptions = [];
96+
97+
/**
98+
* @var array
99+
*/
100+
public $extraColumns = [];
101+
95102
public $withColumnFilter;
96103
/** @var bool */
97104
public $globalVariable = false;
98105
protected $_options = [];
99-
protected $_hiddenColumns = [];
106+
107+
protected $_extraColumns = [];
100108

101109
public function init()
102110
{
@@ -108,6 +116,7 @@ public function init()
108116

109117
protected function initColumns()
110118
{
119+
$this->_extraColumns = $this->extraColumns;
111120
if (isset($this->_options['columns'])) {
112121
foreach ($this->_options['columns'] as $key => $value) {
113122
if (!is_array($value)) {
@@ -128,8 +137,8 @@ protected function initColumns()
128137
}
129138
if (isset($value['class'])) {
130139
$column = \Yii::createObject($value);
131-
if ($column instanceof LinkColumn) {
132-
$this->_hiddenColumns = array_merge($this->_hiddenColumns, $column->queryParams);
140+
if ($column instanceof DataTableColumn) {
141+
$this->_extraColumns = array_merge($this->_extraColumns, $column->getExtraColumns());
133142
}
134143
$this->_options['columns'][$key] = $column;
135144
}
@@ -140,7 +149,7 @@ protected function initColumns()
140149

141150
private function initData()
142151
{
143-
$this->_hiddenColumns = array_unique($this->_hiddenColumns);
152+
$this->_extraColumns = array_unique($this->_extraColumns);
144153
if (array_key_exists('data', $this->_options)) {
145154
$data = [];
146155

@@ -163,7 +172,7 @@ private function initData()
163172
}
164173
}
165174
}
166-
foreach ($this->_hiddenColumns as $column) {
175+
foreach ($this->_extraColumns as $column) {
167176
$row[$column] = ArrayHelper::getValue($obj, $column);
168177
}
169178
if ($row) {

src/DataTableAction.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ class DataTableAction extends Action
7373
*/
7474
public $formatResponse;
7575

76+
/**
77+
* Add extra fields to dataset
78+
* These fields could be used at render function
79+
*
80+
* @var array
81+
*/
82+
public $extraColumns = [];
83+
7684
/**
7785
* Check if query is configured
7886
* @throws InvalidConfigException
@@ -135,11 +143,12 @@ public function run()
135143
$dataProvider = new ActiveDataProvider(['query' => $filterQuery, 'pagination' => ['pageSize' => Yii::$app->request->getQueryParam('length', 10)]]);
136144
Yii::$app->response->format = Response::FORMAT_JSON;
137145
try {
146+
$allColumns = array_merge($columns, $this->getExtraColumns());
138147
$response = [
139148
'draw' => (int)$draw,
140149
'recordsTotal' => (int)$originalQuery->count(),
141150
'recordsFiltered' => (int)$dataProvider->getTotalCount(),
142-
'data' => $this->formatData($filterQuery, $columns),
151+
'data' => $this->formatData($filterQuery, $allColumns),
143152
];
144153
} catch (\Exception $e) {
145154
return ['error' => $e->getMessage()];
@@ -208,6 +217,16 @@ public function applyOrder(ActiveQuery $query, $columns, $order)
208217
return $query;
209218
}
210219

220+
/**
221+
* Prepare extraColumns for
222+
*/
223+
protected function getExtraColumns()
224+
{
225+
return array_map(function ($column) {
226+
return ['data' => $column];
227+
}, $this->extraColumns);
228+
}
229+
211230
/**
212231
* @param ActiveQuery $query
213232
* @param array $columns

src/DataTableColumn.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ class DataTableColumn extends Widget
4747
/** @var string CSS class for column cell */
4848
public $sClass = '';
4949

50+
public $className = '';
51+
52+
/**
53+
* Add extra fields to dataset
54+
* These fields could be used at render function
55+
*
56+
* @var array
57+
*/
58+
public $extraColumns = [];
59+
5060
/**
5161
* @var array|null|false Indicating if a filter will be displayed or not.
5262
*
@@ -56,7 +66,7 @@ class DataTableColumn extends Widget
5666
* the list options.
5767
* - If you don't want a filter for this data column, set this value to be false.
5868
*/
59-
private $filter;
69+
protected $filter;
6070

6171
/**
6272
* Check if all required properties is set
@@ -179,4 +189,12 @@ public function setFilter($filter)
179189
$this->filter = $filter;
180190
}
181191

182-
}
192+
/**
193+
* @return array
194+
*/
195+
public function getExtraColumns()
196+
{
197+
return $this->extraColumns;
198+
}
199+
200+
}

src/LinkColumn.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class LinkColumn extends DataTableColumn
2020
public $searchable = false;
2121
public $orderable = false;
2222

23+
protected $filter = false;
24+
2325
public function init()
2426
{
2527
if (empty($this->linkOptions['id'])) {
@@ -35,5 +37,15 @@ public function init()
3537
link.attr("id", link.attr("id") + meta.row);link.attr("href", link.attr("href") + paramPrefix + jQuery.param(q));
3638
return link.get()[0].outerHTML;}');
3739
}
40+
41+
parent::init();
42+
}
43+
44+
/**
45+
* @return array
46+
*/
47+
public function getExtraColumns()
48+
{
49+
return array_unique(array_merge(parent::getExtraColumns(), $this->queryParams));
3850
}
3951
}

0 commit comments

Comments
 (0)