johmue
Search bar with find as you type autosuggestion along with a LiveView
I am developing a web application which uses LiveViews for many different views of the app. Now I want to have a search bar in the general layout of the app, i. e. accessible from all views and I want this search bar to be autosuggesting find as you type.
For this autosuggestion I would need to make the search bar part of a LiveView which queries the database after every key stroke. How would this LiveView coexist with the other LiveViews?
My ideas so far:
- make the wole app a parent
LiveViewand let this render the layout with the search bar. The otherLiveViews then can be aLiveComponentinside of the parentLiveView. - put the search bar into an
<iframe/>and handle it completely independent from the other views.
I would tend to the letter as the former to me seems to add too much complexity.
Other suggestions, opinions, experiences? What is the best practice here?
Marked As Solved
msimonborg
I think @paulstatezny was saying to build it with LiveView, but you don’t need to rewrite everything as one big nested parent LiveView.
If I’m not mistaken, you can write your search box as a standalone LiveView and then live_render(conn/socket, SearchBoxLive) from any view or template, even the root template. It will run as its own isolated LiveView on any page without changing anything else. You might need to check out the :sticky option and remove the live layout in your mount callback with {:ok, socket, layout: false}.
Also Liked
paulstatezny
The way I typically achieve this these days is:
- Set up a regular Phoenix Form with a text input and a change event handler.
- Add a debounce on the text input, e.g.
phx-debounce="500". That way the search doesn’t happen on literally every keystroke. (Which would slam the DB more than necessary.) - In the change handler, perform the search and set the results in an
assignsvariable. - In the HTML, if there are any search results, display an autosuggest “dropdown”.
I don’t think there’s a need to complicate it with iframes or nested LiveViews.
The docs about debouncing are here:
Much could be said about building an effective autosuggest component for [4] but I don’t think that’s the focus of your question.
johmue
That would make sense, thanks. I am using live_render/3 for child LiveComponents. However, I thought that might lead to issues because some of my views are LiveViews and some are not. So at some pages the search box is actually inside a LiveView in others it isn’t.
Thanks for the hint with the :sticky option. I will try it out.
paulstatezny
It didn’t click with me that this is not fully a LiveView app. My bad! ![]()
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









