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

feat: allow limiting column map in exports #14853

Closed
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
25 changes: 25 additions & 0 deletions packages/actions/docs/07-prebuilt-actions/09-export.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,31 @@ ExportAction::make()
->columnMapping(false)
```

### Limiting selectable columns

By default, the user will be asked which columns to export. You can limit the list of columns shown for export by using the `columns()` method:

```php
use App\Filament\Exports\ProductExporter;

ExportAction::make()
->exporter(ProductExporter::class)
->columns(['name', 'sku', 'price'])
])
```

In case you would like to disable the column selection functionality and only export the defined columns, you can combine the `columns()` method with `columnMapping(false)`:

```php
use App\Filament\Exports\ProductExporter;

ExportAction::make()
->exporter(ProductExporter::class)
->columns(['name', 'sku', 'price'])
->columnMapping(false)
])
```

### Calculated export column state

Sometimes you need to calculate the state of a column, instead of directly reading it from a database column.
Expand Down
6 changes: 3 additions & 3 deletions packages/actions/resources/lang/id/import.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
'form' => [

'file' => [

'label' => 'Berkas',

'placeholder' => 'Unggah berkas CSV',

'rules' => [
'duplicate_columns' => '{0} Berkas tidak boleh memiliki lebih dari satu kolom header yang kosong.|{1,*} Berkas tidak boleh memiliki kolom header yang duplikat: :columns.',
],

],

'columns' => [
Expand Down
74 changes: 54 additions & 20 deletions packages/actions/src/Concerns/CanExportRecords.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ trait CanExportRecords

protected bool | Closure $hasColumnMapping = true;

/**
* @var array<string>|Closure
*/
protected array | Closure $columns = [];

protected function setUp(): void
{
parent::setUp();
Expand All @@ -79,26 +84,32 @@ protected function setUp(): void
->columns(1)
->inlineLabel()
->schema(function () use ($action): array {
return array_map(
fn (ExportColumn $column): Split => Split::make([
Forms\Components\Checkbox::make('isEnabled')
->label(__('filament-actions::export.modal.form.columns.form.is_enabled.label', ['column' => $column->getName()]))
->hiddenLabel()
->default($column->isEnabledByDefault())
->live()
->grow(false),
Forms\Components\TextInput::make('label')
->label(__('filament-actions::export.modal.form.columns.form.label.label', ['column' => $column->getName()]))
->hiddenLabel()
->default($column->getLabel())
->placeholder($column->getLabel())
->disabled(fn (Forms\Get $get): bool => ! $get('isEnabled'))
->required(fn (Forms\Get $get): bool => (bool) $get('isEnabled')),
])
->verticallyAlignCenter()
->statePath($column->getName()),
$action->getExporter()::getColumns(),
);
$columns = $action->getColumns();

return collect($action->getExporter()::getColumns())
->when($columns, function (Collection $exporterColumns) use ($columns): Collection {
return $exporterColumns->filter(fn (ExportColumn $column) => in_array($column->getName(), $columns));
})
->map(function (ExportColumn $column): Split {
return Split::make([
Forms\Components\Checkbox::make('isEnabled')
->label(__('filament-actions::export.modal.form.columns.form.is_enabled.label', ['column' => $column->getName()]))
->hiddenLabel()
->default($column->isEnabledByDefault())
->live()
->grow(false),
Forms\Components\TextInput::make('label')
->label(__('filament-actions::export.modal.form.columns.form.label.label', ['column' => $column->getName()]))
->hiddenLabel()
->default($column->getLabel())
->placeholder($column->getLabel())
->disabled(fn (Forms\Get $get): bool => ! $get('isEnabled'))
->required(fn (Forms\Get $get): bool => (bool) $get('isEnabled')),
])
->verticallyAlignCenter()
->statePath($column->getName());
})
->all();
})
->statePath('columnMap')] : []),
...$action->getExporter()::getOptionsFormComponents(),
Expand Down Expand Up @@ -156,8 +167,13 @@ protected function setUp(): void
->mapWithKeys(fn (array $column, string $columnName): array => [$columnName => $column['label']])
->all();
} else {
$columns = $this->getColumns();

$columnMap = collect($exporter::getColumns())
->mapWithKeys(fn (ExportColumn $column): array => [$column->getName() => $column->getLabel()])
->when($columns, function (Collection $exporterColumns) use ($columns) {
return $exporterColumns->only($columns);
})
->all();
}

Expand Down Expand Up @@ -412,4 +428,22 @@ public function hasColumnMapping(): bool
{
return (bool) $this->evaluate($this->hasColumnMapping);
}

/**
* @param array<string>|Closure $columns
*/
public function columns(array | Closure $columns): static
{
$this->columns = $columns;

return $this;
}

/**
* @return array<string>
*/
public function getColumns(): array
{
return $this->evaluate($this->columns);
}
}
Loading