afcapel / stimulus-autocomplete

Stimulus autocomplete component
MIT License
482 stars 63 forks source link

How can you pass along additional params beyond params[:q] to controller? #99

Closed 34code closed 2 years ago

34code commented 2 years ago

I have a select dropdown param and a textbox for autocomplete. Autocomplete logic depends on knowing the which category the user wants to autocomplete from (from select dropdown)

afcapel commented 2 years ago

You can add any extra param that you need to the data-autocomplete-url-value attribute:

<div data-controller="autocomplete" data-autocomplete-url-value="/products?category_id=3">
...
</div>

You might need to change the autocomplete url dynamically. You can do that with another stimulus controller that listens to changes on your select element and then updates the url with the selected value. Or server side with a turbo stream update if you're using Turbo.

34code commented 2 years ago

Thanks thats a good idea!