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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ Column::make('id')->exportFormat(NumberFormat::FORMAT_GENERAL),
Column::make('id')->exportFormat(NumberFormat::FORMAT_TEXT),
```

## Auto Download

Option to automatically download the exported file.

```php
<livewire:export-button :table-id="$dataTable->getTableId()" filename="my-table.xlsx" auto-download="true"/>
```

## Contributing

Please see [CONTRIBUTING](https://github.com/yajra/laravel-datatables-export/blob/master/.github/CONTRIBUTING.md) for details.
Expand Down
14 changes: 13 additions & 1 deletion src/Livewire/ExportButtonComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class ExportButtonComponent extends Component

public string $sheetName = 'Sheet1';

public bool $autoDownload = false;

public bool $downloaded = false;

public bool $exporting = false;

public bool $exportFinished = false;
Expand All @@ -39,6 +43,7 @@ public function export(string $batchJobId): void
$this->exportFinished = false;
$this->exportFailed = false;
$this->exporting = true;
$this->downloaded = false;
}

public function getExportBatchProperty(): ?Batch
Expand All @@ -50,14 +55,21 @@ public function getExportBatchProperty(): ?Batch
return Bus::findBatch($this->batchJobId);
}

public function updateExportProgress(): void
public function updateExportProgress(): ?StreamedResponse
{
$this->exportFinished = $this->exportBatch->finished();
$this->exportFailed = $this->exportBatch->hasFailures();

if ($this->exportFinished) {
$this->exporting = false;
if ($this->autoDownload and ! $this->downloaded){
$this->downloaded = true;

return $this->downloadExport();
}
}

return null;
}

public function downloadExport(): StreamedResponse
Expand Down
6 changes: 5 additions & 1 deletion src/resources/views/export-button.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ class="{{ $class }}"
<div class="d-inline" wire:poll="updateExportProgress">Exporting...please wait.</div>
@endif

@if($exportFinished && !$exportFailed)
@if($exportFinished && !$exportFailed && !$autoDownload)
<span>Done. Download file <a href="#" class="text-primary" wire:click.prevent="downloadExport">here</a></span>
@endif

@if($exportFinished && !$exportFailed && $autoDownload && $downloaded)
<span>Done. File has been downloaded.</span>
@endif

@if($exportFailed)
<span>Export failed, please try again later.</span>
@endif
Expand Down