F
Filament11mo ago
TKrisee

Is it possible to use dynamic columns in the provided ImportAction?

I'm trying to create an import action with dynamic columns. The idea is to have the user select a given value from a select provided by the getOptionsFormComponents ( I know that this is not what it's made for, but I don't want to override any of the functionalities of the base action ) and based on the value provided by the user I'd like to change the columns in the Importer class. I've managed to make it work but it's not the best approach, to say the least:
public static ?Model $productType = null;

public static function getOptionsFormComponents(): array
{
return [
// If this is set to "live" the sample file provided by the action
// will contain no columns
Select::make('product')
->required()
->hidden(fn (Get $get) => $get('file'))
->options(fn () => ProductType::pluck('name', 'id'))
->afterStateUpdated(function (Set $set, $state) {
self::$productType = ProductType::with(['mappedAttributes'])->find($state);
$set('product_file', $state);
}),
// This needs to be "live", otherwise when you upload the file and change this field
// the mapping columns don't show up on the modal form
Select::make('product_file')
->required()
->hidden(fn (Get $get) => !$get('file'))
->live()
->options(fn () => ProductType::pluck('name', 'id'))
->afterStateUpdated(function ($state) {
self::$productType = ProductType::with(['mappedAttributes'])->find($state);
}),
];
}

public static function getColumns(): array
{
return self::$productType?->mappedAttributes->pluck('name.en')->map(fn ($name) => ImportColumn::make($name))->toArray() ?? [];
}
public static ?Model $productType = null;

public static function getOptionsFormComponents(): array
{
return [
// If this is set to "live" the sample file provided by the action
// will contain no columns
Select::make('product')
->required()
->hidden(fn (Get $get) => $get('file'))
->options(fn () => ProductType::pluck('name', 'id'))
->afterStateUpdated(function (Set $set, $state) {
self::$productType = ProductType::with(['mappedAttributes'])->find($state);
$set('product_file', $state);
}),
// This needs to be "live", otherwise when you upload the file and change this field
// the mapping columns don't show up on the modal form
Select::make('product_file')
->required()
->hidden(fn (Get $get) => !$get('file'))
->live()
->options(fn () => ProductType::pluck('name', 'id'))
->afterStateUpdated(function ($state) {
self::$productType = ProductType::with(['mappedAttributes'])->find($state);
}),
];
}

public static function getColumns(): array
{
return self::$productType?->mappedAttributes->pluck('name.en')->map(fn ($name) => ImportColumn::make($name))->toArray() ?? [];
}
It kinda works, it's very hacky and the mapping guesser won't fire again (as it's tied to the FileUpload component state changes) so I've been wondering if this is possible in any other ways.
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server