bishoy301

bishoy301

Hello everyone, I’m new to Elixir, Phoenix, and Liveview so I’m hoping that this question isn’t too basic.

I have a Liveview that currently should show a different set of HTML based on an assign that I update on click:

    <div>
    <div style="height: 70px; background-color: #00558c;">
            <div style="width: 95%; margin: auto; display: flex; flex-direction: row; justify-content: space-between">
            <div style="color: white; font-size: 30px; font-family: Arial; margin-top: 18px; font-weight: bold;">HL7 Visualizer</div>
            <label class="switch"><input type="checkbox" id="togBtn" class="<%= if @mode == :mode2 do %>checked <% end %>"><div class="slider round" phx-click="toggle"><!--ADDED HTML --><span  phx-click="message_list" class="on"><%= @mode %></span><span class="off"><%= @mode %></span><!--END--></div></label>

    </div>
</div>
    <%= if @mode == :mode1 do %>
        <div style="margin-top: 30px">
            <%= for msg <- @raw_msg do %>
                <div style="margin-bottom: 10px; max-width: 95%; margin-left: auto; margin-right: auto; overflow-wrap: break-word; overflow-y: hidden;"><%= msg %></div>
            <% end %>
        </div>
    <% else %>
        <div class="font-mono bg-white rounded-lg shadow-xl w-full px-6 py-3">
            <%= for {seg, idx} <- Enum.with_index(@msg_list) do %>
            <div class="block border border-gray-250 rounded-sm px-4 py-2 whitespace-no-wrap"><%= Enum.at(seg, 0) %></div>
            <div id="seg"  phx-click="show-detail" style="margin-right: 0px !important" class="ml-5 mt-1 mr-0 flex flex-wrap"><%= for {field, idx} <- Enum.with_index(seg) do %>
                <%= if field == "" do %>
                    <span class="block border border-gray-250 rounded-sm px-1 py-2 whitespace-no-wrap "><%= field %></span>
                <%= else %>
                    <span class="block border border-gray-250 rounded-sm px-4 py-2 whitespace-no-wrap  max-w-md overflow-hidden" style="text-overflow: ellipsis;"> <%= field %></span>
                <%= end %>
            <% end %></div>
            <% end %>
        </div>

        <%= if @show_detail == :true do %>
            <div class="ml-5 mt-3 flex-col flex flex-wrap" style="max-height: 300px"><%= for comp <- @msg_seg do %>
                <div class="flex w-200"><%= comp %></div>
                <% end %></div>
                <!--<%= render("detail.html", assigns) %>-->
        <% end %>
    <% end %>

When I load the liveview, the first set of HTML is rendered correctly and when I press the button that updates the @mode assign, it renders the new HTML correctly as well. My issue is that when I try to press the button again to set the mode back to :mode1 and render the first set of HTML again, the liveview actually renders both sets of HTML on top of each other in a weird way. I can’t seem to figure out what the issue is here. Also, I would appreciate it if any experts could illuminate a better way to achieve this if there is one

Edit: Here’s the Liveview code as well

defmodule MyAppWeb.VisualizerView do
  use Phoenix.LiveView

  def render(assigns) do
    MyAppWeb.PageView.render("visualizer.html", assigns)
  end

  def mount(_session, socket) do
    raw_msg = raw_message()
    msg_list = message_list()
    msg_seg = message_seg(0)
    {:ok, assign(socket, mode: :mode1, raw_msg: raw_msg, msg_list: msg_list, msg_seg: msg_seg, show_detail: :false)}
  end

  def handle_event("toggle", _, socket) do
    {:noreply, update(socket, :mode, fn
      :mode1 -> :mode2
      :mode2 -> :mode1
    end )}
  end

  def handle_event("show-detail", _, socket) do
    {:noreply, update(socket, :show_detail, fn
      :false -> :true
      :true -> :false
    end )}
  end
end

Showing Posts 1 to 10

LostKobrakai

LostKobrakai

It might be an issue with morphdom not correctly detecting the changed parts to update. Maybe try wrapping tags around those string values.

bishoy301

bishoy301 OP

Sorry, what are the string values you’re referring to?

kokolegorille

kokolegorille

Probably this one :slight_smile:

bishoy301

bishoy301 OP

Oh sorry, I have actual HTML wrapped in divs there, I just left it out so that I didn’t clutter up the post.

kokolegorille

kokolegorille

No need to be sorry :slight_smile: , but the code You put has no tags, so it can be confusing

chrismccord

chrismccord

Creator of Phoenix

Can you share your exact template and LV code? It may be an invalid markup issue or possibly a bug on our side, but we need to know more to say for sure. Thanks!

bishoy301

bishoy301 OP

I’ve added the full template and LV code, just changed the name of the module and removed some functions that have nothing to do with the template

chrismccord

chrismccord

Creator of Phoenix

It looks like the issue is you are duping the seg id. Every container in one of the comprehensions is getting the same id (<div id="seg"...) which is breaking our DOM diffing because morphdom does not know how to diff the duplicate IDs. This may be something we can detect on the morphdom side, but the correct fix would be to drop the ID or assign a unique ID for each container.

bishoy301

bishoy301 OP

Thanks! This was the problem. I’m new to web and didn’t even know about that. I appreciate you taking the time to answer the question.

ehayun

ehayun

I solved it like this

<div class="page">
 
        <%= case @display do %>
          <% "chat" -> %>
            <%= render "chat_room.html", assigns %>
          <% "members" -> %>
            <%= render "chat_users.html", assigns %>
        <% _ -> %>
        <% end %>

        <%= render "chat_menu.html", assigns %>

      </div>
— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews