Sebb

Sebb

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>

Showing Posts 1 to 3

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;
Sebb

Sebb OP

thanks for your help.

Yes I considered Alpine, but I thought for this project I go the easy path.
It turns out, that there is too much magic happening I do not understand.
I think Bootstrap and LiveView is really nice, if you know what you are doing, … but I don’t.

a LiveView Hook that would re-initialize Bootstrap JS on each mounted & update LiveView callback

I’ll consider this, but I’m not happy with more Javascript stuff going on I don’t understand.

I think I’ll go the Alpine-and-some-CSS-framework-without-JS-path, it’s a little more work, but at least its possible to understand without being a JS-pro.

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');
      }
    },
  },
});
— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
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

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
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
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews