3
3
namespace Yajra \Datatables \Services ;
4
4
5
5
use Illuminate \Contracts \View \Factory ;
6
+ use Maatwebsite \Excel \Classes \LaravelExcelWorksheet ;
7
+ use Maatwebsite \Excel \Writers \LaravelExcelWriter ;
6
8
use Yajra \Datatables \Contracts \DataTableButtonsContract ;
7
9
use Yajra \Datatables \Contracts \DataTableContract ;
8
10
use Yajra \Datatables \Contracts \DataTableScopeContract ;
9
11
use Yajra \Datatables \Datatables ;
12
+ use Yajra \Datatables \Html \Column ;
10
13
11
14
abstract class DataTable implements DataTableContract, DataTableButtonsContract
12
15
{
@@ -44,7 +47,7 @@ abstract class DataTable implements DataTableContract, DataTableButtonsContract
44
47
/**
45
48
* Query scopes.
46
49
*
47
- * @var array
50
+ * @var \Yajra\Datatables\Contracts\DataTableScopeContract[]
48
51
*/
49
52
protected $ scopes = [];
50
53
@@ -68,7 +71,7 @@ public function __construct(Datatables $datatables, Factory $viewFactory)
68
71
*/
69
72
public function render ($ view , $ data = [], $ mergeData = [])
70
73
{
71
- if ($ this ->request ()->ajax () && $ this ->request ()->wantsJson ()) {
74
+ if ($ this ->request ()->ajax () && $ this ->request ()->wantsJson ()) {
72
75
return $ this ->ajax ();
73
76
}
74
77
@@ -90,6 +93,16 @@ public function render($view, $data = [], $mergeData = [])
90
93
}
91
94
}
92
95
96
+ /**
97
+ * Get Datatables Request instance.
98
+ *
99
+ * @return \Yajra\Datatables\Request
100
+ */
101
+ public function request ()
102
+ {
103
+ return $ this ->datatables ->getRequest ();
104
+ }
105
+
93
106
/**
94
107
* Export results to Excel file.
95
108
*
@@ -107,8 +120,8 @@ public function excel()
107
120
*/
108
121
protected function buildExcelFile ()
109
122
{
110
- return app ('excel ' )->create ($ this ->filename (), function ($ excel ) {
111
- $ excel ->sheet ('exported-data ' , function ($ sheet ) {
123
+ return app ('excel ' )->create ($ this ->filename (), function (LaravelExcelWriter $ excel ) {
124
+ $ excel ->sheet ('exported-data ' , function (LaravelExcelWorksheet $ sheet ) {
112
125
$ sheet ->fromArray ($ this ->getDataForExport ());
113
126
});
114
127
});
@@ -134,45 +147,88 @@ protected function getDataForExport()
134
147
$ decoratedData = $ this ->getAjaxResponseData ();
135
148
136
149
return array_map (function ($ row ) {
137
- if (is_array ( $ this ->exportColumns )) {
138
- return array_only ($ row , $ this -> exportColumns );
150
+ if ($ columns = $ this ->exportColumns ( )) {
151
+ return $ this -> buildExportColumn ($ row , $ columns );
139
152
}
140
153
141
154
return $ row ;
142
155
}, $ decoratedData );
143
156
}
144
157
145
158
/**
146
- * Get mapped columns versus final decorated output .
159
+ * Get decorated data as defined in datatables ajax response .
147
160
*
148
- * @return array
161
+ * @return mixed
149
162
*/
150
- protected function getDataForPrint ()
163
+ protected function getAjaxResponseData ()
151
164
{
152
- $ decoratedData = $ this ->getAjaxResponseData ( );
165
+ $ this ->datatables -> getRequest ()-> merge ([ ' length ' => - 1 ] );
153
166
154
- return array_map (function ($ row ) {
155
- if (is_array ($ this ->printColumns )) {
156
- return array_only ($ row , $ this ->printColumns );
157
- }
167
+ $ response = $ this ->ajax ();
168
+ $ data = $ response ->getData (true );
158
169
159
- return $ row ;
160
- }, $ decoratedData );
170
+ return $ data ['data ' ];
161
171
}
162
172
163
173
/**
164
- * Get decorated data as defined in datatables ajax response .
174
+ * Get export columns definition .
165
175
*
166
- * @return mixed
176
+ * @return array|string
167
177
*/
168
- protected function getAjaxResponseData ()
178
+ private function exportColumns ()
169
179
{
170
- $ this ->datatables ->getRequest ()->merge (['length ' => -1 ]);
180
+ return is_array ($ this ->exportColumns ) ? $ this ->exportColumns : $ this ->getColumnsFromBuilder ();
181
+ }
171
182
172
- $ response = $ this ->ajax ();
173
- $ data = $ response ->getData (true );
183
+ /**
184
+ * Get columns definition from html builder.
185
+ *
186
+ * @return array
187
+ */
188
+ private function getColumnsFromBuilder ()
189
+ {
190
+ return $ this ->html ()->getColumns ()->all ();
191
+ }
174
192
175
- return $ data ['data ' ];
193
+ /**
194
+ * Optional method if you want to use html builder.
195
+ *
196
+ * @return \Yajra\Datatables\Html\Builder
197
+ */
198
+ public function html ()
199
+ {
200
+ return $ this ->builder ();
201
+ }
202
+
203
+ /**
204
+ * Get Datatables Html Builder instance.
205
+ *
206
+ * @return \Yajra\Datatables\Html\Builder
207
+ */
208
+ public function builder ()
209
+ {
210
+ return $ this ->datatables ->getHtmlBuilder ();
211
+ }
212
+
213
+ /**
214
+ * @param array $row
215
+ * @param array|Column[] $columns
216
+ * @return array
217
+ */
218
+ protected function buildExportColumn (array $ row , array $ columns )
219
+ {
220
+ $ results = [];
221
+ foreach ($ columns as $ column ) {
222
+ if ($ column instanceof Column) {
223
+ if (! isset ($ column ['exportable ' ]) || $ column ['exportable ' ]) {
224
+ $ results [$ column ['title ' ]] = strip_tags (array_get ($ row , $ column ['name ' ]));
225
+ }
226
+ } else {
227
+ $ results [] = array_get ($ row , $ column );
228
+ }
229
+ }
230
+
231
+ return $ results ;
176
232
}
177
233
178
234
/**
@@ -209,33 +265,21 @@ public function printPreview()
209
265
}
210
266
211
267
/**
212
- * Optional method if you want to use html builder .
268
+ * Get mapped columns versus final decorated output .
213
269
*
214
- * @return mixed
270
+ * @return array
215
271
*/
216
- public function html ()
272
+ protected function getDataForPrint ()
217
273
{
218
- return $ this ->builder ();
219
- }
274
+ $ decoratedData = $ this ->getAjaxResponseData ();
220
275
221
- /**
222
- * Get Datatables Html Builder instance.
223
- *
224
- * @return \Yajra\Datatables\Html\Builder
225
- */
226
- public function builder ()
227
- {
228
- return $ this ->datatables ->getHtmlBuilder ();
229
- }
276
+ return array_map (function ($ row ) {
277
+ if (is_array ($ this ->printColumns )) {
278
+ return array_only ($ row , $ this ->printColumns );
279
+ }
230
280
231
- /**
232
- * Get Datatables Request instance.
233
- *
234
- * @return \Yajra\Datatables\Request
235
- */
236
- public function request ()
237
- {
238
- return $ this ->datatables ->getRequest ();
281
+ return $ row ;
282
+ }, $ decoratedData );
239
283
}
240
284
241
285
/**
@@ -265,4 +309,31 @@ public function applyScopes($query)
265
309
266
310
return $ query ;
267
311
}
312
+
313
+ /**
314
+ * Get default builder parameters.
315
+ *
316
+ * @return array
317
+ */
318
+ protected function getBuilderParameters ()
319
+ {
320
+ return [
321
+ 'order ' => [[0 , 'desc ' ]],
322
+ 'buttons ' => [
323
+ 'create ' ,
324
+ [
325
+ 'extend ' => 'collection ' ,
326
+ 'text ' => '<i class="fa fa-download"></i> Export ' ,
327
+ 'buttons ' => [
328
+ 'csv ' ,
329
+ 'excel ' ,
330
+ 'pdf ' ,
331
+ ],
332
+ ],
333
+ 'print ' ,
334
+ 'reset ' ,
335
+ 'reload ' ,
336
+ ],
337
+ ];
338
+ }
268
339
}
0 commit comments