Skip to content

Commit 22d49a2

Browse files
committed
Add formatData and formatResponse for post data formatting
1 parent cbb3aac commit 22d49a2

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

src/DataTableAction.php

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@ class DataTableAction extends Action
5050
*/
5151
public $applyFilter;
5252

53+
/**
54+
* Format data
55+
* Signature is following:
56+
* function ($query, $columns)
57+
* @var callable
58+
*/
59+
public $formatData;
60+
61+
/**
62+
* Format response
63+
* Signature is following:
64+
* function ($response)
65+
* @var callable
66+
*/
67+
public $formatResponse;
68+
5369
public function init()
5470
{
5571
if ($this->query === null) {
@@ -89,12 +105,13 @@ public function run()
89105
'draw' => (int)$draw,
90106
'recordsTotal' => (int)$originalQuery->count(),
91107
'recordsFiltered' => (int)$dataProvider->getTotalCount(),
92-
'data' => $filterQuery->all(),
108+
'data' => $this->formatData($filterQuery, $columns),
93109
];
94110
} catch (\Exception $e) {
95111
return ['error' => $e->getMessage()];
96112
}
97-
return $response;
113+
114+
return $this->formatResponse($response);
98115
}
99116

100117
/**
@@ -143,4 +160,31 @@ public function applyOrder(ActiveQuery $query, $columns, $order)
143160
}
144161
return $query;
145162
}
163+
164+
/**
165+
* @param ActiveQuery $query
166+
* @param array $columns
167+
* @return ActiveQuery
168+
*/
169+
public function formatData(ActiveQuery $query, $columns)
170+
{
171+
if ($this->formatData !== null) {
172+
return call_user_func($this->formatData, $query, $columns);
173+
}
174+
175+
return $query->all();
176+
}
177+
178+
/**
179+
* @param array $response
180+
* @return ActiveQuery
181+
*/
182+
public function formatResponse($response)
183+
{
184+
if ($this->formatResponse !== null) {
185+
return call_user_func($this->formatResponse, $response);
186+
}
187+
188+
return $response;
189+
}
146190
}

0 commit comments

Comments
 (0)