How to limit iteration of elements in 'v-for'

I am creating a small application in Vuejs 2.0I have approximately 15 iterative elements. I want to limit v-foronly 5 elements and have more buttons to display the whole list. Are there any possibilities?

+15
source share
2 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: a small change that makes this solution work with both arrays and objects

<div v-if="showLess">
  <div v-for="(value,index) in object" v-if="index <= 5"></div>
</div> 
<div v-else> 
  <div v-for="value in object"></div>
</div> 
<button @click="showLess = false"></button>
+51
source

you can try this solution for ...

<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 a limit in the data

data() {
  return {
    limitationList:5
  };
},

and install the btn function in you

  <button  @click="updateLimitation(limitationList)">
    show {{limitationList == 5 ? 'more' : 'less'}}
  </button>

and these are your methods

updateLimitation(limitationList){
  if (this.limitationList == this.items.length) {
    this.limitationList = 5
  }else{
    this.limitationList = this.items.length
  }
}

I hope you find it useful ...

+7

All Articles