christo-ph

christo-ph

Liveview: navigate with phx-click

How can I create or simulate a link to navigate to another page/liveview? I do not want to create a whole <a> element, but create a click event on a tr - like I would do with JS and onClick=window.location....

My first attempt is this, but obviously doesn’t seem to be the right one:

<tr 
  role="button"
  phx-click={live_redirect "foo", to: Routes.admin_foo_path(@socket, :show, foo) }
>

Most Liked

msimonborg

msimonborg

@christo-ph To tie it all together, something like this should get you there:

def render(assigns) do
  ~H"""
  <button phx-click="redirect">Redirect</button>
  """
end

def handle_event("redirect", _, socket) do
  {:noreply, push_redirect(socket, to: "/path")}
end

serpent

serpent

For reference:

<.button type="button" phx-click={JS.navigate(~p"/de/new_lv")}>
  <%= dgettext("forms", "Begin") %>
</.button>
caleb-bb

caleb-bb

I dunno about the <tr> part, but live_redirect/2 might be what you’re looking for if you’re trying to create a link to another liveview.

EDIT: excuse me, I didn’t read your code carefully. live_redirect/2 returns a link, so it’s not something you would emit as a click event. phx-click="xyz" emits an event to the parent liveview (or to the current liveview if target=@myself is specified) which receives it using an event handler. So you wouldn’t put the live_redirect inside of the phx-click.

If it wasn’t clear: live_redirect/2 does not actually perform a redirect. Rather, it creates a link that can create a redirect when clicked.

I’m not sure how to make a <tr> do the same thing. My gut feeling is to use

<%= live_redirect to: Routes.live_path(@socket, MyLive, dir: :asc), replace: false do %>
  <tr> table stuff</tr>
<% end %>

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
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
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New

Other popular topics Top

axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48475 226
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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 42158 114
New
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
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54250 245
New

We're in Beta

About us Mission Statement