Phoenix liveview checkboxes are not updated (the UI)

Hi all,

in a live component I render some checkboxes and a button to check all of them:

...
<%= for x <- @list_of_things do %>
      <input type="checkbox"
        id=...
        name=...
        phx-click="some-event"
        phx-value-column={elem(x, 1)}
        phx-target={@myself}
        checked={is_checked?(x, @another_list)}
      />
<% end %>
<button type="button" phx-click={JS.push("check-all", target: @myself)}>
...

Inside the event handler I update @another_list
{:noreply, assign(socket, another_list: [...])}

the is_checked? function gets called (I inspected inside it) and it is returning true a number of times equal to the number of checkboxes.

Anyway, the UI doesn’t show all the checkboxes as checked until I refresh the page.

What am I doing wrong here?

Cheers!

I think you’re going to need an id and/or name on those check boxes at least.

They went missing when I pasted, I have them (I am going to edit the original post)

I faced the same issue few weeks ago. My checkboxes were inside a phx-update="stream". In that case, the inner block is not updated, and this is the expected behaviour. Is it your case ?

Nope, no phx-update attr at all.

Do you have a form defined around those or not? Can you show a more complete set of code?

No, there’s no form around those.

Here is the whole snippet:

<div class="flex">
  <h4 class="pr-4"><%= dgettext("oha", "Visible columns") %></h4>
  <button
    type="button"
    phx-click="make-all-columns-visible"
    phx-target={@myself}
  >
    <span class="text-primary font-normal">(<%= dgettext("oha", "select all") %>)</span>
  </button>
</div>
<%= for col <- @all_columns, elem(col, 1) != :actions do %>
  <label class="custom-checkbox mt-2">
    <input
      type="checkbox"
      id={"col-#{elem(col, 1)}"}
      name="col"
      class="col-visibility-checkbox"
      phx-click="set-columns-visibility"
      phx-value-column={elem(col, 1)}
      phx-target={@myself}
      checked={col_checked?(col, @columns)}
    />
    <span/>
    <span><%= elem(col, 0) %></span>
  </label>
<% end %>

This is the col_checked? function

defp col_checked?(col, columns) do
  Enum.find_value(columns, false, fn c -> elem(c, 1) == elem(col, 1) end)
end

I have to mention that when the make-all-columns-visible event is handled, the table in the page really show all the columns, only the checkboxes are not updated.

Just to sense check, you are updating the @all_columns assign and putting the updated value back in the socket assigns right?

Since its a component, are the checkbox values getting passed in from a parent? Perhaps there is a value sync issue there?

Umm @all_columns does not change. Doesn’t need to change TBH. So maybe that’s why my checkboxes aren’t re-rendered? The comprehension is simply ignored?

Is there any way around this?

Cheers!

Ah sorry, I miscomprehended the comprehension.

There is a erroneous <span/> tag in the code above, but probably just copy paste bug, I dont think heex would compile as given, probably not the issue.

If @columns is changing, then it will re-render, so I suspect the updated value isn’t getting propagated to the checkbox component.

Is @columns owned by the component or the parent view? Who is updating it (the checkbox component or the parent view?) when things are toggled (sounds like its the component by the first post)? Are the checkboxes in the same component as the table? Is it possible that Component#update(assigns, socket) is getting called and re-initialising the @columns to some stale?

I guess I would also check another browser just to be sure its not a odd sticky-html-quirk.

I believe this is valid heex, equivalent to <span></span> (90% sure :))

Yes, @columns is changing, but it is inside a comprehension where @all_columns is not changing.
I can confirm @columns is changing because it is used to show and hide table columns (and it’s working fine).

@columns is passed to this component from the parent view, the first time I assign it to @all_columns and then the checkboxes updates @columns.

Tried on other browsers with no luck.

Yeah Im an idiot.

This works for me, so, IDK, good luck.

defmodule PhxcomponentsWeb.Index2Live do
  use PhxcomponentsWeb, :live_view

  defmodule Component do
    use PhxcomponentsWeb, :live_component

    def update(assigns, socket) do
      socket =
        socket
        |> assign(:all_columns, assigns.all_columns)
        |> assign(:columns, assigns.all_columns)

      {:ok, socket}
    end

    def handle_event("toggle-one", params, socket) do
      # bit jank but whatever
      columns =
        case params do
          %{"column" => col_key, "value" => "on"} ->
            [{col_key, String.to_existing_atom(col_key)} | socket.assigns.columns]
            |> Enum.uniq()

          %{"column" => col_key} ->
            Enum.reject(socket.assigns.columns, fn {_name, key} ->
              key == String.to_existing_atom(col_key)
            end)
        end

      socket = assign(socket, :columns, columns)

      {:noreply, socket}
    end

    def handle_event("toggle-all", _params, socket) do
      Enum.reduce(
        ["colors", "dogs", "cats"],
        {:noreply, socket},
        fn key_string, {:noreply, socket} ->
          handle_event("toggle-one", %{"column" => key_string, "value" => "on"}, socket)
        end
      )
    end

    defp col_checked?(target_key, columns) do
      Enum.any?(columns, fn {_col_name, col_key} -> col_key == target_key end)
    end

    def render(assigns) do
      ~H"""
      <div>
        <%= for {name, key} <- @all_columns do %>
          <input
            type="checkbox"
            id={"id-#{key}"}
            name={"col-#{key}"}
            phx-click="toggle-one"
            phx-value-column={key}
            phx-target={@myself}
            checked={col_checked?(key, @columns)}
          />
          <label for={"id-#{key}"}><%= name %></label>
        <% end %>
        <button class="ml-8 border" type="button" phx-click={JS.push("toggle-all", target: @myself)}>
          check all
        </button>
        <div class="flex gap-4">
          <%= for {name, _key} <- @columns do %>
            <span><%= name %></span>
          <% end %>
        </div>
      </div>
      """
    end
  end

  def mount(_params, _session, socket) do
    socket =
      assign(
        socket,
        :all_columns,
        [{"colors", :colors}, {"dogs", :dogs}, {"cats", :cats}]
      )

    {:ok, socket}
  end

  def render(assigns) do
    ~H"""
    <div>
      <.live_component module={Component} all_columns={@all_columns} id={:comp} />
    </div>
    """
  end
end

You could streamline this by removing the @columns assign entirely and instead adding a :visible field directly to each element @all_columns assign list and then update that field to true in the event handler for show all columns.

Then in the template, you could just access it directly with something like this: <input ... checked={col.visible}>. This means you wouldn’t be running a check for every column every time @columns gets updated.

Indeed. Problem is in the existing code the columns are tuples.
And they have 2 or 3 elements, hence I have some elem scattered around where I would have used pattern matching.

Another option could be to normalize the tuples, adding missing fields and the visible boolean.

I’ll think about that, thank you

The checkboxes were inside a dropdown activated by some old javascript.
I converted that old dropdown logic to LV and everything works fine now.

Thank you