Skip to content

Commit c17d9ca

Browse files
Added support to export records (#20)
1 parent 5d7c766 commit c17d9ca

File tree

7 files changed

+83
-9
lines changed

7 files changed

+83
-9
lines changed

docs/.vitepress/config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ export default {
7171
{
7272
text: 'Links',
7373
link: '/usage/links'
74+
},
75+
{
76+
text: 'Exports',
77+
link: '/usage/exports'
7478
}
7579
]
7680
},

docs/usage/actions.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Action::make(__('My Action'), 'my_action', function (Enumerable $models): void {
2020
}),
2121
```
2222

23+
Actions can return anything but are not required to. This makes it very simple to redirect the user to another page or to download an [export](/usage/exports) of the records.
24+
2325
When an action has been executed, it will automatically clear the selection and refresh the table. This can be prevented
2426
if you return `false` from your callback.
2527

docs/usage/exports.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Exports
2+
3+
Exporting data from the table can be really useful to quickly get all records in a spreadsheet. Creating an export functionality is very simple using [actions](/usage/actions).
4+
5+
The example below makes use of [maatwebsite/excel](https://laravel-excel.com/). It is not required to use this package, as you can use anything you want. If you are planning to use `maatwebsite/excel`, please follow the installation instructions before continuing.
6+
7+
## Example
8+
9+
Start by adding an action to your table. This example will make use of a [standalone](/usage/actions#standalone) action. By doing so, all records that are available in the table will be included in the export while respecting all filters and sortings.
10+
11+
```php
12+
protected function actions(): array
13+
{
14+
return [
15+
Action::make(__('Export All'), 'export_all', function (): mixed {
16+
$collection = $this->appliedQuery()->get();
17+
18+
return Excel::download(
19+
new BlogExport($collection), 'blogs.xlsx',
20+
);
21+
})->standalone(),
22+
];
23+
}
24+
```
25+
26+
You can also use a regular action, only exporting records that have been selected.
27+
28+
```php
29+
protected function actions(): array
30+
{
31+
return [
32+
Action::make(__('Export'), 'export', function (Enumerable $models): mixed {
33+
return Excel::download(
34+
new BlogExport($models), 'blogs.xlsx',
35+
);
36+
}),
37+
];
38+
}
39+
```
40+
41+
An example of the `BlogExport` could look like the following. Note that any formatting can be applied in this class.
42+
43+
```php
44+
<?php
45+
46+
namespace App\Exports;
47+
48+
use Illuminate\Support\Collection;
49+
use Maatwebsite\Excel\Concerns\FromCollection;
50+
51+
class BlogExport implements FromCollection
52+
{
53+
public function __construct(
54+
protected Collection $collection
55+
) {
56+
}
57+
58+
public function collection()
59+
{
60+
return $this->collection;
61+
}
62+
}
63+
```

src/Columns/BaseColumn.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@
1919

2020
abstract class BaseColumn
2121
{
22+
use CanBeClickable;
2223
use CanBeComputed;
2324
use CanBeQualified;
24-
use CanBeClickable;
2525
use CanBeRaw;
2626
use HasFooter;
2727
use HasHeader;
28+
use HasMetadata;
2829
use HasSearch;
2930
use HasSorting;
3031
use HasValue;
31-
use HasMetadata;
3232
use Makeable;
3333

3434
protected string $code;

src/Concerns/HasActions.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,27 @@ protected function resolveActions(): Enumerable
2121
return collect($this->actions());
2222
}
2323

24-
public function executeAction(string $code): void
24+
public function executeAction(string $code): mixed
2525
{
2626
/** @var BaseAction $action */
27-
$action = $this->resolveActions()->firstOrFail(fn (BaseAction $action): bool => $action->code() === $code);
27+
$action = $this->resolveActions()->firstOrFail(fn (BaseAction $action): bool => $code === $action->code());
2828

2929
$models = collect();
3030

3131
if (! $action->isStandalone() && count($this->selected) > 0) {
3232
$models = $this->query()->whereIn($this->model()->getKeyName(), $this->selected)->get();
3333
}
3434

35-
$status = $action->execute($models);
35+
$response = $action->execute($models);
36+
37+
if ($response !== false) {
38+
if (! $action->isStandalone()) {
39+
$this->clearSelection();
40+
}
3641

37-
if ($status !== false) {
38-
$this->clearSelection();
3942
$this->dispatch('refreshLivewireTable');
4043
}
44+
45+
return $response;
4146
}
4247
}

src/Concerns/HasSorting.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ protected function applySorting(Builder $builder): static
5959

6060
/** @var ?BaseColumn $column */
6161
$column = $this->resolveColumns()->first(function (BaseColumn $column): bool {
62-
return $column->isSortable() && $column->code() === $this->sortColumn;
62+
return $column->isSortable() && $this->sortColumn === $column->code();
6363
});
6464

6565
if ($column !== null && $direction !== null) {

src/Livewire/LivewireTable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
class LivewireTable extends Component
2929
{
30-
use WithPagination;
3130
use HasActions;
3231
use HasColumns;
3332
use HasDeferredLoading;
@@ -42,6 +41,7 @@ class LivewireTable extends Component
4241
use HasSelection;
4342
use HasSoftDeletes;
4443
use HasSorting;
44+
use WithPagination;
4545

4646
protected string $model = Model::class;
4747

0 commit comments

Comments
 (0)