For reference I’m using laravel 8 with blade syntax.
This line of code is part of a vue component I made:
<li class="w-3/12 md:w-auto pt-0 md:px-4 md:pt-2 md:pb-0 list-none relative"
v-if="comic.item_type === 'b' && comic.stock > 0"
v-for="(comic, index) in [...list].slice(0, 20)">
in the
.slice(0, 20)
I want to make the 20 dynamic. I’m using the V-for lop to render items on a page, but I want to limit the rendered items to 20 instances, until a ‘load more’ button is clicked, so the plan is to create a function that increases this parameter by 20 each time it is clicked.
I’m new to Vue, so if there is a better way to do this, please let me know!
I have tried making the second parameter a bound class, but it just (obviously) throws an error, as it’s not supposed to be used that way.
2
Answers
I would go for a computed list variable (containing the pre-filtered actual list) along with an
loadMoreTimes
index (how much the "load more"-button was pressed) that provides a simple access to the filtered list like so:You can then simply loop over that array
BTW: it is not a good practice to use
v-if
andv-for
in the same element.Try like following snippet: