romenigld

romenigld

I am doing the course Phoenix Live View Studio and I am trying to add a tag <h2> when it’s updated the Hooks.InfiniteScroll.
I add this html code on the render function:

 def render(assigns) do
    ~L"""
    <div id="infinite-scroll">
      <h1>Pizza Lovers Timeline</h1>
      <div id="orders" phx-update="append">
        <%= for order <- @orders do %>
          <div class="order" id="<%= order.id %>">
            <div class="id">
              <%= order.id %>
            </div>
            <div>
              <div class="pizza">
                <%= order.pizza %>
              </div>
              <div>
                ordered_by
                <span class="username">
                  <%= order.username %>
                </span>
              </div>
            </div>
          </div>
          
          <%= if @page > 1 do %>
            <div id="page-number">
            </div>
          <% end %>
          
        <% end %>
      </div>

      <div id="footer"
           phx-hook="InfiniteScroll"
           data-page-number="<%= @page %>">
       <div class="loader">
          Load More...
       </div>
    </div>
    """
  end

and in the Hooks.infiniteScroll:

...

  updated() {
    const pageNumber = this.el.dataset.pageNumber
    console.log("updated", pageNumber)

    let h2 = document.createElement('h2')
    let text = document.createTextNode(`Page Number ${pageNumber}`)  
    h2.appendChild(text)

    let divpageNumber = document.querySelector("#orders #page-number")
    divpageNumber.appendChild(h2)

  },
...

I don’t know how to fix, can anyone help me with this?
I just want to show the Number of pages when each time updates the hooks.

Showing Posts 1 to 2

dbaer

dbaer

The phoenix code looks good!

Try setting a class on the created element and then removing elements with that class before adding the new one.

Then a bit of CSS to affix the H2 to the top of the page should do it so you can see it always in the same place.

I’m not sure how particular the forum is about it, but this is really more of a Javascript related question than Phoenix.

Also a caveat to that is the page number wont update if you scroll back up. It will stay as the most recent page you loaded.

romenigld

romenigld OP

Yes I understand, my intentation was to show the number of the pages each time it updates and append the value of the <h2>.
I was thinking it will be better to show the value on a float right nav. But how you told me if I scroll back up don’t updates this.
It will need to code with the observer. I am right now just continuing the course and this sample don’t matter anymore.
Thank you for reply @dbaer, and maybe in the future I will try to do what you told me.

— 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
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

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
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews