Sebb

Sebb

Liveview + bootstrap accordion

I have a two level hierarchy that I want to render in a bootstrap accordion.
It looks like this:

first-level-item-text-1
   second-level-item-text-1-1
   second-level-item-text-1-2
first-level-item-text-1
   second-level-item-text-2-1
   ...

Normally I would put phx-update="ignore" on the accordion, to avoid that it is being closed on each render, but I can’t do that because I want to change the data that is displayed in a liveview - Is there a way to do that?

here is some code

<!-- cant put phx-update=ignore here -->
<div class="accordion accordion-flush p-1" id="blockAccordion"> 
<%= for first_level_item in first_level_items %>
<div class="accordion-item">
  <h2 class="accordion-header">
    <button data-bs-target="#flush-collapse-<%= first_level_item.id %>">
      <%= first_level_item.text %>
    </button>
  </h2>
  <div id="flush-collapse-<%= first_level_item.id %>" class="accordion-collapse collapse"
    data-bs-parent="#blockAccordion">
    <div class="accordion-body p-0 m-0">
      <ul class="list-group list-group-flush">
        <%= for second_level_item <- first_level_item.second_level_items do %>
          <%= render_second_level(second_level_item.id, assigns) %>
        <% end %>
      </ul>
    </div>
  </div>
</div>
<% end %>
</div>

Marked As Solved

sfusato

sfusato

First option would be to add a LiveView Hook that would re-initialize Bootstrap JS on each mounted & update LiveView callback. From Bootstrap docs, it looks like this for V5:

var collapseElementList = [].slice.call(document.querySelectorAll('.collapse'))
var collapseList = collapseElementList.map(function (collapseEl) {
  return new bootstrap.Collapse(collapseEl)
})

Second option, have you considered using Alpine? You can enhance your LiveView templates with Alpine without the need of phx-update="ignore" and also without the need of hooks manually re-initializing JS libraries due to how it’s set up to work with LiveView (check the end of the section of the previous link).

Basically, you will use Alpine instead of Bootstrap JS code to handle the accordion state for you. Something like:

<div class="accordion accordion-flush p-1" id="blockAccordion"> 
<%= for first_level_item in first_level_items %>
+ <div x-data="{open: false}" id="accordion_<%= first_level_item.id %>" class="accordion-item">
  <h2 class="accordion-header">
+   <button @click="open=!open">
      <%= first_level_item.text %>
    </button>
  </h2>
+ <div x-show="open" x-cloak class="accordion-collapse collapse">
    <div class="accordion-body p-0 m-0">
      <ul class="list-group list-group-flush">
        <%= for second_level_item <- first_level_item.second_level_items do %>
          <%= render_second_level(second_level_item.id, assigns) %>
        <% end %>
      </ul>
    </div>
  </div>
</div>
<% end %>
</div>
  • x-data initializes Alpine; we set open to false;
  • on button click, it toggles the value;
  • the accordion is only displayed when open==true;
  • Patrick Thompson’s blog has some great articles on LiveView & Alpine working together;

Also Liked

cj5

cj5

If all you want to do is keep the bootstrap state through LiveView updates without marking the container with phx-update="ignore", it is possible to use onBeforeElUpdated hook. For example, inside your app.js where the live socket is initialized:

const liveSocket = new LiveSocket('/live', Socket, {
  // ...
  dom: {
    onBeforeElUpdated (from, to) {
      // Clone bs-collapse state
      if (from.classList.contains('collapse') && from.classList.contains('show')) {
        to.classList.add('show');
      }
    },
  },
});

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
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

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement