Skip to content

Commit 98c8744

Browse files
authored
Add auto download feature (#30)
* Add new feature auto-download. * Add auto-download in readme. * Add new line before return
1 parent f0a77b3 commit 98c8744

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,14 @@ Column::make('id')->exportFormat(NumberFormat::FORMAT_GENERAL),
180180
Column::make('id')->exportFormat(NumberFormat::FORMAT_TEXT),
181181
```
182182

183+
## Auto Download
184+
185+
Option to automatically download the exported file.
186+
187+
```php
188+
<livewire:export-button :table-id="$dataTable->getTableId()" filename="my-table.xlsx" auto-download="true"/>
189+
```
190+
183191
## Contributing
184192

185193
Please see [CONTRIBUTING](https://github.com/yajra/laravel-datatables-export/blob/master/.github/CONTRIBUTING.md) for details.

src/Livewire/ExportButtonComponent.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ class ExportButtonComponent extends Component
2525

2626
public string $sheetName = 'Sheet1';
2727

28+
public bool $autoDownload = false;
29+
30+
public bool $downloaded = false;
31+
2832
public bool $exporting = false;
2933

3034
public bool $exportFinished = false;
@@ -39,6 +43,7 @@ public function export(string $batchJobId): void
3943
$this->exportFinished = false;
4044
$this->exportFailed = false;
4145
$this->exporting = true;
46+
$this->downloaded = false;
4247
}
4348

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

53-
public function updateExportProgress(): void
58+
public function updateExportProgress(): ?StreamedResponse
5459
{
5560
$this->exportFinished = $this->exportBatch->finished();
5661
$this->exportFailed = $this->exportBatch->hasFailures();
5762

5863
if ($this->exportFinished) {
5964
$this->exporting = false;
65+
if ($this->autoDownload and ! $this->downloaded){
66+
$this->downloaded = true;
67+
68+
return $this->downloadExport();
69+
}
6070
}
71+
72+
return null;
6173
}
6274

6375
public function downloadExport(): StreamedResponse

src/resources/views/export-button.blade.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@ class="{{ $class }}"
2323
<div class="d-inline" wire:poll="updateExportProgress">Exporting...please wait.</div>
2424
@endif
2525

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

30+
@if($exportFinished && !$exportFailed && $autoDownload && $downloaded)
31+
<span>Done. File has been downloaded.</span>
32+
@endif
33+
3034
@if($exportFailed)
3135
<span>Export failed, please try again later.</span>
3236
@endif

0 commit comments

Comments
 (0)