TKrisee
TKrisee
FFilament
Created by TKrisee on 2/5/2024 in #❓┊help
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.
2 replies
FFilament
Created by TKrisee on 1/11/2024 in #❓┊help
Resource navigation item using getNavigationParentItem with topNavigation is invisible
could this be a bug, or is it something not yet implemented?
1 replies