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 ?
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.
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.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With