Skip to content

Commit 1d95b5f

Browse files
committed
Enhance printing function to match what is displayed in UI.
1 parent 62c153b commit 1d95b5f

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/Generators/stubs/datatables.stub

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use Yajra\Datatables\Services\DataTable;
88
class PoliciesDataTable extends DataTable
99
{
1010
// protected $printPreview = 'path.to.print.preview.view';
11-
// protected $printColumns = '*';
1211

1312
/**
1413
* Display ajax response.

src/Services/DataTable.php

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ protected function buildExportColumn(array $row, array $columns)
220220
$results = [];
221221
foreach ($columns as $column) {
222222
if ($column instanceof Column) {
223-
if (! isset($column['exportable']) || $column['exportable']) {
223+
if ($column['exportable']) {
224224
$results[$column['title']] = strip_tags(array_get($row, $column['name']));
225225
}
226226
} else {
@@ -274,14 +274,47 @@ protected function getDataForPrint()
274274
$decoratedData = $this->getAjaxResponseData();
275275

276276
return array_map(function ($row) {
277-
if (is_array($this->printColumns)) {
278-
return array_only($row, $this->printColumns);
277+
if ($columns = $this->printColumns()) {
278+
return $this->buildPrintColumn($row, $columns);
279279
}
280280

281281
return $row;
282282
}, $decoratedData);
283283
}
284284

285+
/**
286+
* Get printable columns.
287+
*
288+
* @return array|string
289+
*/
290+
protected function printColumns()
291+
{
292+
return is_array($this->printColumns) ? $this->printColumns : $this->getColumnsFromBuilder();
293+
}
294+
295+
/**
296+
* Build printable column.
297+
*
298+
* @param array $row
299+
* @param array|Column[] $columns
300+
* @return array
301+
*/
302+
protected function buildPrintColumn(array $row, array $columns)
303+
{
304+
$results = [];
305+
foreach ($columns as $column) {
306+
if ($column instanceof Column) {
307+
if ($column['printable']) {
308+
$results[$column['title']] = array_get($row, $column['name']);
309+
}
310+
} else {
311+
$results[] = array_get($row, $column);
312+
}
313+
}
314+
315+
return $results;
316+
}
317+
285318
/**
286319
* Add basic array query scopes.
287320
*

0 commit comments

Comments
 (0)