From cbb3aac295fc2258b478b050bf66643b89646cd6 Mon Sep 17 00:00:00 2001 From: Patrick Yi Date: Sun, 14 May 2017 09:49:50 -0700 Subject: [PATCH 1/2] Add an option to decide to use $_POST or $_GET values depending on the request method --- src/DataTableAction.php | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/DataTableAction.php b/src/DataTableAction.php index 8f726e1..df3f48c 100644 --- a/src/DataTableAction.php +++ b/src/DataTableAction.php @@ -22,6 +22,13 @@ */ class DataTableAction extends Action { + /** + * GET or POST + * + * @var string + */ + public $requestMethod = "GET"; + /** * @var ActiveQuery */ @@ -50,24 +57,31 @@ public function init() } } + 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 { From 22d49a26c5f4753454767806990d03ff5a9aff62 Mon Sep 17 00:00:00 2001 From: Patrick Yi Date: Sun, 14 May 2017 10:23:53 -0700 Subject: [PATCH 2/2] Add formatData and formatResponse for post data formatting --- src/DataTableAction.php | 48 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/src/DataTableAction.php b/src/DataTableAction.php index df3f48c..90ce666 100644 --- a/src/DataTableAction.php +++ b/src/DataTableAction.php @@ -50,6 +50,22 @@ 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) { @@ -89,12 +105,13 @@ public function run() '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); } /** @@ -143,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; + } }