Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
"mockery/mockery": "~1.0",
"phpunit/phpunit": "~7.0"
},
"suggest": {
"rap2hpoutre/fast-excel": "Faster exporting of dataTables using fast-excel package.",
"barryvdh/laravel-snappy": "For exporting of dataTables to PDF."
},
"autoload": {
"psr-4": {
"Yajra\\DataTables\\": "src/"
Expand Down
2 changes: 1 addition & 1 deletion src/Exports/DataTablesCollectionExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;

abstract class DataTablesCollectionExport implements FromCollection, WithHeadings
abstract class DataTablesCollectionExport implements FromCollection, WithHeadings
{
use Exportable;

Expand Down
117 changes: 105 additions & 12 deletions src/Services/DataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use Illuminate\Http\JsonResponse;
use Illuminate\Support\Collection;
use Rap2hpoutre\FastExcel\FastExcel;
use Yajra\DataTables\Contracts\DataTableButtons;
use Yajra\DataTables\Contracts\DataTableScope;
use Yajra\DataTables\Html\Column;
use Yajra\DataTables\QueryDataTable;
use Yajra\DataTables\Transformers\DataArrayTransformer;

abstract class DataTable implements DataTableButtons
Expand Down Expand Up @@ -116,6 +118,23 @@ abstract class DataTable implements DataTableButtons
*/
protected $request;

/**
* Flag to use fast-excel package for export.
*
* @var bool
*/
protected $fastExcel = false;

/**
* Flag to enable/disable fast-excel callback.
* Note: Disabling this flag can improve you export time.
* Enabled by default to emulate the same output
* with laravel-excel.
*
* @var bool
*/
protected $fastExcelCallback = true;

/**
* Export class handler.
*
Expand Down Expand Up @@ -393,9 +412,14 @@ public function response(callable $callback)
*/
public function excel()
{
$ext = '.' . strtolower($this->excelWriter);
set_time_limit(3600);

$ext = '.' . strtolower($this->excelWriter);
$callback = $this->fastExcel ?
($this->fastExcelCallback ? $this->fastExcelCallback() : null)
: $this->excelWriter;

return $this->buildExcelFile()->download($this->getFilename() . $ext, $this->excelWriter);
return $this->buildExcelFile()->download($this->getFilename() . $ext, $callback);
}

/**
Expand All @@ -405,17 +429,19 @@ public function excel()
*/
protected function buildExcelFile()
{
if ($this->exportClass != DataTablesExportHandler::class) {
$collection = collect($this->getAjaxResponseData());
} else {
$collection = collect($this->getDataForExport());
if ($this->fastExcel) {
return $this->buildFastExcelFile();
}

if (method_exists($collection, 'lazy')) {
$collection->lazy();
if ($this->exportClass != DataTablesExportHandler::class) {
$collection = $this->getAjaxResponseData();

return new $this->exportClass($this->convertToLazyCollection($collection));
}

return new $this->exportClass($collection);
$collection = $this->getDataForExport();

return new $this->exportClass($this->convertToLazyCollection($collection));
}

/**
Expand Down Expand Up @@ -468,7 +494,7 @@ protected function getDataForExport()
*
* @return array|string
*/
private function exportColumns()
protected function exportColumns()
{
return is_array($this->exportColumns) ? $this->toColumnsCollection($this->exportColumns) : $this->getExportColumnsFromBuilder();
}
Expand Down Expand Up @@ -504,9 +530,13 @@ private function toColumnsCollection(array $columns)
*/
public function csv()
{
$ext = '.' . strtolower($this->csvWriter);
set_time_limit(3600);
$ext = '.' . strtolower($this->csvWriter);
$callback = $this->fastExcel ?
($this->fastExcelCallback ? $this->fastExcelCallback() : null)
: $this->csvWriter;

return $this->buildExcelFile()->download($this->getFilename() . $ext, $this->csvWriter);
return $this->buildExcelFile()->download($this->getFilename() . $ext, $callback);
}

/**
Expand Down Expand Up @@ -637,4 +667,67 @@ protected function getBuilderParameters()
{
return config('datatables-buttons.parameters');
}

/**
* @param \Illuminate\Support\|array $collection
* @return \Illuminate\Support\Collection
*/
protected function convertToLazyCollection($collection)
{
if (is_array($collection)) {
$collection = collect($collection);
}

if (method_exists($collection, 'lazy')) {
$collection->lazy();
}

return $collection;
}

/**
* @return \Closure
*/
public function fastExcelCallback()
{
return function ($row) {
$mapped = [];
foreach ($this->exportColumns() as $column) {
if ($column['exportable']) {
$mapped[$column['title']] = $row[$column['name']];
}
}

return $mapped;
};
}

/**
* @return \Rap2hpoutre\FastExcel\FastExcel
*/
protected function buildFastExcelFile()
{
$query = null;
if (method_exists($this, 'query')) {
$query = app()->call([$this, 'query']);
$query = $this->applyScopes($query);
}

/** @var \Yajra\DataTables\DataTableAbstract $dataTable */
$dataTable = app()->call([$this, 'dataTable'], compact('query'));
$dataTable->skipPaging();

if ($dataTable instanceof QueryDataTable) {
function queryGenerator($dataTable)
{
foreach ($dataTable->getFilteredQuery()->cursor() as $row) {
yield $row;
}
}

return new FastExcel(queryGenerator($dataTable));
}

return new FastExcel($this->convertToLazyCollection($dataTable->toArray()['data']));
}
}