Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render v-for from particular index

Tags:

vue.js

I want to loop in v-for from suppose from number 5 to 10 . The loop want to be started at 5 and end at 10.

I have tried this

<div v-for="n in 10" v-if="n>=5"></div>

But I want more effective way of doing the loop . Does anyone knows how to do so that loop starts at 5 ?

like image 265
ashok poudel Avatar asked Apr 08 '18 07:04

ashok poudel


People also ask

What is conditional rendering in Vue?

Conditional rendering refers to the ability to render distinct user interface (UI) markup based on whether a condition is true or not. This notion is frequently used in contexts like showing or hiding components (toggling), switching application functionality, handling authentication and authorization, and many more.

What is key in V for?

The purpose of this key attribute is to give "a hint for Vue's virtual DOM algorithm to identify VNodes when diffing the new list of nodes against the old list" (from Vue. js Docs). Essentially, it helps Vue identify what's changed and what hasn't.

How does Vue reconcile changes to the DOM?

When virtual nodes start to change Vue compares the new and the old state and decides if the DOM needs to be updated. This process is called reconciliation. If a change is required only the associated DOM nodes will be altered with the rest of the tree to remain intact.


2 Answers

Its possible for your example:

<div v-for="i in range(5, 10)">
... some code here
</div>

and mount range function:

var app = new Vue({
  el: '#app',
  methods:{
    range : function (start, end) {
      return Array(end - start + 1).fill().map((_, idx) => start + idx)
   }
  }
});

and sample in jsfidle.

If want know more than this github issue can help you.

like image 90
A1Gard Avatar answered Sep 19 '22 18:09

A1Gard


Also try this simple approach:

new Vue({
  el: '#test',
  data: {
    items: [
      '1',
      '2',
      '3',
      '4',
      '5',
      '6',
      '7',
      '8',
      '9',
      '10'
    ]
  },
  methods: {
    sliceItems: function (start, end) {
      return this.items.slice(start, end);
    }
  }
})
<script src="https://unpkg.com/[email protected]/dist/vue.min.js"></script>
<div id="test">
  <ul>
    <li v-for="(item, index) in sliceItems(5,10)" >{{ item }}</li>
  </ul>
</div>
like image 33
Ali Ezzat Odeh Avatar answered Sep 19 '22 18:09

Ali Ezzat Odeh