Skip to content

Commit

Permalink
Added support to export records (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonrietdijk authored Sep 5, 2023
1 parent 5d7c766 commit c17d9ca
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 9 deletions.
4 changes: 4 additions & 0 deletions docs/.vitepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export default {
{
text: 'Links',
link: '/usage/links'
},
{
text: 'Exports',
link: '/usage/exports'
}
]
},
Expand Down
2 changes: 2 additions & 0 deletions docs/usage/actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Action::make(__('My Action'), 'my_action', function (Enumerable $models): void {
}),
```

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.

When an action has been executed, it will automatically clear the selection and refresh the table. This can be prevented
if you return `false` from your callback.

Expand Down
63 changes: 63 additions & 0 deletions docs/usage/exports.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Exports

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).

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.

## Example

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.

```php
protected function actions(): array
{
return [
Action::make(__('Export All'), 'export_all', function (): mixed {
$collection = $this->appliedQuery()->get();

return Excel::download(
new BlogExport($collection), 'blogs.xlsx',
);
})->standalone(),
];
}
```

You can also use a regular action, only exporting records that have been selected.

```php
protected function actions(): array
{
return [
Action::make(__('Export'), 'export', function (Enumerable $models): mixed {
return Excel::download(
new BlogExport($models), 'blogs.xlsx',
);
}),
];
}
```

An example of the `BlogExport` could look like the following. Note that any formatting can be applied in this class.

```php
<?php

namespace App\Exports;

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;

class BlogExport implements FromCollection
{
public function __construct(
protected Collection $collection
) {
}

public function collection()
{
return $this->collection;
}
}
```
4 changes: 2 additions & 2 deletions src/Columns/BaseColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@

abstract class BaseColumn
{
use CanBeClickable;
use CanBeComputed;
use CanBeQualified;
use CanBeClickable;
use CanBeRaw;
use HasFooter;
use HasHeader;
use HasMetadata;
use HasSearch;
use HasSorting;
use HasValue;
use HasMetadata;
use Makeable;

protected string $code;
Expand Down
15 changes: 10 additions & 5 deletions src/Concerns/HasActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,27 @@ protected function resolveActions(): Enumerable
return collect($this->actions());
}

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

$models = collect();

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

$status = $action->execute($models);
$response = $action->execute($models);

if ($response !== false) {
if (! $action->isStandalone()) {
$this->clearSelection();
}

if ($status !== false) {
$this->clearSelection();
$this->dispatch('refreshLivewireTable');
}

return $response;
}
}
2 changes: 1 addition & 1 deletion src/Concerns/HasSorting.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected function applySorting(Builder $builder): static

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

if ($column !== null && $direction !== null) {
Expand Down
2 changes: 1 addition & 1 deletion src/Livewire/LivewireTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

class LivewireTable extends Component
{
use WithPagination;
use HasActions;
use HasColumns;
use HasDeferredLoading;
Expand All @@ -42,6 +41,7 @@ class LivewireTable extends Component
use HasSelection;
use HasSoftDeletes;
use HasSorting;
use WithPagination;

protected string $model = Model::class;

Expand Down

0 comments on commit c17d9ca

Please sign in to comment.