I'm building a small application in Vuejs 2.0
I'm having approx 15 iterating elements I want to limit the v-for
for only 5 elements and can have more buttons to display the whole list. Is there any possibilities?

- 4,417
- 10
- 73
- 132

- 6,054
- 21
- 82
- 148
-
show us what you've tried... – Muthu Kumaran Oct 07 '17 at 16:00
-
1Possible duplicate of [VueJs how to make pagination with limiter and range..?](https://stackoverflow.com/questions/35596389/vuejs-how-to-make-pagination-with-limiter-and-range) – Roy J Oct 07 '17 at 16:18
7 Answers
You can try this code
<div v-if="showLess">
<div v-for="value in array.slice(0, 5)"></div>
</div>
<div v-else>
<div v-for="value in array"></div>
</div>
<button @click="showLess = false"></button>
You will only have 5 elements in the new array.
Update: Tiny change that makes this solution work with both arrays and objects
<div v-if="showLess">
<div v-for="(value,index) in object">
<template v-if="index <= 5"></template>
</div>
</div>
<div v-else>
<div v-for="value in object"></div>
</div>
<button @click="showLess = false"></button>

- 655
- 8
- 11

- 4,798
- 1
- 22
- 34
-
2As stated, this only works on **arrays**, objects will throw errors. – Fusseldieb Aug 24 '18 at 23:35
-
1@Fusseldieb Yes.. That's why it is very good practice to save your data **as arrays** even when you got data as object. It will help you to work with Vuejs in better way. – kuzey beytar Oct 01 '18 at 08:11
-
1
-
8it's a bad practice including a v-if and v-for on same element - try avoid and add a wrapper with the v-if condition – YonatanAr Feb 27 '19 at 13:18
-
@unplugged answer below is the correct answer, this is bad practise – BritishSam Jun 28 '20 at 16:28
-
This will lead to doubled code for the inner loop, better use a computed property as stated in the second answer, this way your html code is not doubled. – Kenten Fina Feb 10 '23 at 15:00
Am I too late? You can solve this using computed properties:
<div v-for="value in computedObj">{{value}}</div>
<button @click="limit = null">Show more</button>
Then in data:
data(){
return {
object:[], // your original data
limit: 5 // or any number you wish to limit to
}
}
And finally in your computed properties:
computed:{
computedObj(){
return this.limit ? this.object.slice(0,this.limit) : this.object
}
}
When your click the button, the limit is cleared and the whole data is shown/returned

- 655
- 8
- 11

- 851
- 7
- 12
you can try this solution to...
<div class="body-table div-table" v-for="(item,index) in items" :key="item.id" v-if="items && items.length > 0 && index <= limitationList">....
and set your limit in data
data() {
return {
limitationList:5
};
},
and set a function in you btn
<button @click="updateLimitation(limitationList)">
show {{limitationList == 5 ? 'more' : 'less'}}
</button>
and this your methods
updateLimitation(limitationList){
if (this.limitationList == this.items.length) {
this.limitationList = 5
}else{
this.limitationList = this.items.length
}
}
i hope useful for you...

- 680
- 6
- 17
-
2This solutions isn't good as vue's v-if will leave a blank comment for every element that didn't pass limit check. So you'll end up having many "" in your DOM. – PinkiNice Feb 06 '18 at 16:48
This is what computed
was made for (I'm using script setup
):
const paginatedList = computed(() => {
// for arrays
return yourArray.slice(0,5)
// for objects/proxies such as reactive, refs, other computed
// return yourObject.value.slice(0,5)
})
render it in a v-for
loop:
<div v-for="item in paginatedList">
<p>{{ item }}</p>
</div

- 4,417
- 10
- 73
- 132
for resolve that problem u can computed limit list in computed method
like this
<div class="body-table div-table" v-for="(item,index) in filterItems" :key="item.id">
....
<script>
export default {
data() {
return {
items: [],
limitationList:5
};
},
computed: {
filterItems () {
return this.items && this.items.length > 0 && (this.items.length - 1) <= this.limitationList // or any condition u want
}
}
}
</script>
I hope useful.

- 680
- 6
- 17
This is my solution, check that if your rendering list you must hide the root section in the iteration <li>
.
<ul role="list">
<li v-for="(actor,index) in this.cast" :key="actor.id" :class="{'your-hidden-class': index>5}">
<div v-if="index <= 5">
<img v-if="actor.profile_path" :src="'https://image.tmdb.org/t/p/original' + actor.profile_path" alt="" />
<img v-else src="/images/image-not-found.png" alt="" />
<div>
<div>
<h3>{{ actor.original_name }}</h3>
<p>{{ actor.character }}</p>
</div>
</div>
</div>
</li>
</ul>

- 31
- 3
{{index}}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<div v-for="(contact, index) in allContacts" v-if="index < limitNumber" id="contacts">
<p>{{index}}</p>
</div>
</div>
<script>
new Vue({
el: "#app",
data:{
filterId:'10',
limitNumber:10,
allContacts:[{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},],
},
});
</script>
-
As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 23 '23 at 23:11