Skip to content

Commit 3a4604c

Browse files
authored
Merge pull request #80 from rappasoft/develop
Exports
2 parents 0c1bb25 + e883c91 commit 3a4604c

File tree

16 files changed

+484
-4
lines changed

16 files changed

+484
-4
lines changed

CHANGELOG.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ All notable changes to `laravel-livewire-tables` will be documented in this file
44

55
## [Unreleased]
66

7+
## [0.3.2] - 2020-09-25
8+
9+
### Added
10+
11+
- Added thead class to option array
12+
- Ability to export the list set to CSV/XLS/XLSX/PDF
13+
- Ability to mark a visible column as not to be exported
14+
- Ability to mark a column as export only, which hides it from UI
15+
- Ability to format a single column differently for export as it is for its UI
16+
- Added option to change the button class from the config
17+
718
## [0.3.1] - 2020-09-18
819

920
### Changed
@@ -119,7 +130,8 @@ All notable changes to `laravel-livewire-tables` will be documented in this file
119130

120131
- Initial release
121132

122-
[Unreleased]: https://github.com/rappasoft/laravel-livewire-tables/compare/v0.3.1...development
133+
[Unreleased]: https://github.com/rappasoft/laravel-livewire-tables/compare/v0.3.2...development
134+
[0.3.2]: https://github.com/rappasoft/laravel-livewire-tables/compare/v0.3.1...v0.3.2
123135
[0.3.1]: https://github.com/rappasoft/laravel-livewire-tables/compare/v0.3.0...v0.3.1
124136
[0.3.0]: https://github.com/rappasoft/laravel-livewire-tables/compare/v0.2.1...v0.3.0
125137
[0.2.1]: https://github.com/rappasoft/laravel-livewire-tables/compare/v0.2.0...v0.2.1

README.md

Lines changed: 142 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,21 @@ public function hide() : self;
164164
* Hide this column based on a condition. i.e.: user has or doesn't have a role or permission. Must return a boolean, not a closure.
165165
*/
166166
public function hideIf($condition) : self;
167+
168+
/**
169+
* This column is only included in exports and is not available to the UI
170+
*/
171+
public function exportOnly() : self;
172+
173+
/**
174+
* This column is excluded from the export but visible to the UI unless defined otherwise with hide() or hideIf()
175+
*/
176+
public function excludeFromExport() : self;
177+
178+
/**
179+
* If supplied, and the column is exportable, this will be the format when rendering the CSV/XLS/PDF instead of the format() function. You may have both, format() for the UI, and exportFormat() for the export only. If this method is not supplied, format() will be used and passed through strip_tags() to try to clean the output.
180+
*/
181+
public function exportFormat(callable $callable = null) : self;
167182
```
168183

169184
### Properties
@@ -221,6 +236,13 @@ You can override any of these in your table component:
221236
| -------- | ------- | ----- |
222237
| $offlineIndicator | true | Whether or not to display an offline message when there is no connection |
223238

239+
#### Exports
240+
241+
| Property | Default | Usage |
242+
| -------- | ------- | ----- |
243+
| $exportFileName | data | The name of the downloaded file when exported |
244+
| $exports | [] | The available options to export this table as (csv, xls, xlsx, pdf) |
245+
224246
#### Other
225247

226248
| Property | Default | Usage |
@@ -292,7 +314,120 @@ public function email($email): string
292314
public function html($html): HtmlString
293315
```
294316

295-
### Setting Options
317+
### Exporting Data
318+
319+
The table component supports exporting to CSV, XLS, XLSX, and PDF.
320+
321+
In order to use this functionality you must install [Laravel Excel](https://laravel-excel.com) 3.1 or newer.
322+
323+
In order to use the PDF export functionality, you must also install either [DOMPDF](https://github.com/dompdf/dompdf) or [MPDF](https://github.com/mpdf/mpdf).
324+
325+
You may set the PDF export library in the config file under **pdf_library**. DOMPDF is the default.
326+
327+
#### What exports your table supports
328+
329+
By default, exporting is off. You can add a list of available export types with the $exports class property.
330+
331+
`public $exports = ['csv', 'xls', 'xlsx', 'pdf'];`
332+
333+
#### Defining the file name.
334+
335+
By default, the filename will be `data`.*type*. I.e. `data.pdf`, `data.csv`.
336+
337+
You can change the filename with the `$exportFileName` class property.
338+
339+
`public $exportFileName = 'users-table';` - *Obviously omit the file type*
340+
341+
#### Deciding what columns to export
342+
343+
You have a couple option on exporting information. By default, if not defined at all, all columns will be exported.
344+
345+
If you have a column that you want visible to the UI, but not to the export, you can chain on `excludeFromExport()`
346+
347+
If you have a column that you want visible to the export, but not to the UI, you can chain on `exportOnly()`
348+
349+
#### Formatting column data for export
350+
351+
By default, the export will attempt to render the information just as it is shown to the UI. For a normal column based attribute this is fine, but when exporting formatted columns that output a view or HTML, it will attempt to strip the HTML out.
352+
353+
Instead, you have available to you the `exportFormat()` method on your column, to define how you want this column to be formatted when outputted to the file.
354+
355+
So you can have a column that you want both available to the UI and the export, and format them differently based on where it is being outputted.
356+
357+
#### Exporting example
358+
359+
```php
360+
<?php
361+
362+
namespace App\Http\Livewire;
363+
364+
use App\User;
365+
use Illuminate\Database\Eloquent\Builder;
366+
use Rappasoft\LaravelLivewireTables\TableComponent;
367+
use Rappasoft\LaravelLivewireTables\Traits\HtmlComponents;
368+
use Rappasoft\LaravelLivewireTables\Views\Column;
369+
370+
class UsersTable extends TableComponent
371+
{
372+
use HtmlComponents;
373+
374+
public function query() : Builder
375+
{
376+
return User::with('role')->withCount('permissions');
377+
}
378+
379+
public function columns() : array
380+
{
381+
return [
382+
Column::make('ID')
383+
->searchable()
384+
->sortable()
385+
->excludeFromExport(), // This column is visible to the UI, but not export.
386+
Column::make('ID')
387+
->exportOnly(), // This column is only rendered on export
388+
Column::make('Avatar')
389+
->format(function(User $model) {
390+
return $this->image($model->avatar, $model->name, ['class' => 'img-fluid']);
391+
})
392+
->excludeFromExport(), // This column is visible to the UI, but not export.
393+
Column::make('Name') // This columns is visible to both the UI and export, and is rendered the same
394+
->searchable()
395+
->sortable(),
396+
Column::make('E-mail', 'email')
397+
->searchable()
398+
->sortable()
399+
->format(function(User $model) {
400+
return $this->mailto($model->email, null, ['target' => '_blank']);
401+
})
402+
->exportFormat(function(User $model) { // This column is visible to both the UI and the export, but is formatted differently to the export via this method.
403+
return $model->email;
404+
}),
405+
Column::make('Role', 'role.name') // This columns is visible to both the UI and export, and is rendered the same
406+
->searchable()
407+
->sortable(),
408+
Column::make('Permissions') // This columns is visible to both the UI and export, and is rendered the same, except the HTML tags will be removed because it is not specifically calling a exportFormat() function.
409+
->sortable()
410+
->format(function(User $model) {
411+
return $this->html('<strong>'.$model->permissions_count.'</strong>');
412+
}),
413+
Column::make('Actions')
414+
->format(function(User $model) {
415+
return view('backend.auth.user.includes.actions', ['user' => $model]);
416+
})
417+
->hideIf(auth()->user()->cannot('do-this'))
418+
->excludeFromExport(), // This column is visible to the UI, but not export.
419+
];
420+
}
421+
}
422+
```
423+
424+
#### Customizing Exports
425+
426+
Currently, there are no customization options available. But there is a config item called `exports` where you can define the class to do the rendering. You can use the `\Rappasoft\LaravelLivewireTables\Exports\Export` class as a base.
427+
428+
More options will be added in the future, but the built in options should be good for most applications.
429+
430+
### Setting Component Options
296431

297432
There are some frontend framework specific options that can be set.
298433

@@ -304,6 +439,12 @@ They are done this way instead of the config file that way you can have per-comp
304439
protected $options = [
305440
// The class set on the table when using bootstrap
306441
'bootstrap.classes.table' => 'table table-striped table-bordered',
442+
443+
// The class set on the table's thead when using bootstrap
444+
'bootstrap.classes.thead' => null,
445+
446+
// The class set on the table's export dropdown button
447+
'bootstrap.classes.buttons.export' => 'btn',
307448

308449
// Whether or not the table is wrapped in a `.container-fluid` or not
309450
'bootstrap.container' => true,

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
"orchestra/testbench": "^5.0",
2828
"phpunit/phpunit": "^9.0"
2929
},
30+
"suggest": {
31+
"maatwebsite/excel": "Required to use export functionality"
32+
},
3033
"autoload": {
3134
"psr-4": {
3235
"Rappasoft\\LaravelLivewireTables\\": "src"

config/config.php

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

33
return [
44

5+
/*
6+
* The class to use to handle the export functionality
7+
*/
8+
'exports' => \Rappasoft\LaravelLivewireTables\Exports\Export::class,
9+
10+
/*
11+
* Which library you want to use for PDF generation
12+
* Supports dompdf, mpdf
13+
* You must install the appropriate third party package for each
14+
* See: https://docs.laravel-excel.com/3.1/exports/export-formats.html
15+
* And: https://phpspreadsheet.readthedocs.io/en/latest/topics/reading-and-writing-to-file/#pdf
16+
*/
17+
'pdf_library' => 'dompdf',
18+
519
/*
620
* The frontend styling framework to use
721
* 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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
20+
@if (in_array('pdf', $exports, true))
21+
<a class="dropdown-item" href="#" wire:click.prevent="export('pdf')">PDF</a>
22+
@endif
23+
</div>
24+
</div><!--export-dropdown-->
25+
@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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@if ($tableHeaderEnabled)
2-
<thead>
2+
<thead class="{{ $this->getOption('bootstrap.classes.thead') }}">
33
@include('laravel-livewire-tables::'.config('laravel-livewire-tables.theme').'.includes.columns')
44
</thead>
55
@endif
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Exceptions;
4+
5+
use Exception;
6+
7+
class UnsupportedExportFormatException extends Exception
8+
{
9+
}

0 commit comments

Comments
 (0)