Skip to content

Commit 86befce

Browse files
committed
Exports
- Added export functionality powered my maatwebsite/excel - Added config item to override exporter class - Added a new translation string - Added a new export dropdown with export choices chosen from component classes $exports array - Export to either csv, xls, or xlsx - Add new option to add class to export button - Added callback to column to be able to format the column differently for the exports - Added a flag to omit a column from an export
1 parent bb15993 commit 86befce

File tree

12 files changed

+273
-1
lines changed

12 files changed

+273
-1
lines changed

config/config.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
return [
44

5+
/*
6+
* The class to use to handle the export functionality
7+
*/
8+
'exports' => \Rappasoft\LaravelLivewireTables\Exports\Export::class,
9+
510
/*
611
* The frontend styling framework to use
712
* Options: bootstrap-4

resources/lang/ar/strings.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
return [
44
'clear' => 'مسح',
5+
'export' => 'Export',
56
'loading' => 'تحميل...',
67
'no_results' => 'لا توجد نتائج لعرضها لهذا الاستعلام.',
78
'offline' => 'أنت غير متصل حاليا بالإنترنت.',

resources/lang/en/strings.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
return [
44
'clear' => 'Clear',
5+
'export' => 'Export',
56
'loading' => 'Loading...',
67
'no_results' => 'There are no results to display for this query.',
78
'offline' => 'You are not currently connected to the internet.',
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
@if (count($exports))
2+
<div class="dropdown table-export">
3+
<button class="dropdown-toggle {{ $this->getOption('bootstrap.classes.buttons.export') }}" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
4+
@lang('laravel-livewire-tables::strings.export')
5+
</button>
6+
7+
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
8+
@if (in_array('csv', $exports, true))
9+
<a class="dropdown-item" href="#" wire:click.prevent="export('csv')">CSV</a>
10+
@endif
11+
12+
@if (in_array('xls', $exports, true))
13+
<a class="dropdown-item" href="#" wire:click.prevent="export('xls')">XLS</a>
14+
@endif
15+
16+
@if (in_array('xlsx', $exports, true))
17+
<a class="dropdown-item" href="#" wire:click.prevent="export('xlsx')">XLSX</a>
18+
@endif
19+
</div>
20+
</div><!--export-dropdown-->
21+
@endif

resources/views/bootstrap-4/includes/options.blade.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,7 @@ class="form-control"
3333
@endif
3434
</div>
3535
@endif
36+
37+
@include('laravel-livewire-tables::'.config('laravel-livewire-tables.theme').'.includes.export')
3638
</div><!--row-->
3739
@endif
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Exceptions;
4+
5+
use Exception;
6+
7+
class UnsupportedExportFormatException extends Exception {}

src/Exports/Export.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Exports;
4+
5+
use Illuminate\Database\Eloquent\Builder;
6+
use Maatwebsite\Excel\Concerns\Exportable;
7+
use Maatwebsite\Excel\Concerns\FromQuery;
8+
use Maatwebsite\Excel\Concerns\WithHeadings;
9+
use Maatwebsite\Excel\Concerns\WithMapping;
10+
use Rappasoft\LaravelLivewireTables\Traits\ExportHelper;
11+
12+
/**
13+
* Class CSVExport
14+
*
15+
* @package Rappasoft\LaravelLivewireTables\Exports
16+
*/
17+
class Export implements FromQuery, WithHeadings, WithMapping
18+
{
19+
20+
use Exportable, ExportHelper;
21+
22+
/**
23+
* @var array
24+
*/
25+
public $builder;
26+
27+
/**
28+
* @var array
29+
*/
30+
public $columns;
31+
32+
/**
33+
* CSVExport constructor.
34+
*
35+
* @param Builder $builder
36+
* @param array $columns
37+
*/
38+
public function __construct(Builder $builder, array $columns = []) {
39+
$this->builder = $builder;
40+
$this->columns = $columns;
41+
}
42+
43+
/**
44+
* @return array|\Illuminate\Database\Query\Builder
45+
*/
46+
public function query()
47+
{
48+
return $this->builder;
49+
}
50+
51+
/**
52+
* @return array
53+
*/
54+
public function headings(): array
55+
{
56+
return $this->getHeadingRow();
57+
}
58+
59+
/**
60+
* @param mixed $row
61+
*
62+
* @return array
63+
*/
64+
public function map($row): array
65+
{
66+
$map = [];
67+
68+
foreach ($this->columns as $column) {
69+
if ($column->isVisible() && $column->includedInExport()) {
70+
if ($column->isFormatted()) {
71+
if ($column->hasExportFormat()) {
72+
$map[] = $column->formattedForExport($row, $column);
73+
} else {
74+
$map[] = strip_tags($column->formatted($row, $column));
75+
}
76+
} else {
77+
$map[] = data_get($row, $column->getAttribute());
78+
}
79+
}
80+
}
81+
82+
return $map;
83+
}
84+
}

src/TableComponent.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\View\View;
88
use Livewire\Component;
99
use Livewire\WithPagination;
10+
use Rappasoft\LaravelLivewireTables\Traits\Exports;
1011
use Rappasoft\LaravelLivewireTables\Traits\Loading;
1112
use Rappasoft\LaravelLivewireTables\Traits\Options;
1213
use Rappasoft\LaravelLivewireTables\Traits\Pagination;
@@ -20,7 +21,8 @@
2021
*/
2122
abstract class TableComponent extends Component
2223
{
23-
use Loading,
24+
use Exports,
25+
Loading,
2426
Options,
2527
Pagination,
2628
Search,

src/Traits/ExportHelper.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Traits;
4+
5+
/**
6+
* Trait ExportHelper.
7+
*/
8+
trait ExportHelper
9+
{
10+
11+
/**
12+
* @return array
13+
*/
14+
public function getHeadingRow(): array {
15+
$headers = [];
16+
17+
foreach ($this->columns as $column) {
18+
if ($column->isVisible() && $column->includedInExport()) {
19+
$headers[] = $column->getText();
20+
}
21+
}
22+
23+
return $headers;
24+
}
25+
}

src/Traits/Exports.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Traits;
4+
5+
use Exception;
6+
use Maatwebsite\Excel\Excel;
7+
use Rappasoft\LaravelLivewireTables\Exceptions\UnsupportedExportFormatException;
8+
9+
/**
10+
* Trait Exports.
11+
*/
12+
trait Exports
13+
{
14+
15+
/**
16+
* @var string
17+
*/
18+
public $exportFileName = 'data';
19+
20+
/**
21+
* @var bool
22+
*/
23+
public $exports = [];
24+
25+
/**
26+
* @param $type
27+
*
28+
* @return mixed
29+
* @throws Exception
30+
*/
31+
public function export($type) {
32+
$type = strtolower($type);
33+
34+
if (!in_array($type, ['csv', 'xls', 'xlsx'], true)) {
35+
throw new UnsupportedExportFormatException(__('This export type is not supported.'));
36+
}
37+
38+
if (!in_array($type, array_map('strtolower', $this->exports), true)) {
39+
throw new UnsupportedExportFormatException(__('This export type is not set on this table component.'));
40+
}
41+
42+
switch ($type) {
43+
case 'csv':default:
44+
$writer = Excel::CSV;
45+
break;
46+
47+
case 'xls':
48+
$writer = Excel::XLS;
49+
break;
50+
51+
case 'xlsx':
52+
$writer = Excel::XLSX;
53+
break;
54+
}
55+
56+
$class = config('laravel-livewire-tables.exports');
57+
58+
return (new $class(
59+
$this->models(),
60+
$this->columns(),
61+
))->download($this->exportFileName.'.'.$type, $writer);
62+
}
63+
}

0 commit comments

Comments
 (0)