Hello,
In my scenario, there is a list of items and a search field. I want to update the list of items with elements filtered with the search term.
I have a working solution right now, but I think there should be a better solution.
I’m using Kaminari for Pagination as well and this works by
- Setting
remote=true
- In the controller, filtering the elements
@surveys = Survey.where(...).page(...).per(...)
- Respond to js with a
.js.erb
:format.js { render :update_list }
- In
update_list.js.erb
replacing the list with a new list rendered with the partial and the new elements:document.getElementById("surveys").innerHTML = "<%= j render(partial: 'surveys', locals: { surveys: @surveys }) %>"
In order to include search as well, I have created a Stimulus Controller that performs an AJAX call with the search term as parameter and Accept: Javascript header, so that the controller responds the same way as for a pagination.
The problem is, I would also like show additional information when searching, e.g. the number of records matching the search query. Right now, Stimulus is not even involved with updating the DOM, that happens as “side effect”
I feel like there should be a way to request the filtered elements via Stimulus, render the partial into the correct div AND then update additional information.
How can this be done?