LalarukhPixelative

LalarukhPixelative

Toggle-all isn't applying checkbox on every entry

I am following this tutorial: bulk_delete
But when i select the toggle-all checkbox it is not selecting the checkbox of every entry below in the table.
And giving me this error:

[error] GenServer #PID<0.3801.0> terminating
** (Protocol.UndefinedError) protocol Enumerable not implemented for "checked" of type BitString

Here’s my code:

<th>
<input type="checkbox" name="toggle-all" id="toggle-all" phx-click="toggle-all" phx-update="ignore" />
 </th>
<% checked = if (file.id in @toggle_ids), do: "checked", else: IO.inspect(@toggle_ids)%>
            <tr id={"file-#{file.id}"}>
                <td>
                <input type="checkbox" name="toggle" phx-click="toggle"
                phx-value-toggle-id={file.id} {checked} />
                </td>
                 //other fields
             </tr>

This is my index.ex code:

@impl true
  def handle_event("toggle-all", %{"value" => "on"}, socket) do
    file_ids = socket.assigns.files |> Enum.map(& &1.id)
    # IO.inspect(file_ids)
    {:noreply, assign(socket, :toggle_ids, file_ids)}
  end

  def handle_event("toggle-all", %{}, socket) do
    {:noreply, assign(socket, :toggle_ids, [])}
  end

  def handle_event("toggle", %{"toggle-id" => id}, socket) do
    id = String.to_integer(id)
    toggle_ids = socket.assigns.toggle_ids
    IO.inspect(toggle_ids)

    my_toggle_ids =
      if id in toggle_ids do
        Enum.reject(toggle_ids, &(&1 == id))
      else
        [id | toggle_ids]
      end

    {:noreply, assign(socket, :toggle_ids, my_toggle_ids)}
  end

Most Liked

moogle19

moogle19

If you use the interpolation in html tags without an attribute, it must contain a keyword list (or map) with your values. In your case you could replace your

<% checked = if (file.id in @toggle_ids), do: "checked", else: IO.inspect(@toggle_ids) %>

with

<% checked = if (file.id in @toggle_ids), do: [checked: true], else: [] %>

or write it as

<% checked = file.id in @toggle_ids %>
...
<td>
  <input type="checkbox" name="toggle" phx-click="toggle" 
    phx-value-toggle-id={file.id} checked={checked} />
</td>
LalarukhPixelative

LalarukhPixelative

Thank you soo much. Like i was stuck in it from around couple of hours. And now, Solved!!!
Thanks again. :smiley:

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48342 226
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52673 488
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New

We're in Beta

About us Mission Statement