Collecting value from a list using phx-click?

I have a doubt.

Currently in my html, I have a

   <%= for result <- @allresult do %>
   <td><%= result.priority %></td>
    <button phx-click="test" phx-value-myvar="<% result %>" phx-throttle="1000">Buttom Test</button>
  <% %>

How do I pass the value of “result” in variable “myvar”?
The code above does not return any value.

Would have any idea how to do this?

I need it to return the value when clicking the button.

It looks like result is a complex structure…

You might want to return simpler data, maybe like result.id

Worst of all, I’ve tried something like that.
It returns me the following error.

  • (exit) an exception was raised:
    ** (ArgumentError) argument error
    :erlang.apply([%{example1: “1”, example2: “Result End 1”, example3: 1, example4: “Critical”}, %{example1: “1”, example2: “Result End 2”, example3: 1, example4: “Low”}, %{example1: “1”, example2: “Result End 3”, example3: 1, example4: “Critical”}], :clause, [])
    (project 0.1.0) lib/project_web/live/example_live/index.html.leex:48: anonymous fn/3 in ProjectWeb.ExampleLive.Index.render/1
    (elixir 1.11.2) lib/enum.ex:2181: Enum."-reduce/3-lists^foldl/2-0-"/3
    (project 0.1.0) lib/project_web/live/example_live/index.html.leex:41: anonymous fn/2 in ProjectWeb.ExampleLive.Index.render/1
    (phoenix_live_view 0.15.7) lib/phoenix_live_view/diff.ex:356: Phoenix.LiveView.Diff.traverse/6
    (phoenix_live_view 0.15.7) lib/phoenix_live_view/diff.ex:430: anonymous fn/4 in Phoenix.LiveView.Diff.traverse_dynamic/6
    (elixir 1.11.2) lib/enum.ex:2181: Enum."-reduce/3-lists^foldl/2-0-"/3
    (phoenix_live_view 0.15.7) lib/phoenix_live_view/diff.ex:356: Phoenix.LiveView.Diff.traverse/6
    (phoenix_live_view 0.15.7) lib/phoenix_live_view/diff.ex:430: anonymous fn/4 in Phoenix.LiveView.Diff.traverse_dynamic/6
    (elixir 1.11.2) lib/enum.ex:2181: Enum."-reduce/3-lists^foldl/2-0-"/3
    (phoenix_live_view 0.15.7) lib/phoenix_live_view/diff.ex:356: Phoenix.LiveView.Diff.traverse/6
    (phoenix_live_view 0.15.7) lib/phoenix_live_view/diff.ex:130: Phoenix.LiveView.Diff.render/3
    (phoenix_live_view 0.15.7) lib/phoenix_live_view/static.ex:298: Phoenix.LiveView.Static.to_rendered_content_tag/4
    (phoenix_live_view 0.15.7) lib/phoenix_live_view/static.ex:149: Phoenix.LiveView.Static.render/3
    (phoenix_live_view 0.15.7) lib/phoenix_live_view/controller.ex:35: Phoenix.LiveView.Controller.live_render/3
    (phoenix 1.5.7) lib/phoenix/router.ex:352: Phoenix.Router.__call__/2
    (project 0.1.0) lib/project_web/endpoint.ex:1: ProjectWeb.Endpoint.plug_builder_call/2
    (project 0.1.0) lib/plug/debugger.ex:136: ProjectWeb.Endpoint."call (overridable 3)"/2
    (project 0.1.0) lib/project_web/endpoint.ex:1: ProjectWeb.Endpoint.call/2
    (phoenix 1.5.7) lib/phoenix/endpoint/cowboy2_handler.ex:65: Phoenix.Endpoint.Cowboy2Handler.init/4

You need to know what type of data result is…

And when You show error stack trace, You should show the code resulting to this result.

You should help us understand what is the root of the problem, because we cannot guess :slight_smile:

1 Like

That’s why I sent the complete error.
I thought they were easy to understand, for those who have been through this.

But the result is

result =
[
%{exemplo1: “1”, exemplo2: “Result End 1”, exemplo3: 1, exemplo4: “Crítical”},
%{ exemplo1: “1”, exemplo2: “Result End 2”, exemplo3: 1, exemplo4: “low”},
%{exemplo1: “1”, exemplo2: “Result End 3”, exemplo3: 1, exemplo4: “Crítical” }
]

I’m still trying to understand the real reason for the error.
Still, thanks for the attempt.

You could start with a simpler test…

like phx-value-myvar=“koko” and see if the value is passed correctly, then You could decide what You want to send.

A simple example a string with comma delimited values…

like Enum.join([result.example1, result.examplen], ", ")

Then decode server side.

You’re missing the = when interpolating: <%= result %> instead of <% result %>.

As @kokolegorille mentioned, since result isn’t a primitive data structure, you need to either encode it, or pass some sort of an id and then later on retrieve the whole struct based on this id.

1 Like

I’ll leave some tests that I did below.

Test 1
<button phx-click="test" phx-value-myvar="<% result %>" phx-throttle="1000">Button Test</button>

%{"myvar" => "", "value" => ""}

Does not return any value,
returns the following warning

warning: variable result in code block has no effect as it is never returned (remove the variable or assign it to _ to avoid warnings)
lib/project_web/live/example_live/index.html.leex:48: ProjectWeb.MatrixPriorityLive.Index.render/1

Test 2
When I put it that way
<button phx-click="test" phx-value-myvar="My Variable test" phx-throttle="1000">Button Test</button>

%{"myvar" => "My Variable test", "value" => ""}

I manage to have variable return successfully. :ok:

Test 3
<button phx-click="test" phx-value-myvar="<%= result %>" phx-throttle="1000">Button Test</button>

When I add the “=” it returns the following error.

(exit) an exception was raised:
** (ArgumentError) argument error
:erlang.apply([%{example1: “1”, example2: “Result End 1”, example3: 1, example4: “Critical”}, %{example1: “1”, example2: “Result End 2”, example3: 1, example4: “Low”}, %{example1: “1”, example2: “Result End 3”, example3: 1, example4: “Critical”}], :clause, [])
(project 0.1.0) lib/project_web/live/example_live/index.html.leex:48: anonymous fn/3 in ProjectWeb.ExampleLive.Index.render/1
(elixir 1.11.2) lib/enum.ex:2181: Enum."-reduce/3-lists^foldl/2-0-"/3
(project 0.1.0) lib/project_web/live/example_live/index.html.leex:41: anonymous fn/2 in ProjectWeb.ExampleLive.Index.render/1
(phoenix_live_view 0.15.7) lib/phoenix_live_view/diff.ex:356: Phoenix.LiveView.Diff.traverse/6
(phoenix_live_view 0.15.7) lib/phoenix_live_view/diff.ex:430: anonymous fn/4 in Phoenix.LiveView.Diff.traverse_dynamic/6
(elixir 1.11.2) lib/enum.ex:2181: Enum."-reduce/3-lists^foldl/2-0-"/3
(phoenix_live_view 0.15.7) lib/phoenix_live_view/diff.ex:356: Phoenix.LiveView.Diff.traverse/6
(phoenix_live_view 0.15.7) lib/phoenix_live_view/diff.ex:430: anonymous fn/4 in Phoenix.LiveView.Diff.traverse_dynamic/6
(elixir 1.11.2) lib/enum.ex:2181: Enum."-reduce/3-lists^foldl/2-0-"/3
(phoenix_live_view 0.15.7) lib/phoenix_live_view/diff.ex:356: Phoenix.LiveView.Diff.traverse/6
(phoenix_live_view 0.15.7) lib/phoenix_live_view/diff.ex:130: Phoenix.LiveView.Diff.render/3
(phoenix_live_view 0.15.7) lib/phoenix_live_view/static.ex:298: Phoenix.LiveView.Static.to_rendered_content_tag/4
(phoenix_live_view 0.15.7) lib/phoenix_live_view/static.ex:149: Phoenix.LiveView.Static.render/3
(phoenix_live_view 0.15.7) lib/phoenix_live_view/controller.ex:35: Phoenix.LiveView.Controller.live_render/3
(phoenix 1.5.7) lib/phoenix/router.ex:352: Phoenix.Router.call/2
(project 0.1.0) lib/project_web/endpoint.ex:1: ProjectWeb.Endpoint.plug_builder_call/2
(project 0.1.0) lib/plug/debugger.ex:136: ProjectWeb.Endpoint.“call (overridable 3)”/2
(project 0.1.0) lib/project_web/endpoint.ex:1: ProjectWeb.Endpoint.call/2
(phoenix 1.5.7) lib/phoenix/endpoint/cowboy2_handler.ex:65: Phoenix.Endpoint.Cowboy2Handler.init/4

Test 4
<button phx-click="test" phx-value-myvar="<%= result.example2 %>" phx-throttle="1000">Button Test</button>

(exit) an exception was raised:
** (ArgumentError) argument error
:erlang.apply([%{example1: “1”, example2: “Result End 1”, example3: 1, example4: “Critical”}, %{example1: “1”, example2: “Result End 2”, example3: 1, example4: “Low”}, %{example1: “1”, example2: “Result End 3”, example3: 1, example4: “Critical”}], :clause, [])
(project 0.1.0) lib/project_web/live/example_live/index.html.leex:48: anonymous fn/3 in ProjectWeb.ExampleLive.Index.render/1
(elixir 1.11.2) lib/enum.ex:2181: Enum."-reduce/3-lists^foldl/2-0-"/3
(project 0.1.0) lib/project_web/live/example_live/index.html.leex:41: anonymous fn/2 in ProjectWeb.ExampleLive.Index.render/1
(phoenix_live_view 0.15.7) lib/phoenix_live_view/diff.ex:356: Phoenix.LiveView.Diff.traverse/6
(phoenix_live_view 0.15.7) lib/phoenix_live_view/diff.ex:430: anonymous fn/4 in Phoenix.LiveView.Diff.traverse_dynamic/6
(elixir 1.11.2) lib/enum.ex:2181: Enum."-reduce/3-lists^foldl/2-0-"/3
(phoenix_live_view 0.15.7) lib/phoenix_live_view/diff.ex:356: Phoenix.LiveView.Diff.traverse/6
(phoenix_live_view 0.15.7) lib/phoenix_live_view/diff.ex:430: anonymous fn/4 in Phoenix.LiveView.Diff.traverse_dynamic/6
(elixir 1.11.2) lib/enum.ex:2181: Enum."-reduce/3-lists^foldl/2-0-"/3
(phoenix_live_view 0.15.7) lib/phoenix_live_view/diff.ex:356: Phoenix.LiveView.Diff.traverse/6
(phoenix_live_view 0.15.7) lib/phoenix_live_view/diff.ex:130: Phoenix.LiveView.Diff.render/3
(phoenix_live_view 0.15.7) lib/phoenix_live_view/static.ex:298: Phoenix.LiveView.Static.to_rendered_content_tag/4
(phoenix_live_view 0.15.7) lib/phoenix_live_view/static.ex:149: Phoenix.LiveView.Static.render/3
(phoenix_live_view 0.15.7) lib/phoenix_live_view/controller.ex:35: Phoenix.LiveView.Controller.live_render/3
(phoenix 1.5.7

It looks like you call something like result.clause somewhere in your template and this result happens to be a List. Can you make sure it’s not the case?