Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 66 additions & 8 deletions src/DataTableAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
*/
class DataTableAction extends Action
{
/**
* GET or POST
*
* @var string
*/
public $requestMethod = "GET";

/**
* @var ActiveQuery
*/
Expand All @@ -43,44 +50,68 @@ class DataTableAction extends Action
*/
public $applyFilter;

/**
* Format data
* Signature is following:
* function ($query, $columns)
* @var callable
*/
public $formatData;

/**
* Format response
* Signature is following:
* function ($response)
* @var callable
*/
public $formatResponse;

public function init()
{
if ($this->query === null) {
throw new InvalidConfigException(get_class($this) . '::$query must be set.');
}
}

protected function getParam($name, $defaultValue = null)
{
return $this->requestMethod == 'GET' ?
Yii::$app->request->getQueryParam($name, $defaultValue) :
Yii::$app->request->getBodyParam($name, $defaultValue);
}

public function run()
{
/** @var ActiveQuery $originalQuery */
$originalQuery = $this->query;
$filterQuery = clone $originalQuery;
$draw = Yii::$app->request->getQueryParam('draw');
$draw = $this->getParam('draw');
$filterQuery->where = null;
$search = Yii::$app->request->getQueryParam('search', ['value' => null, 'regex' => false]);
$columns = Yii::$app->request->getQueryParam('columns', []);
$order = Yii::$app->request->getQueryParam('order', []);
$search = $this->getParam('search', ['value' => null, 'regex' => false]);
$columns = $this->getParam('columns', []);
$order = $this->getParam('order', []);
$filterQuery = $this->applyFilter($filterQuery, $columns, $search);
$filterQuery = $this->applyOrder($filterQuery, $columns, $order);
if (!empty($originalQuery->where)) {
$filterQuery->andWhere($originalQuery->where);
}
$filterQuery
->offset(Yii::$app->request->getQueryParam('start', 0))
->limit(Yii::$app->request->getQueryParam('length', -1));
->offset($this->getParam('start', 0))
->limit($this->getParam('length', -1));
$dataProvider = new ActiveDataProvider(['query' => $filterQuery, 'pagination' => ['pageSize' => Yii::$app->request->getQueryParam('length', 10)]]);
Yii::$app->response->format = Response::FORMAT_JSON;
try {
$response = [
'draw' => (int)$draw,
'recordsTotal' => (int)$originalQuery->count(),
'recordsFiltered' => (int)$dataProvider->getTotalCount(),
'data' => $filterQuery->all(),
'data' => $this->formatData($filterQuery, $columns),
];
} catch (\Exception $e) {
return ['error' => $e->getMessage()];
}
return $response;

return $this->formatResponse($response);
}

/**
Expand Down Expand Up @@ -129,4 +160,31 @@ public function applyOrder(ActiveQuery $query, $columns, $order)
}
return $query;
}

/**
* @param ActiveQuery $query
* @param array $columns
* @return ActiveQuery
*/
public function formatData(ActiveQuery $query, $columns)
{
if ($this->formatData !== null) {
return call_user_func($this->formatData, $query, $columns);
}

return $query->all();
}

/**
* @param array $response
* @return ActiveQuery
*/
public function formatResponse($response)
{
if ($this->formatResponse !== null) {
return call_user_func($this->formatResponse, $response);
}

return $response;
}
}