ca1989

ca1989

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!

Showing Posts 1 to 10

cmo

cmo

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

ca1989

ca1989 OP

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

Malian

Malian

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 ?

ca1989

ca1989 OP

Nope, no phx-update attr at all.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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

ca1989

ca1989 OP

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.

soup

soup

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?

ca1989

ca1989 OP

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!

soup

soup

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.

ca1989

ca1989 OP

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.

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews