tubedude

tubedude

Phoenix Live View: Client-Side Patching with Select Element (JS.dispatch & LiveSocket.execJS)

Hi everyone,

I’m working on a Phoenix Live View application where I wanted to achieve client-side navigation triggered by a user’s selection in a select element. The user selects an option, and the application navigates to the corresponding path without a full page reload.

Initially, I explored using phx-click on each option, but it did not generate any event. Moving to the select element itself, I had trouble accessing the selected value.

My current approach utilizes phx-change to trigger a custom event named "live_view_navigate". Within the event listener, I access the selected value from the event object’s target.value. And trigger a JS.patch via liveview.execJS.

Here’s the interesting part:

in app.js

window.addEventListener("live_view_navigate", event => {
  const path = event.target.value;
  const encodedJS = '[["patch",{"replace":false,"href":"' + path + '"}]]';
  liveSocket.execJS(event.target, encodedJS)
});

And in the html:

<select id="current-tab" name="current-tab" phx-change={JS.dispatch("live_view_navigate")} >
  <%= for option <- @options do %>
    <option value={option.path} selected={option[:current]}><%= option.label 
  </option>
  <% end %>
</select>

This code constructs a JavaScript array representing a patch operation. It then uses liveSocket.execJS to execute this patch directly on the select element, achieving navigation without a server roundtrip.

My question:

Is this the recommended approach for achieving client-side navigation based on a select element in Phoenix Live View? Is there a cleaner or more efficient way to accomplish this functionality?

Thanks in advance for your insights!

First Post!

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Did you try a phx-change on the form containing the select?

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
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
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement