F
Filament3mo ago
Tjiel

How can I refresh the page after a filter is changed

Hey all, I am currently looking for a way to refresh the whole page whenever a filter of a table is changed. The reason for this is that I have a widget with a mapping tool. And whenever the filter is changed I want it to change map. The problem is that the map of this mapping tool, can not be changed unless the page is reloaded. These are my current filters:
->filters([
Tables\Filters\TrashedFilter::make(),
Tables\Filters\SelectFilter::make('locations')
->relationship('location', 'name')
])
->filters([
Tables\Filters\TrashedFilter::make(),
Tables\Filters\SelectFilter::make('locations')
->relationship('location', 'name')
])
Preferably it only refreshes the page when the transmitters filter gets changed, but I won't mind if there is a solution where it happens if either one gets updated.
7 Replies
Bdev
Bdev3mo ago
Hi Tjiel, I had similar problem try to inject current livewire component to your filter closures then do $livewire->dispatch('eventName') Remember to register listener in your component and pass it $refresh like this:
protected $listeners = [
'eventName' => $refresh,
];
protected $listeners = [
'eventName' => $refresh,
];
Tjiel
Tjiel3mo ago
Thanks for the responses I've currently added an action to the filters:
->filtersApplyAction(function () {
// Refresh the page
})
->filtersApplyAction(function () {
// Refresh the page
})
This action is running whenever a filter gets changed (like how I wanted it). The problem with the dispatch and then refreshing the livewire component is that for some reason the maping tool can't have multiple instances on the same page. Even if you delete/remove the previous one. So basicly I need a way to reload the page and then set the map url in the getFooterwidgets using the parameters of the filter. Here is how the data is passed in the getFooterWidgets:
protected function getFooterWidgets(): array
{
return [
AssetResource\Widgets\MapWidget::make(['mapUrl' => 'real.url']),
];
}
protected function getFooterWidgets(): array
{
return [
AssetResource\Widgets\MapWidget::make(['mapUrl' => 'real.url']),
];
}
Bdev
Bdev3mo ago
You could reach for laravel redirect method maybe something like this:
return redirect()->to(AssetResource::getUrl('list`));

return redirect()->to(AssetResource::getUrl('list`));

Tjiel
Tjiel3mo ago
Update: I just found a solution using livewire dispatch:
->filtersApplyAction(function (Component $livewire) {
$livewire->dispatch('refresh');
})
->filtersApplyAction(function (Component $livewire) {
$livewire->dispatch('refresh');
})
and then have a listener in the mapplicWidget like this:
protected $listeners = ['refresh'];
protected $listeners = ['refresh'];
And lastly have a livewire.on in the blade file:
Livewire.on('refresh', refresh => {
window.location.reload(true);
})
Livewire.on('refresh', refresh => {
window.location.reload(true);
})
now i only need to find out how to get the filters inside the getFooterWidget
Bdev
Bdev3mo ago
Great glad that you have sorted it out thats roughly what i meant with using listeners:)
Tjiel
Tjiel3mo ago
Yeah ty for that at first I didn't think about that but then i was thinking adding custom js and circled back to the dispatch you brought up