Skip to content

Commit 62c153b

Browse files
committed
Enhance export function to match what is displayed in UI.
Enhance datatables service stub. Add option to set id as exportable and printable.
1 parent 354c043 commit 62c153b

File tree

4 files changed

+156
-73
lines changed

4 files changed

+156
-73
lines changed

src/Generators/stubs/datatables.stub

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
<?php
22

3-
namespace DummyNamespace;
3+
namespace LOD\DataTables;
44

55
use App\User;
66
use Yajra\Datatables\Services\DataTable;
77

8-
class DummyClass extends DataTable
8+
class PoliciesDataTable extends DataTable
99
{
1010
// protected $printPreview = 'path.to.print.preview.view';
11-
// protected $exportColumns = ['id', 'name'];
1211
// protected $printColumns = '*';
1312

1413
/**
@@ -44,30 +43,24 @@ class DummyClass extends DataTable
4443
public function html()
4544
{
4645
return $this->builder()
47-
->columns([
48-
'id',
49-
// add columns to display
50-
'created_at',
51-
'updated_at',
52-
])
53-
->addAction(['width' => '50px'])
54-
->ajax('')
55-
->parameters([
56-
'buttons' => [
57-
'create',
58-
[
59-
'extend' => 'collection',
60-
'text' => '<i class="fa fa-download"></i> Export',
61-
'buttons' => [
62-
'csv',
63-
'excel',
64-
'pdf',
65-
],
66-
],
67-
'print',
68-
'reset',
69-
'reload',
70-
],
71-
]);
46+
->columns($this->getColumns())
47+
->ajax('')
48+
->addAction(['width' => '80px'])
49+
->parameters($this->getBuilderParameters());
50+
}
51+
52+
/**
53+
* Get columns.
54+
*
55+
* @return array
56+
*/
57+
private function getColumns()
58+
{
59+
return [
60+
'id',
61+
// add your columns
62+
'created_at',
63+
'updated_at',
64+
];
7265
}
7366
}

src/Html/Builder.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ public function parameterize($attributes = [])
137137
$column_functions = [];
138138

139139
foreach ($parameters['columns'] as $i => $column) {
140+
unset($parameters['columns'][$i]['exportable']);
141+
unset($parameters['columns'][$i]['printable']);
142+
140143
if (isset($column['render'])) {
141144
$column_functions[$i] = $column['render'];
142145
$parameters['columns'][$i]['render'] = "#column_function.{$i}#";
@@ -258,6 +261,8 @@ public function addCheckbox(array $attributes = [])
258261
'name' => 'checkbox',
259262
'orderable' => false,
260263
'searchable' => false,
264+
'exportable' => false,
265+
'printable' => false,
261266
'width' => '10px',
262267
], $attributes
263268
);
@@ -283,6 +288,8 @@ public function addAction(array $attributes = [])
283288
'render' => null,
284289
'orderable' => false,
285290
'searchable' => false,
291+
'exportable' => false,
292+
'printable' => false,
286293
], $attributes
287294
);
288295
$this->collection->push(new Column($attributes));
@@ -341,4 +348,14 @@ public function setTemplate($template)
341348

342349
return $this;
343350
}
351+
352+
/**
353+
* Get collection of columns.
354+
*
355+
* @return Collection
356+
*/
357+
public function getColumns()
358+
{
359+
return $this->collection;
360+
}
344361
}

src/Html/Column.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public function __construct($attributes = [])
1919
{
2020
$attributes['orderable'] = isset($attributes['orderable']) ? $attributes['orderable'] : true;
2121
$attributes['searchable'] = isset($attributes['searchable']) ? $attributes['searchable'] : true;
22+
$attributes['exportable'] = isset($attributes['exportable']) ? $attributes['exportable'] : true;
23+
$attributes['printable'] = isset($attributes['printable']) ? $attributes['printable'] : true;
2224

2325
// Allow methods override attribute value
2426
foreach ($attributes as $attribute => $value) {

src/Services/DataTable.php

Lines changed: 116 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
namespace Yajra\Datatables\Services;
44

55
use Illuminate\Contracts\View\Factory;
6+
use Maatwebsite\Excel\Classes\LaravelExcelWorksheet;
7+
use Maatwebsite\Excel\Writers\LaravelExcelWriter;
68
use Yajra\Datatables\Contracts\DataTableButtonsContract;
79
use Yajra\Datatables\Contracts\DataTableContract;
810
use Yajra\Datatables\Contracts\DataTableScopeContract;
911
use Yajra\Datatables\Datatables;
12+
use Yajra\Datatables\Html\Column;
1013

1114
abstract 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

Comments
 (0)