refresh select after suffixAction completed
I need to refresh my select options after the suffix action is completed. How would I accomplish this? Maybe using afterStateUpdated()?
10 Replies
Have you found any solution to this?
You can do something similar to the following, but notice that it works when you don't use
->searchable()
:
Try
$component->callAfterStateUpdated()
in the action callback. That should work even with searchable.
Not sure exactly what you are trying to accomplish though.Are you using a resource or custom LW component?
@awcodes I have a select with options that are populated from an external api. I'm caching the results so it's not constantly fetching them. If a new item is added in this external software, I want to be able to click the refresh button to get a fresh list from the API and cache it. It's working, but you have to refresh the page to get the new options in the select.
@Leandro Ferreira It's a livewire component. I'll try your suggestion.
Should work ✌️
thanks!
These ideas aren't working for some reason. Here's some context in case that's helpful. The dropdown isn't updating unless I refresh the page.
public function mount()
{
$this->location = SelectedLocation::get();
$this->vendors = $this->getVendors($this->location);
$this->form->fill();
}
//lives in a trait.
public function getVendors(Location $location, $forceRefresh = false)
{
$key = $this->getCacheKeyForLocation(CacheKeys::VENDORS, $location->id);
if ($forceRefresh) {
Cache::forget($key);
}
return Cache::remember($key, null, function () {
return LocationVendors::get($this->location)->pluck('display_name', 'quickbooks_vendor_ref_id')->toArray();
});
}
public function updateVendors(array $vendors)
{
$this->vendors = $vendors;
}
public function form(Form $form): Form
{
return $form->schema([
Select::make('quickbooks_vendor_ref_id')
->required()
->label('Vendor')
->options($this->vendors)
->preload()
->searchable()
->inlineLabel()
->loadingMessage('Loading vendors...')
->suffixAction(
Action::make('refreshVendors')
->label('Refresh Vendors')
->icon('heroicon-o-arrow-path')
->action(function (Select $component) {
$component->state(null);
$vendors = $this->getVendors($this->location, true);
$this->updateVendors($vendors);
$component->callAfterStateUpdated();
})
),
])->statePath('data')
->model($this->item)
->disabled(fn () => in_array($this->item->document_status_id, [5, 6]));
}
also, not sure how you guys are getting the nice formatted code in discord..Like following just clear space after (`):
` ` ` php
// Put your code here
` ` `
Solution
use
->options(fn () => $this->vendors)
that worked!! oh man, you're my hero. thanks!