F
Filament17mo ago
sm

TableWidget interactions with livewire events.

Hey there, I'm trying to understand how tablewidgets interact with livewire events. My structure is: Resource that has a custom field, which renders a tablewidget through livewire. The resource has an event defined in its EditResource, and the TableWidget has an event defined in it (both using On attribute). The table($table) function on the TableWidget has default query builder, basically modelClass::query(). It also has an Action, which fires an event which the EditResource is listening for. All of this works fine. The Resource has a set of filters (not filament filters, custom filters built using a form component). When these are updated, the Resource fires an event which the TableWidget listens for in a func called updateFilters. The updateFilters func receives the filters from the event, and updates the TableWidget's query (by doing $this->query(modelClass::getFiltersFromEventData($filters));).. this works. The TableWidget has an Add action attached to it, this just fires a livewire event (on $livewire) to indicate to the resource that a new record has been selected by the user. This is the event that the function in EditResource listens for, it attaches the selected record (using the id from the event) to the resource's modelClass. This is where things get a bit funky: after that event is fired, the table widget's table($table) func gets called again, which resets the query builder (because it has ->query(modelClass::query())), resetting all the filters.. My understanding is that table($table) would only be called once on initialization, is this not the case? How can I preserve the Builder? I've tried adding a protected $filtersState to the TableWidget, but that causes all kinds of weird race conditions.. and I feel like I'm missing something obvious. Any pointers? Thanks!
1 Reply
sm
smOP17mo ago
I've also tried making $filtersState public, just to see if that fixed things.. but no luck :/ Oh, and I also tried using modifyQueryUsing in my updateFilters event.. but that isn't preserved either. Turns out I just needed to read the livewire docs and understand how the rendering cycle works. table() and then render(). In order to achieve what I wanted, I just needed to pass a closure to the $query. hope this helps someone in the future!

Did you find this page helpful?