Load 4-in-4 items into a javascript loop

Asked

Viewed 83 times

0

I’m making a request on an API that returns some events, however I only need to display 4 items first, and if a "See More" button is clicked.. 4 more items should appear at a time.. I have the following code:

    jQuery.ajax({
        type: 'GET',
        url: 'url'
    }).then(function(res) {
        //Dando um console aqui consigo ver todos os itens listados

       // Como consigo validar para exibir somente 4 itens..
       // E em seguida ao clicar exibir mais 4 ?

        res.events.map(function(item) {
           // Resumi essa parte..
           // Aqui faço o loop de todos os itens
           // Monto ele em uma div e renderizo no html.
        });

     });

Thank you!

  • Do you want to make this pagination on client or server?

  • In client, because I do not have access to the API code.

1 answer

4


What you need is what they call Chunks, which is basically to divide a list into smaller lists of the maximum size defined. To do this, just use the method slice of Array to get each slice of your complete list. For ease, I have created a generator that will iterate over the sub-lists:

function* paginate(list, size) {
  for (let i = 0, j = list.length; i < j; i += size) {
    yield list.slice(i, i+size)
  }
}

const eventos = [
  'Evento 01',
  'Evento 02',
  'Evento 03',
  'Evento 04',
  'Evento 05',
  'Evento 06',
  'Evento 07',
  'Evento 08',
  'Evento 09',
  'Evento 10',
  'Evento 11',
  'Evento 12'
]

const paginas = paginate(eventos, 4)

for (let pagina of paginas) {
  console.log(pagina)
}

So, all you have to do is increase the logic that when the user presses the "See More" button, you search for the next value of the generator and add to the list of elements.

function* paginate(list, size) {
  for (let i = 0, j = list.length; i < j; i += size) {
    yield list.slice(i, i+size)
  }
}

const eventList = [
  'Evento 01', 'Evento 02', 'Evento 03', 'Evento 04',
  'Evento 05', 'Evento 06', 'Evento 07', 'Evento 08',
  'Evento 09', 'Evento 10', 'Evento 11', 'Evento 12'
]

const list = document.getElementById('lista')
const button = document.getElementById('btn')
const pages = paginate(eventList, 4)

button.addEventListener('click', function() {
  const page = pages.next()
  const events = page.value
  
  if (page.done) {
    this.disabled = true;
    return;
  }
  
  for (let event of events) {
    list.innerHTML += `<li>${event}</li>`
  }
});
<ul id="lista"></ul>
<button id="btn">Ver Mais</button>

  • 1

    Interesting use of generators!

  • Cool brother, I’ll try to implement this logic, thank you.

  • Brother this example. is very close to what I need.. however in the project I am doing maintenance, has not configured Ecmascript.. how can I do the same using pure javascript? Thank you!

  • @wDrik think just use var in place of const and tie a bow normal in place of for .. of

  • Right.. I just don’t understand the part where you use a ' * ' in the function.. and a Yield in for loop, gave a researched but did not understand the concept nor how to apply it in the most basic javascript. I’m sorry but I’m a little layman. Thank you!

  • The asterisk and the yield are interconnected. Both are used to define a generator. The generator does what we call Lazy-Evaluation, processing each value only on demand. https://answall.com/q/191752/5878

  • Cool brother. Thanks, I’ll try here!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.