Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add askForColumns method #43

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: pxlrbt
43 changes: 43 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,49 @@ class CustomExport extends ExcelExport
}
```

### User Report Builder

You can use the `->askForColumns()` to let the user build a report. This is especially useful when you have a lot of columns and want to let the user pick the ones they need and customize the display order.

```php
use pxlrbt\FilamentExcel\Actions\Tables\ExportAction;
use pxlrbt\FilamentExcel\Exports\ExcelExport;

ExportAction::make()->exports([
ExcelExport::make()->askForColumns()
])
```

You can also create a custom export class and specify the columns where user can select a display format

```php
use pxlrbt\FilamentExcel\Actions\Tables\ExportAction;
use pxlrbt\FilamentExcel\Exports\ExcelExport;
use pxlrbt\FilamentExcel\Columns\Column;
use pxlrbt\FilamentExcel\Columns\ColumnFormats;

class CustomExport extends ExcelExport
{

public function setUp()
{
$this->withFilename('custom_export');
$this->withColumns([
Column::make('total')->askForFormat(availableFormats: ColumnFormats::NUMBER),
Column::make('created_at')->askForFormat(availableFormats: ColumnFormats::DATE),
]);
}
}
```

Then you can you custom export with `->askForColumns()`:

```php
ExportAction::make()->exports([
CustomExport::make()->askForColumns()
])
```

## Contributing

If you want to contribute to this packages, you may want to test it in a real Filament project:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions resources/lang/en/fields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

return [
'export_template' => 'Export template',
'filename' => 'Filename',
'writer_type' => 'Type',
'column_format' => 'Format',
'columns' => [
'label' => 'Columns',
'hint' => 'Select the columns you want to export.',
]
];
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions resources/lang/pt_BR/actions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'label' => 'Exportar',
];
12 changes: 12 additions & 0 deletions resources/lang/pt_BR/fields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

return [
'export_template' => 'Modelo de Exportação',
'filename' => 'Nome do arquivo',
'writer_type' => 'Tipo',
'column_format' => 'Formato',
'columns' => [
'label' => 'Colunas',
'hint' => 'Selecione as colunas que você deseja exportar.',
]
];
14 changes: 14 additions & 0 deletions resources/lang/pt_BR/notifications.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

return [
'queued' => [
'title' => 'Exportação na fila',
'body' => 'Você será notificado quando estiver pronto para download.',
],

'finished' => [
'title' => 'Exportação concluída',
'body' => 'Sua exportação está pronta para download.',
'download' => 'Download',
],
];
230 changes: 230 additions & 0 deletions resources/views/components/export-builder.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
<x-dynamic-component
:component="$getFieldWrapperView()"
:id="$getId()"
:label="$getLabel()"
:label-sr-only="$isLabelHidden()"
:helper-text="$getHelperText()"
:hint="$getHint()"
:hint-icon="$getHintIcon()"
:required="$isRequired()"
:state-path="$getStatePath()"
>
@php
$containers = $getChildComponentContainers();

$isCollapsible = $isCollapsible();
$isItemCreationDisabled = $isItemCreationDisabled();
$isItemDeletionDisabled = $isItemDeletionDisabled();
$isItemMovementDisabled = $isItemMovementDisabled();
$columnLabels = $getColumnLabels();
@endphp
<div
{{-- x-data="{ state: $wire.entangle('{{ $getStatePath() }}') }" --}}
x-data="{ isCollapsed: @js($isCollapsed()) }"
x-on:builder-collapse.window="$event.detail === '{{ $getStatePath() }}' && (isCollapsed = true)"
x-on:builder-expand.window="$event.detail === '{{ $getStatePath() }}' && (isCollapsed = false)"

@class([
"bg-white border border-gray-300 shadow-sm rounded-xl px-4 py-2 relative",
"dark:bg-gray-800 dark:border-gray-600" => config('forms.dark_mode'),
])
>
<div>
@if ((count($containers) > 1) && $isCollapsible)
<div class="space-x-2 rtl:space-x-reverse" x-data="{}">
<x-forms::link
x-on:click="$dispatch('builder-collapse', '{{ $getStatePath() }}')"
tag="button"
size="sm"
>
{{ __('forms::components.builder.buttons.collapse_all.label') }}
</x-forms::link>

<x-forms::link
x-on:click="$dispatch('builder-expand', '{{ $getStatePath() }}')"
tag="button"
size="sm"
>
{{ __('forms::components.builder.buttons.expand_all.label') }}
</x-forms::link>
</div>
@endif
</div>

<div {{ $attributes->merge($getExtraAttributes())->class([
'filament-forms-builder-compone nt space-y-6 rounded-xl',
'bg-gray-50 p-6' => $isInset(),
'dark:bg-gray-500/10' => $isInset() && config('forms.dark_mode'),
]) }}>
@if (count($containers))
<table class="w-full text-left rtl:text-right table-auto mx-4 filament-table-repeater" x-show="! isCollapsed">
<thead>
<tr>
<th class="filament-table-repeater-header-cell">{{ __('Coluna') }}</th>
@foreach($columnLabels as $columnLabel)
@if($columnLabel['display'])
<th class="p-2 filament-table-repeater-header-cell">
<span>
{{ $columnLabel['name'] }}
</span>
</th>
@else
<th style="display: none"></th>
@endif
@endforeach
@if ((! $isItemMovementDisabled) || $hasBlockLabels || (! $isItemDeletionDisabled) || $isCollapsible)
<th class="p-2 filament-table-repeater-header-cell w-20"></th>
@endif

</tr>
</thead>
<tbody
wire:sortable
wire:end.stop="dispatchFormEvent('builder::moveItems', '{{ $getStatePath() }}', $event.target.sortable.toArray())"
>
@php
$hasBlockLabels = $hasBlockLabels();
$hasBlockNumbers = $hasBlockNumbers();
@endphp

@foreach ($containers as $uuid => $item)
@php
$components = collect($item->getComponents())
->mapWithKeys(function ($component) {
return [$component->getName() => $component];
});
@endphp
<tr
x-data="{
isCreateButtonVisible: false,
isCollapsed: @js($isCollapsed()),
}"
x-on:builder-collapse.window="$event.detail === '{{ $getStatePath() }}' && (isCollapsed = true)"
x-on:builder-expand.window="$event.detail === '{{ $getStatePath() }}' && (isCollapsed = false)"
x-on:click="isCreateButtonVisible = true"
x-on:mouseenter="isCreateButtonVisible = true"
x-on:click.away="isCreateButtonVisible = false"
x-on:mouseleave="isCreateButtonVisible = false"
wire:key="{{ $this->id }}.{{ $item->getStatePath() }}.item"
wire:sortable.item="{{ $uuid }}"
>
<td>
@php
$block = $item->getParentComponent();

$block->labelState($item->getRawState());
@endphp

{{ $item->getParentComponent()->getLabel() }}

@php
$block->labelState(null);
@endphp

@if ($hasBlockNumbers)
<small class="font-mono">{{ $loop->iteration }}</small>
@endif
</td>

@foreach(array_keys($columnLabels) as $column)
<td>
{{ $components->get($column) }}
</td>
@endforeach

@if ((! $isItemMovementDisabled) || $hasBlockLabels || (! $isItemDeletionDisabled) || $isCollapsible)
<td>
<div class="flex justify-end">
@unless ($isItemMovementDisabled)
<button
wire:sortable.handle
wire:keydown.prevent.arrow-up="dispatchFormEvent('builder::moveItemUp', '{{ $getStatePath() }}', '{{ $uuid }}')"
wire:keydown.prevent.arrow-down="dispatchFormEvent('builder::moveItemDown', '{{ $getStatePath() }}', '{{ $uuid }}')"
type="button"
@class([
'flex items-center justify-center flex-none w-10 h-10 text-gray-400 border-r rtl:border-l rtl:border-r-0 transition hover:text-gray-300',
'dark:text-gray-400 dark:border-gray-700 dark:hover:text-gray-500' => config('forms.dark_mode'),
])
>
<span class="sr-only">
{{ __('forms::components.builder.buttons.move_item_down.label') }}
</span>

<x-heroicon-s-switch-vertical class="w-4 h-4"/>
</button>
@endunless

<ul @class([
'flex divide-x rtl:divide-x-reverse',
'dark:divide-gray-700' => config('forms.dark_mode'),
])>
@unless ($isItemDeletionDisabled)
<li>
<button
wire:click="dispatchFormEvent('builder::deleteItem', '{{ $getStatePath() }}', '{{ $uuid }}')"
type="button"
@class([
'flex items-center justify-center flex-none w-10 h-10 text-danger-600 transition hover:text-danger-500',
'dark:text-danger-500 dark:hover:text-danger-400' => config('forms.dark_mode'),
])
>
<span class="sr-only">
{{ __('forms::components.builder.buttons.delete_item.label') }}
</span>

<x-heroicon-s-trash class="w-4 h-4"/>
</button>
</li>
@endunless

@if ($isCollapsible)
<li>
<button
x-on:click="isCollapsed = !isCollapsed"
type="button"
@class([
'flex items-center justify-center flex-none w-10 h-10 text-gray-400 transition hover:text-gray-300',
'dark:text-gray-400 dark:hover:text-gray-500' => config('forms.dark_mode'),
])
>
<x-heroicon-s-minus-sm class="w-4 h-4" x-show="! isCollapsed"/>

<span class="sr-only" x-show="! isCollapsed">
{{ __('forms::components.builder.buttons.collapse_item.label') }}
</span>

<x-heroicon-s-plus-sm class="w-4 h-4" x-show="isCollapsed" x-cloak/>

<span class="sr-only" x-show="isCollapsed" x-cloak>
{{ __('forms::components.builder.buttons.expand_item.label') }}
</span>
</button>
</li>
@endif
</ul>
</div>
</td>
@endif
</tr>
@endforeach
</tbody>
</table>
@endif

@if (! $isItemCreationDisabled)
<x-forms::dropdown class="flex justify-center">
<x-slot name="trigger">
<x-forms::button size="sm">
{{ $getCreateItemButtonLabel() }}
</x-forms::button>
</x-slot>

<x-forms::builder.block-picker
:blocks="$getUnusedColumns()"
:state-path="$getStatePath()"
/>
</x-forms::dropdown>
@endif
</div>
</div>
</x-dynamic-component>
29 changes: 20 additions & 9 deletions src/Actions/Concerns/ExportableAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Exception;
use Filament\Forms\Components\Group;
use Filament\Forms\Components\Select;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use pxlrbt\FilamentExcel\Exports\ExcelExport;

Expand All @@ -17,13 +18,12 @@ trait ExportableAction

protected function setUp(): void
{
$this->modalWidth('md');

$this->label(__('filament-excel::actions.label'));
$this->icon('heroicon-o-download');
$this->action(Closure::fromCallable([$this, 'handleExport']));

$this->exports = collect([ExcelExport::make('export')->fromTable()]);
$this
->modalWidth('md')
->label(__('filament-excel::actions.label'))
->icon('heroicon-o-download')
->action(Closure::fromCallable([$this, 'handleExport']))
->exports([ExcelExport::make()->fromTable()]);
}

public function getFormSchema(): array
Expand All @@ -42,7 +42,7 @@ protected function getSelectExportField(): array
{
return [
Select::make('selected_exportable')
->label(__('Export template'))
->label(__('filament-excel::fields.export_template'))
->reactive()
->default(0)
->disablePlaceholderSelection()
Expand All @@ -57,7 +57,7 @@ protected function getExportFormSchemas(): Collection
{
return $this->exports
->map(function (ExcelExport $export, $key) {
$schema = $export->getFormSchema();
$schema = $export->container($this->getLivewire())->getFormSchema();

return empty($schema)
? null
Expand Down Expand Up @@ -85,4 +85,15 @@ public function exports(array $exports): static

return $this;
}

public function handleExport(array $data, ?Collection $records = null)
{
$exportable = $this->getSelectedExport($data);

return app()->call([$exportable, 'hydrate'], [
'livewire' => $this->getLivewire(),
'formData' => Arr::get($data, $exportable->getName(), []),
'records' => $records,
])->export();
}
}
Loading