33namespace Yajra \Datatables \Services ;
44
55use Illuminate \Contracts \View \Factory ;
6+ use Maatwebsite \Excel \Classes \LaravelExcelWorksheet ;
7+ use Maatwebsite \Excel \Writers \LaravelExcelWriter ;
68use Yajra \Datatables \Contracts \DataTableButtonsContract ;
79use Yajra \Datatables \Contracts \DataTableContract ;
810use Yajra \Datatables \Contracts \DataTableScopeContract ;
911use Yajra \Datatables \Datatables ;
12+ use Yajra \Datatables \Html \Column ;
1013
1114abstract class DataTable implements DataTableContract, DataTableButtonsContract
1215{
@@ -44,7 +47,7 @@ abstract class DataTable implements DataTableContract, DataTableButtonsContract
4447 /**
4548 * Query scopes.
4649 *
47- * @var array
50+ * @var \Yajra\Datatables\Contracts\DataTableScopeContract[]
4851 */
4952 protected $ scopes = [];
5053
@@ -68,7 +71,7 @@ public function __construct(Datatables $datatables, Factory $viewFactory)
6871 */
6972 public function render ($ view , $ data = [], $ mergeData = [])
7073 {
71- if ($ this ->request ()->ajax () && $ this ->request ()->wantsJson ()) {
74+ if ($ this ->request ()->ajax () && $ this ->request ()->wantsJson ()) {
7275 return $ this ->ajax ();
7376 }
7477
@@ -90,6 +93,16 @@ public function render($view, $data = [], $mergeData = [])
9093 }
9194 }
9295
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+
93106 /**
94107 * Export results to Excel file.
95108 *
@@ -107,8 +120,8 @@ public function excel()
107120 */
108121 protected function buildExcelFile ()
109122 {
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 ) {
112125 $ sheet ->fromArray ($ this ->getDataForExport ());
113126 });
114127 });
@@ -134,45 +147,88 @@ protected function getDataForExport()
134147 $ decoratedData = $ this ->getAjaxResponseData ();
135148
136149 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 );
139152 }
140153
141154 return $ row ;
142155 }, $ decoratedData );
143156 }
144157
145158 /**
146- * Get mapped columns versus final decorated output .
159+ * Get decorated data as defined in datatables ajax response .
147160 *
148- * @return array
161+ * @return mixed
149162 */
150- protected function getDataForPrint ()
163+ protected function getAjaxResponseData ()
151164 {
152- $ decoratedData = $ this ->getAjaxResponseData ( );
165+ $ this ->datatables -> getRequest ()-> merge ([ ' length ' => - 1 ] );
153166
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 );
158169
159- return $ row ;
160- }, $ decoratedData );
170+ return $ data ['data ' ];
161171 }
162172
163173 /**
164- * Get decorated data as defined in datatables ajax response .
174+ * Get export columns definition .
165175 *
166- * @return mixed
176+ * @return array|string
167177 */
168- protected function getAjaxResponseData ()
178+ private function exportColumns ()
169179 {
170- $ this ->datatables ->getRequest ()->merge (['length ' => -1 ]);
180+ return is_array ($ this ->exportColumns ) ? $ this ->exportColumns : $ this ->getColumnsFromBuilder ();
181+ }
171182
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+ }
174192
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 ;
176232 }
177233
178234 /**
@@ -209,33 +265,21 @@ public function printPreview()
209265 }
210266
211267 /**
212- * Optional method if you want to use html builder .
268+ * Get mapped columns versus final decorated output .
213269 *
214- * @return mixed
270+ * @return array
215271 */
216- public function html ()
272+ protected function getDataForPrint ()
217273 {
218- return $ this ->builder ();
219- }
274+ $ decoratedData = $ this ->getAjaxResponseData ();
220275
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+ }
230280
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 );
239283 }
240284
241285 /**
@@ -265,4 +309,31 @@ public function applyScopes($query)
265309
266310 return $ query ;
267311 }
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+ }
268339}
0 commit comments