Hello everyone,
so I solved the last problem but now I’m facing two new ones. I’m working on them over 1 hour but I can’t find a solution.
Problem 1:
I have a custom dropdown with 2 disciplines for this example.
The code looks like this:
defmodule ExampleWeb.IndexSearch.DisciplineDropdownComponent do
use ExampleWeb, :live_component
def mount(socket) do
socket =
assign(socket,
disciplines: [
"Football",
"Basketball"
],
checked_disciplines: []
)
{:ok, socket}
end
def handle_event("check_discipline", params, socket) do
IO.inspect(params)
IO.puts("Triggered")
{:noreply, socket}
end
end
My shortened HTML template code is the following:
<form phx-target="<%= @myself %>" phx-change="check_discipline">
<%= for element <- @disciplines do %>
<div class="p-2 cursor-pointer">
<input class="mr-2" type="checkbox" id="<%= element %>" value="<%= element %>" name="checked_disciplines[]" />
<label for="<%= element %>"><%= element %></label>
</div>
<% end %>
</form>
Whenever I check or uncheck the change event isn’t emitted.
The outcome should be something like:
Dropdown:
[ ] Football
[ ] Basketball
Nothing selected: checked_disciplines = []
[ x ] Football
[ ] Basketball
1 selected: checked_disciplines = ["Football"]
[ x ] Football
[ x ] Basketball
Both selected: checked_disciplines = ["Football", "Basketball"]