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.push
to 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:text
and 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.heex
there already is a modal for thenew
andedit
actions. Let's add a modal for theshow
action right below it:We want this modal to open when a table row is clicked, but right now the
row_click
function of the table navigates to/posts/#{post}
wich is routed to thePostLive.Show
view. Let's change that by routing theshow
route/action toPostLive.Index
. Inrouter.ex
change the line:and add a corresponding action to the
PostLive.Index
module (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_click
function of the table to usepatch
instead ofnavigate
, because we navigate to the same live module (more about that here):