AxlF
AxlF
FFilament
Created by AxlF on 1/29/2024 in #❓┊help
Is it possible to load select options asynchronous?
I tried to create a custom field and I got the async search working with alpine. It's all fine, but I don't know, how to get the data to the form. The user types, a suggestion box appears and if the user selects a row, I have the needed data as an array. e.g.: The user types "910" and clicks on "91058 Erlangen, Germany". How would I get the underlying array of the selected label to my main form?
6 replies
FFilament
Created by AxlF on 1/29/2024 in #❓┊help
Is it possible to load select options asynchronous?
But, I want a solution without a Select Field. The problem is, that if I click in that select, another Input Field with the search prompt appears. I need suggestions in a box below the input field, where the user is typing.
6 replies
FFilament
Created by AxlF on 1/29/2024 in #❓┊help
Is it possible to load select options asynchronous?
Thank you. I got a working solution with a Select Field. It something like this:
Select::make('location')
->live(onBlur: true)
->searchable()
->required()
->getSearchResultsUsing(function (string $search, Get $get): array {
if (strlen($search) < 3) {
return [];
}
$result = $this->postcode($search, 'de'); // API call

return collect($result)
->mapWithKeys(fn ($item) => [
$item['postal'] => $item['label'],
])
->toArray();
})
->afterStateUpdated(function (?string $state, Set $set) {
$result = $this->postcode($state, 'de');
$result = collect($result)->unique('postal')->last();

if ($result) {
$set('postal', $result['postal']); // Set hidden fields
$set('city', $result['city']); // Set hidden fields
}
}),
Select::make('location')
->live(onBlur: true)
->searchable()
->required()
->getSearchResultsUsing(function (string $search, Get $get): array {
if (strlen($search) < 3) {
return [];
}
$result = $this->postcode($search, 'de'); // API call

return collect($result)
->mapWithKeys(fn ($item) => [
$item['postal'] => $item['label'],
])
->toArray();
})
->afterStateUpdated(function (?string $state, Set $set) {
$result = $this->postcode($state, 'de');
$result = collect($result)->unique('postal')->last();

if ($result) {
$set('postal', $result['postal']); // Set hidden fields
$set('city', $result['city']); // Set hidden fields
}
}),
6 replies
FFilament
Created by AxlF on 1/22/2024 in #❓┊help
Select first item
Thank you. Isn't it possible to get the options data, without doing a second query? I think they are already loaded and it should't be necessary to load it again.
5 replies
FFilament
Created by AxlF on 12/6/2023 in #❓┊help
Add Items to Repeater with a Modal
Repeater::make('locations')
->schema([
Placeholder::make('location')
->label('')
->content(fn (Get $get) => $get('postcode').' '.$get('city')),
])
->addAction(function ($action) {
return $action->form([
TextInput::make('postcode'),
TextInput::make('city'),
])
->action(function ($data, Set $set, Get $get) {
$currentState = $get('locations') ?? [];
$result = array_merge($currentState, [$data]);
$set('locations', $result);
});
})
Repeater::make('locations')
->schema([
Placeholder::make('location')
->label('')
->content(fn (Get $get) => $get('postcode').' '.$get('city')),
])
->addAction(function ($action) {
return $action->form([
TextInput::make('postcode'),
TextInput::make('city'),
])
->action(function ($data, Set $set, Get $get) {
$currentState = $get('locations') ?? [];
$result = array_merge($currentState, [$data]);
$set('locations', $result);
});
})
5 replies
FFilament
Created by AxlF on 12/6/2023 in #❓┊help
Add Items to Repeater with a Modal
I think I got a working solution. Perhaps there is a better apporach, but for now it works.
5 replies