diff --git a/lib/moon/design/table.ex b/lib/moon/design/table.ex index ce16a2750..a82209b84 100644 --- a/lib/moon/design/table.ex +++ b/lib/moon/design/table.ex @@ -19,6 +19,9 @@ defmodule Moon.Design.Table do @doc "Event that firset on row click" prop(row_click, :event) + @doc "Callback for generating on_click per row. row and row_id will bi given as parameters" + prop(row_click_cb, :any) + @doc "Sorting stuff" prop(sorting_click, :event) @@ -119,14 +122,16 @@ defmodule Moon.Design.Table do {#for {row_index, item} <- stream_data(assigns)} {#for {col, col_index} <- Enum.with_index(@cols)} sort_items(sort) |> Enum.with_index(&{&2, &1}) end + + defp dom_id(id, _) when is_binary(id), do: id + defp dom_id(id, id2), do: "#{id2}-row-#{id}" end diff --git a/lib/moon_web/examples/design/table_example/stream.ex b/lib/moon_web/examples/design/table_example/stream.ex index 242dee48d..67f969dd0 100644 --- a/lib/moon_web/examples/design/table_example/stream.ex +++ b/lib/moon_web/examples/design/table_example/stream.ex @@ -25,7 +25,11 @@ defmodule MoonWeb.Examples.Design.TableExample.Stream do def render(assigns) do ~F"""
- "stream"}}> +
"stream"}} + row_click="row_click" + > {model.id} @@ -40,6 +44,14 @@ defmodule MoonWeb.Examples.Design.TableExample.Stream do """ end + def handle_event("row_click", params, socket) do + data = + models() + |> Enum.map(&(("#{&1.id}" == params["selected"] && Map.put(&1, :is_selected, true)) || &1)) + + {:noreply, stream(socket, :models, data, reset: true)} + end + def code() do """ use Phoenix.LiveView @@ -80,6 +92,11 @@ defmodule MoonWeb.Examples.Design.TableExample.Stream do \""" end + def handle_event("row_click", params, socket) do + data = models() + |> Enum.map(&("\#{&1.id}" == params["selected"] && Map.put(&1, :is_selected, true) || &1)) + {:noreply, stream(socket, :models, data, reset: true )} + end """ end end