How do I call handle_event wish specific params without using a form?
I'd like table row details to be displayed in a modal when row_click happens. I am able to trigger handle events, but params are always %{}.
How do I call handle_event wish specific params without using a form?
I'd like table row details to be displayed in a modal when row_click happens. I am able to trigger handle events, but params are always %{}.
Copyright © 2021 Jogjafile Inc.
Check out the docs about click events to read more about event parameters and about JS commands.
You can use
JS.pushto send an event (with params) or use any other of the JS functions.That was the short answer, now the long answer, hopefully more specific to your second question. If not, you should have provided more details. ;p
Show a modal with table row details
I'm going to assume we just created a new Phoenix project, ran
mix phx.gen.live Blog Post posts title:string body:textand followed the instructions to create a new live view. The goal is to click on a table row in the index view and show a modal with the table row details.In
index.html.heexthere already is a modal for thenewandeditactions. Let's add a modal for theshowaction right below it:We want this modal to open when a table row is clicked, but right now the
row_clickfunction of the table navigates to/posts/#{post}wich is routed to thePostLive.Showview. Let's change that by routing theshowroute/action toPostLive.Index. Inrouter.exchange the line:and add a corresponding action to the
PostLive.Indexmodule (post_live/index.ex):Now everything is wired up and when a table row is clicked, the show modal opens.
To improve on this a bit more, change the
row_clickfunction of the table to usepatchinstead ofnavigate, because we navigate to the same live module (more about that here):