johmue

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 LiveView and let this render the layout with the search bar. The other LiveViews then can be a LiveComponent inside of the parent LiveView.
  • 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

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}.

Phoenix.LiveView.Helpers.live_render/3

Also Liked

paulstatezny

paulstatezny

The way I typically achieve this these days is:

  1. Set up a regular Phoenix Form with a text input and a change event handler.
  2. 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.)
  3. In the change handler, perform the search and set the results in an assigns variable.
  4. 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

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

paulstatezny

It didn’t click with me that this is not fully a LiveView app. My bad! :smile:

Where Next?

Popular in Questions Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41989 114
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New

We're in Beta

About us Mission Statement