fix: select all checkbox handling
This commit is contained in:
parent
bb7e3cbe77
commit
124ab295a6
3 changed files with 41 additions and 26 deletions
|
|
@ -90,11 +90,14 @@ defmodule MvWeb.UserLive.Index do
|
|||
# Selects one user in the list of users
|
||||
@impl true
|
||||
def handle_event("select_user", %{"id" => id}, socket) do
|
||||
# Normalize ID to string for consistent comparison
|
||||
id_str = to_string(id)
|
||||
|
||||
selected =
|
||||
if id in socket.assigns.selected_users do
|
||||
List.delete(socket.assigns.selected_users, id)
|
||||
if id_str in socket.assigns.selected_users do
|
||||
List.delete(socket.assigns.selected_users, id_str)
|
||||
else
|
||||
[id | socket.assigns.selected_users]
|
||||
[id_str | socket.assigns.selected_users]
|
||||
end
|
||||
|
||||
{:noreply, assign(socket, :selected_users, selected)}
|
||||
|
|
@ -129,7 +132,8 @@ defmodule MvWeb.UserLive.Index do
|
|||
def handle_event("select_all", _params, socket) do
|
||||
users = socket.assigns.users
|
||||
|
||||
all_ids = Enum.map(users, & &1.id)
|
||||
# Normalize IDs to strings for consistent comparison
|
||||
all_ids = Enum.map(users, &to_string(&1.id))
|
||||
|
||||
selected =
|
||||
if Enum.sort(socket.assigns.selected_users) == Enum.sort(all_ids) do
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@
|
|||
type="checkbox"
|
||||
name="select_all"
|
||||
phx-click="select_all"
|
||||
checked={Enum.sort(@selected_users) == Enum.map(@users, & &1.id) |> Enum.sort()}
|
||||
checked={Enum.sort(@selected_users) == Enum.map(@users, &to_string(&1.id)) |> Enum.sort()}
|
||||
aria-label={gettext("Select all users")}
|
||||
role="checkbox"
|
||||
/>
|
||||
|
|
@ -26,10 +26,10 @@
|
|||
>
|
||||
<.input
|
||||
type="checkbox"
|
||||
name={user.id}
|
||||
name={to_string(user.id)}
|
||||
phx-click="select_user"
|
||||
phx-value-id={user.id}
|
||||
checked={user.id in @selected_users}
|
||||
phx-value-id={to_string(user.id)}
|
||||
checked={to_string(user.id) in @selected_users}
|
||||
phx-capture-click
|
||||
phx-stop-propagation
|
||||
aria-label={gettext("Select user")}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue