Skip to content

Commit 7d288f8

Browse files
committed
Add ability to have a fast-excel export callback.
1 parent bdaf02e commit 7d288f8

File tree

1 file changed

+51
-29
lines changed

1 file changed

+51
-29
lines changed

src/Services/DataTable.php

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@ abstract class DataTable implements DataTableButtons
125125
*/
126126
protected $fastExcel = false;
127127

128+
/**
129+
* Flag to enable/disable fast-excel callback.
130+
* Note: Disabling this flag can improve you export time.
131+
* Enabled by default to emulate the same output
132+
* with laravel-excel.
133+
*
134+
* @var bool
135+
*/
136+
protected $fastExcelCallback = true;
137+
128138
/**
129139
* Export class handler.
130140
*
@@ -402,8 +412,12 @@ public function response(callable $callback)
402412
*/
403413
public function excel()
404414
{
415+
set_time_limit(3600);
416+
405417
$ext = '.' . strtolower($this->excelWriter);
406-
$callback = $this->fastExcel ? $this->fastExcelCallback() : $this->excelWriter;
418+
$callback = $this->fastExcel ?
419+
($this->fastExcelCallback ? $this->fastExcelCallback() : null)
420+
: $this->excelWriter;
407421

408422
return $this->buildExcelFile()->download($this->getFilename() . $ext, $callback);
409423
}
@@ -416,27 +430,7 @@ public function excel()
416430
protected function buildExcelFile()
417431
{
418432
if ($this->fastExcel) {
419-
$query = null;
420-
if (method_exists($this, 'query')) {
421-
$query = app()->call([$this, 'query']);
422-
$query = $this->applyScopes($query);
423-
}
424-
425-
/** @var \Yajra\DataTables\DataTableAbstract $dataTable */
426-
$dataTable = app()->call([$this, 'dataTable'], compact('query'));
427-
$dataTable->skipPaging();
428-
429-
if ($dataTable instanceof QueryDataTable) {
430-
function queryGenerator($dataTable) {
431-
foreach ($dataTable->getFilteredQuery()->cursor() as $row) {
432-
yield $row;
433-
}
434-
}
435-
436-
return new FastExcel(queryGenerator($dataTable));
437-
}
438-
439-
return new FastExcel($this->convertToLazyCollection($dataTable->toArray()['data']));
433+
return $this->buildFastExcelFile();
440434
}
441435

442436
if ($this->exportClass != DataTablesExportHandler::class) {
@@ -500,7 +494,7 @@ protected function getDataForExport()
500494
*
501495
* @return array|string
502496
*/
503-
private function exportColumns()
497+
protected function exportColumns()
504498
{
505499
return is_array($this->exportColumns) ? $this->toColumnsCollection($this->exportColumns) : $this->getExportColumnsFromBuilder();
506500
}
@@ -536,8 +530,11 @@ private function toColumnsCollection(array $columns)
536530
*/
537531
public function csv()
538532
{
533+
set_time_limit(3600);
539534
$ext = '.' . strtolower($this->csvWriter);
540-
$callback = $this->fastExcel ? $this->fastExcelCallback() : $this->csvWriter;
535+
$callback = $this->fastExcel ?
536+
($this->fastExcelCallback ? $this->fastExcelCallback() : null)
537+
: $this->csvWriter;
541538

542539
return $this->buildExcelFile()->download($this->getFilename() . $ext, $callback);
543540
}
@@ -693,17 +690,42 @@ protected function convertToLazyCollection($collection)
693690
*/
694691
public function fastExcelCallback()
695692
{
696-
$columns = $this->exportColumns();
697-
698-
$callback = function ($row) use ($columns) {
693+
return function ($row) {
699694
$mapped = [];
700-
foreach ($columns as $column) {
695+
foreach ($this->exportColumns() as $column) {
701696
$mapped[$column['title']] = $row[$column['data']];
702697
}
703698

704699
return $mapped;
705700
};
701+
}
702+
703+
/**
704+
* @return \Rap2hpoutre\FastExcel\FastExcel
705+
*/
706+
protected function buildFastExcelFile()
707+
{
708+
$query = null;
709+
if (method_exists($this, 'query')) {
710+
$query = app()->call([$this, 'query']);
711+
$query = $this->applyScopes($query);
712+
}
713+
714+
/** @var \Yajra\DataTables\DataTableAbstract $dataTable */
715+
$dataTable = app()->call([$this, 'dataTable'], compact('query'));
716+
$dataTable->skipPaging();
717+
718+
if ($dataTable instanceof QueryDataTable) {
719+
function queryGenerator($dataTable)
720+
{
721+
foreach ($dataTable->getFilteredQuery()->cursor() as $row) {
722+
yield $row;
723+
}
724+
}
725+
726+
return new FastExcel(queryGenerator($dataTable));
727+
}
706728

707-
return $callback;
729+
return new FastExcel($this->convertToLazyCollection($dataTable->toArray()['data']));
708730
}
709731
}

0 commit comments

Comments
 (0)