pxlrbt / filament-excel

Excel Export for Filament Admin Resources
MIT License
355 stars 68 forks source link

Apply active table filters with headerActions export #87

Closed imbue closed 1 year ago

imbue commented 1 year ago

Is it currently possible to apply the active table filters when exporting using the ExportAction as header action?

Currently this ExportAction will export all records of that db table. It would be great if the filters could be applied to the export query so the user can create an export for i.e. a specific date range:

CleanShot 2023-06-20 at 15 32 28

->filters([
    Tables\Filters\Filter::make('created_at')
        ->form([
            Forms\Components\DatePicker::make('created_from')
                ->placeholder(fn($state): string => 'Dec 18, ' . now()->subYear()->format('Y')),
            Forms\Components\DatePicker::make('created_until')
                ->placeholder(fn($state): string => now()->format('M d, Y')),
        ])
        ->query(function (Builder $query, array $data): Builder {
            return $query
                ->when(
                    $data['created_from'],
                    fn(Builder $query, $date): Builder => $query->whereDate('created_at', '>=', $date),
                )
                ->when(
                    $data['created_until'],
                    fn(Builder $query, $date): Builder => $query->whereDate('created_at', '<=', $date),
                );
        })
        ->indicateUsing(function (array $data): array {
            $indicators = [];
            if ($data['created_from'] ?? null) {
                $indicators['created_from'] = 'Order from ' . Carbon::parse($data['created_from'])->toFormattedDateString();
            }
            if ($data['created_until'] ?? null) {
                $indicators['created_until'] = 'Order until ' . Carbon::parse($data['created_until'])->toFormattedDateString();
            }

            return $indicators;
        }),
])

->headerActions([
    ExportAction::make()->exports([
        ExcelExport::make()->withColumns([
            Column::make('case_number')->heading('Case number'),
            Column::make('created_at')->heading('Creation date'),
        ]),
    ])
])
imbue commented 1 year ago

It looks like a workaround could be to use the ExportBulkAction in combination with the Select all X functionality. This will also export records that are not visible in the current page.

CleanShot 2023-06-20 at 15 38 19

pxlrbt commented 1 year ago

I think this was implemented when you use ->fromTable()

imbue commented 1 year ago

Yeah this option also works for me. Thanks @pxlrbt

imbue commented 1 year ago

A little bit off-issue-topic but is there a way to export related data on a row-for-every-relation basis? I.e some orders have 1 order line, some have 2 or more.

Order Product Quantity
ORD001 product_a 1
ORD001 product_b 3
ORD002 product_c 1

I've considered a couple of options but each of them has a blocker:

Any ideas?

pxlrbt commented 1 year ago

Not really. Haven't found a good way to implement relation across multiple columns yet.

jdion84 commented 9 months ago

Solution:

            ExportAction::make()
                ->exports([
                    ExcelExport::make()
                        ->fromModel()
                        ->useTableQuery(),
                ]),

This uses the model attributes and the currently active table query.