Skip to content

Commit 6d93492

Browse files
authored
Merge pull request #1 from yajra/develop
0.1.0 Initial version.
2 parents 1cdac4b + 4b0418f commit 6d93492

File tree

6 files changed

+56
-25
lines changed

6 files changed

+56
-25
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ php artisan migrate
4848
1. Add the export-button livewire component on your view file that uses dataTable class.
4949

5050
```phpt
51-
<livewire:export-button :table-id="$dataTable->getTableAttribute('id')" />
51+
<livewire:export-button :table-id="$dataTable->getTableId()" />
5252
```
5353

5454
2. On your `DataTable` class instance, use `WithExportQueue`
@@ -66,6 +66,27 @@ class PermissionsDataTable extends DataTable
6666

6767
3. Run your queue worker via `php artisan queue:work`.
6868

69+
## Export Filename
70+
71+
You can set the export filename by setting the property.
72+
73+
```phpt
74+
<livewire:export-button :table-id="$dataTable->getTableId()" filename="my-table.xlsx" />
75+
<livewire:export-button :table-id="$dataTable->getTableId()" filename="my-table.csv" />
76+
77+
<livewire:export-button :table-id="$dataTable->getTableId()" :filename="$filename" />
78+
```
79+
80+
## Export Type
81+
82+
You can set the export type by setting the property to `csv` or `xlsx`. Default value is `csv`.
83+
84+
```phpt
85+
<livewire:export-button :table-id="$dataTable->getTableId()" type="xlsx" />
86+
<livewire:export-button :table-id="$dataTable->getTableId()" type="csv" />
87+
```
88+
89+
6990
## Contributing
7091

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

composer.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"laravel",
66
"datatables",
77
"export",
8-
"jquery"
8+
"excel",
9+
"livewire",
10+
"queue"
911
],
1012
"license": "MIT",
1113
"authors": [
@@ -17,9 +19,8 @@
1719
"require": {
1820
"php": ">=7.4|8.*",
1921
"yajra/laravel-datatables-buttons": "4.*",
20-
"maatwebsite/excel": "^3.0",
21-
"livewire/livewire": "^2.6",
22-
"laravel/framework": "^8.61"
22+
"yajra/laravel-datatables-html": "^4.40",
23+
"livewire/livewire": "2.*"
2324
},
2425
"require-dev": {
2526
"phpunit/phpunit": "^9.5.9"

src/Exports/DataTableQueuedExport.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function map($row): array
3131
{
3232
return $this->columns
3333
->map(function (Column $column) use ($row) {
34-
return $row[$column['name']];
34+
return $row[$column['data']];
3535
})
3636
->toArray();
3737
}

src/Livewire/ExportButtonComponent.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
use Illuminate\Support\Facades\Bus;
66
use Illuminate\Support\Facades\Storage;
7+
use Illuminate\Support\Str;
78
use Livewire\Component;
89

910
class ExportButtonComponent extends Component
1011
{
12+
public $class = 'btn btn-primary';
1113
public $tableId;
1214
public $type = 'csv';
1315
public $filename = null;
@@ -46,19 +48,31 @@ public function updateExportProgress()
4648

4749
public function downloadExport()
4850
{
49-
return Storage::download('exports/'.$this->batchJobId.'.'.$this->getType(), $this->filename);
51+
return Storage::download('exports/'.$this->batchJobId.'.'.$this->getType(), $this->getFilename());
5052
}
5153

5254
public function render()
5355
{
54-
return view('datatables-export::export-button');
56+
return view('datatables-export::export-button', [
57+
'fileType' => $this->getType()
58+
]);
5559
}
5660

57-
/**
58-
* @return string
59-
*/
6061
protected function getType(): string
6162
{
63+
if (Str::endsWith($this->filename, ['csv', 'xlsx'])) {
64+
return pathinfo($this->filename, PATHINFO_EXTENSION);
65+
}
66+
6267
return $this->type == 'csv' ? 'csv' : 'xlsx';
6368
}
69+
70+
protected function getFilename()
71+
{
72+
if (Str::endsWith($this->filename, ['csv', 'xlsx'])) {
73+
return $this->filename;
74+
}
75+
76+
return null;
77+
}
6478
}

src/WithExportQueue.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Illuminate\Support\Facades\Bus;
66
use Yajra\DataTables\Jobs\DataTableExportJob;
7-
use Yajra\DataTables\Services\DataTable;
87

98
trait WithExportQueue
109
{
@@ -18,19 +17,11 @@ trait WithExportQueue
1817
*/
1918
public function render($view, $data = [], $mergeData = [])
2019
{
21-
if ($this->request()->ajax() && $this->request()->wantsJson()) {
22-
return app()->call([$this, 'ajax']);
20+
if (! $this->request()->wantsJson() && $this->request()->get('action') == 'exportQueue') {
21+
return $this->exportQueue();
2322
}
2423

25-
if ($action = $this->request()->get('action') and in_array($action, array_merge($this->actions, ['exportQueue']))) {
26-
if ($action == 'print') {
27-
return app()->call([$this, 'printPreview']);
28-
}
29-
30-
return app()->call([$this, $action]);
31-
}
32-
33-
return view($view, $data, $mergeData)->with($this->dataTableVariable, $this->getHtmlBuilder());
24+
return parent::render($view, $data, $mergeData);
3425
}
3526

3627
/**

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@
33
x-on:submit.prevent="
44
$refs.exportBtn.disabled = true;
55
var url = window._buildUrl(LaravelDataTables['{{ $tableId }}'], 'exportQueue');
6-
$.get(url + '&exportType={{$type}}').then(function(exportId) {
6+
$.get(url + '&exportType={{$fileType}}').then(function(exportId) {
77
$wire.export(exportId)
8+
}).catch(function(error) {
9+
$wire.exportFinished = true;
10+
$wire.exporting = false;
11+
$wire.exportFailed = true;
812
});
913
"
1014
>
1115
<button type="submit"
1216
x-ref="exportBtn"
1317
:disabled="$wire.exporting"
14-
class="btn btn-primary"
18+
class="{{ $class }}"
1519
>Export</button>
1620
</form>
1721

0 commit comments

Comments
 (0)