Skip to content

Commit 554162f

Browse files
authored
DataTableAction query parameter can be Closure. (#71)
* DataTableAction query parameter can be Closure. * Update README with DataTableAction query example. * Add description about v1.1.3 to CHANGELOG.
1 parent 5205ba6 commit 554162f

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

CHANGELOG.md

+5-1
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.3
6+
### Added
7+
- `DataTableAction` `query` parameter can be `Closure`
8+
59
## v1.1.2
610
### Added
711
- Support for bootstrap 4/5 (autodetect required bootstrap version)
@@ -46,4 +50,4 @@ This project adheres to [Semantic Versioning](http://semver.org/).
4650
### Changed
4751
- Move DataTable options to protected array. Add __set and __get methods.
4852

49-
[#52]: https://github.com/NullRefExcep/yii2-datatables/issues/52
53+
[#52]: https://github.com/NullRefExcep/yii2-datatables/issues/52

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,17 @@ public function actions()
286286

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

289+
You may also specify a closure for `query` in `DataTableAction` config if you need complex query like in the following code:
290+
```php
291+
/** ... */
292+
'query' => function() {
293+
$calculatedValue = calculate_value_for_query();
294+
295+
return Model::find()->where(['calculated_value' => $calculatedValue]);
296+
},
297+
/** ... */
298+
```
299+
289300
And add options to widget:
290301

291302
```php

composer.json

+6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
"email": "[email protected]",
2929
"homepage": "https://github.com/Radon8472",
3030
"role": "Contributor"
31+
},
32+
{
33+
"name": "Oleksandr Lynnyk",
34+
"email": "[email protected]",
35+
"homepage": "https://github.com/alxlnk",
36+
"role": "Contributor"
3137
}
3238
],
3339
"minimum-stability": "stable",

src/DataTableAction.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace nullref\datatable;
99

1010

11+
use Closure;
1112
use Yii;
1213
use yii\base\Action;
1314
use yii\base\InvalidConfigException;
@@ -37,7 +38,7 @@ class DataTableAction extends Action
3738
public $requestMethod = self::REQUEST_METHOD_GET;
3839

3940
/**
40-
* @var ActiveQuery
41+
* @var ActiveQuery|Closure
4142
*/
4243
public $query;
4344

@@ -91,6 +92,10 @@ public function init()
9192
throw new InvalidConfigException(get_class($this) . '::$query must be set.');
9293
}
9394

95+
if ($this->query instanceof Closure) {
96+
$this->query = call_user_func($this->query);
97+
}
98+
9499
if ($this->formatData === null) {
95100
$this->formatData = function ($query, $columns) {
96101
$rows = [];
@@ -249,4 +254,4 @@ public function formatResponse($response)
249254

250255
return $response;
251256
}
252-
}
257+
}

0 commit comments

Comments
 (0)