I have a list of news, initially the first 10 come from the API. Then, if I press the "load more" button, the next 10 appear, and so on.
I want to build an html block that respects a structure something like this: (repeating every 10 news, and in the last load that shows the ones there are, which will generally be less than 10)
At startup, the first page was loaded
<div class="row">
{{ noticia 1 }}
{{ noticia 2 }}
</div>
<div class="row">
{{ noticia 3 }}
{{ noticia 4 }}
</div>
<div class="row">
{{ noticia 5 }}
{{ noticia 6 }}
{{ noticia 7 }}
</div>
<div class="row">
{{ noticia 8 }}
{{ noticia 9 }}
{{ noticia 10 }}
</div>
Click on load more
<div class="row">
{{ noticia 1 }}
{{ noticia 2 }}
</div>
<div class="row">
{{ noticia 3 }}
{{ noticia 4 }}
</div>
<div class="row">
{{ noticia 5 }}
{{ noticia 6 }}
{{ noticia 7 }}
</div>
<div class="row">
{{ noticia 8 }}
{{ noticia 9 }}
{{ noticia 10 }}
</div>
<div class="row">
{{ noticia 11 }}
{{ noticia 12 }}
</div>
<div class="row">
{{ noticia 13 }}
{{ noticia 14 }}
</div>
<div class="row">
{{ noticia 15 }}
{{ noticia 16 }}
{{ noticia 17 }}
</div>
<div class="row">
{{ noticia 18 }}
{{ noticia 19 }}
{{ noticia 20 }}
</div>
Sorry for the repetitive code, I wanted to be clear. I don't know how to build this structure inside av-for
Data chunking / Data splitting
What you are looking for is what we normally call "chunking", breaking a data set into smaller sets (chunks). There are also those who call it split or splitting.
First, if it
noticias
were an array likeConvert it to an array of arrays, style.
If you
cargar más
make any API requests or whatever gives you another normal array, just split the additional data into chunks and add them, keeping in mind that the last chunk may still have a free slot.How to partition arrays into regular subsets (chunks)
I leave you a simple function
To convert your
noticias
to chunks (skipping typing issues, I don't know if you use TypeScript)If you happen to use a data processing library such as lodash , they usually already have this function, or a similar one.
With irregular sets a little more is needed
If you want to have chunks of 2 and 3 in a controlled way, you will have to make a custom algorithm. You can start from the suggested one and add some modification to it. For example, instead of a fixed size, you could have a set of sizes that are applied in order (and cyclically)
Example function to split into 2, 2, 3 and 3
NOTE: I'm leaving it with while because it's easier to see the variables update on each loop. to your liking
As you can see, it is cyclic according to the passed sizes array. The first two chunks will have size 2, the next two will be size 3, the next two will return to size 2, etc...
Chunk rendering in Vue
Veo que le estás dando una definición espacial a los chunks, y los entiendes como filas (rows), algo habitual y completamente correcto. Podrías recorrer el array de filas con un for, y con un for interno recorrer cada una de las noticias del chunk.
NOTA: usando div porque no sé qué semántica estás usando. Típicamente para una noticia se puede utilizar
<article>
. Si no quieres ninguna etiqueta (ni div, ni article, solo el texto plano como sugieres) puedes usar la etiqueta<template>
que no aparecerá en el resultado final.NOTA 2: dependiendo tu versión de Vue y de la forma de tus datos, su orden y frecuencia de actualización, puede ser conveniente usar la directiva :key en v-for.
IMPORTANTE Consejo adicional: considera solo CSS
Sometimes, to display news boards with different sizes and layouts, it is not even necessary to modify the data structure in the html (DOM) nor in the logical data, and it is a matter of rendering with CSS. If you really don't need to structure the data and you're just looking for a layout, you might be interested in simply having all the news in a row and modifying its layout with CSS. display: grid is very useful for this, but it can also be done in traditional ways with the nth-of-type or nth-child pseudoselector.