Select with search in Phoenix template

Hello,
I need a select with search for a very long list of items in a view. Something like this one. https://select2.github.io/examples.html. Autocomplete is welcome as well. What do you suggest? Thanks in advance for any ideas.

2 Likes

I’ve used https://leaverou.github.io/awesomplete/ once. It was okay.

2 Likes

If your list of items is very big, I recommend my own library which enhances Lea Verou’s awesomplete widget. It supports dynamic remote data loading using ajax queries.
The phoenix helper for this is available in hex: https://hex.pm/packages/phoenix_form_awesomplete
You will have to make a rest service that returns the search results in json format. This is easy in Phoenix.

6 Likes

That’s exactly what I was looking for. Many thanks!

1 Like

I’ve found some solace in the Select2 box for JQuery

https://select2.org/tagging

using some Phoenix like this (the select2 will change the select boxes with certain ids with JS)

<!-- <%= select f, :item_id, @items, id: "single", class: "form-control", single: true % > -->

You will need to get Select2 working in a MWE (Minimum Working Example) context first.

I’ve personally downloaded the css and made it a file in my project to get this to work. I also added an override file in case we need to update the main file in the future, so as to get the css I want but also not have to deal with file diffs later.

The reason I’m not using awesomeplete is because it didn’t seem to support a data list of maps. I needed that so that the form could read the select input as an id and just associate it by updating the association id through a create form. If you just need one value, awesomeplete will be 10x easier but for associations you will likely have to use select2. that is if the maintainers of awesomeplete don’t add support for lists in this way.

Here is an example of the way my input box looks and it does function appropriately by passing in the ID instead of the name, the black boxes are for obvious obfuscation.

Screen Shot 2021-08-04 at 12.31.06 PM