bishoy301

bishoy301

Basics....Swapping content in Liveview when value is changed

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

First 10 of 10 Posts Switch mode

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?

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement