@@ -50,6 +50,22 @@ class DataTableAction extends Action
50
50
*/
51
51
public $ applyFilter ;
52
52
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
+
53
69
public function init ()
54
70
{
55
71
if ($ this ->query === null ) {
@@ -89,12 +105,13 @@ public function run()
89
105
'draw ' => (int )$ draw ,
90
106
'recordsTotal ' => (int )$ originalQuery ->count (),
91
107
'recordsFiltered ' => (int )$ dataProvider ->getTotalCount (),
92
- 'data ' => $ filterQuery -> all ( ),
108
+ 'data ' => $ this -> formatData ( $ filterQuery , $ columns ),
93
109
];
94
110
} catch (\Exception $ e ) {
95
111
return ['error ' => $ e ->getMessage ()];
96
112
}
97
- return $ response ;
113
+
114
+ return $ this ->formatResponse ($ response );
98
115
}
99
116
100
117
/**
@@ -143,4 +160,31 @@ public function applyOrder(ActiveQuery $query, $columns, $order)
143
160
}
144
161
return $ query ;
145
162
}
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
+ }
146
190
}
0 commit comments