admin管理员组文章数量:1405759
I'm working on printing out the page numbers a user may click on for a pager ponent in Vue.js. I see that the docs clearly say v-for can be used for a range:
v-for can also take an integer. In this case it will repeat the template that many times.
<div> <span v-for="n in 10">{{ n }} </span> </div>
Result:
1 2 3 4 5 6 7 8 9 10
Per .html#v-for-with-a-Range
I have not found anyway to specify the starting value for n. It seems that it starts at 1 and increments by 1 until 10.
Is there anyway to start n at, say, 5?
It almost seems like I'll need to create a method or puted property that will return an array of the exact values I want iterated in place of the static 10.
Anyone have any other thoughts?
I'm working on printing out the page numbers a user may click on for a pager ponent in Vue.js. I see that the docs clearly say v-for can be used for a range:
v-for can also take an integer. In this case it will repeat the template that many times.
<div> <span v-for="n in 10">{{ n }} </span> </div>
Result:
1 2 3 4 5 6 7 8 9 10
Per https://v2.vuejs/v2/guide/list.html#v-for-with-a-Range
I have not found anyway to specify the starting value for n. It seems that it starts at 1 and increments by 1 until 10.
Is there anyway to start n at, say, 5?
It almost seems like I'll need to create a method or puted property that will return an array of the exact values I want iterated in place of the static 10.
Anyone have any other thoughts?
Share edited Jul 14, 2022 at 2:30 tony19 139k23 gold badges278 silver badges348 bronze badges asked Sep 29, 2020 at 22:19 jasonjason 2,2695 gold badges37 silver badges68 bronze badges3 Answers
Reset to default 5There is no way to start a v-for
at n.
However, starting at an offset is as simple as adding the offset to your value and then stopping when you hit max.
<div>
<template v-for="n in max">
<span v-if="n + offset <= max">{{ n + offset }} </span>
</template>
</div>
If you need more control, a puted
property is most definitely the way to go, as it will provide you full control over whatever you're iterating over.
<div>
<span v-for="n in putedArr">{{ n }} </span>
</div>
puted: {
putedArr() {
let arr = [];
for (var i = this.offset; i <= this.max; i++)
arr.push(i);
return arr;
},
...
One way is to define your own helper function to generate the sequence:
Vue.prototype.$range = function *(start, end, step = 1) {
for (let i = start; i <= end; i += step) {
yield i
}
}
new Vue({ el: '#app' })
<script src="https://cdnjs.cloudflare./ajax/libs/vue/2.6.11/vue.js"></script>
<div id="app">
<div v-for="i of $range(5, 10)">
{{ i }}
</div>
</div>
<div-for="index in Array(y).fill(x).map((x, y) => x + y)" :key="index">{{ index }}</div>
x
is the start position and y
is the count of subsequent numbers.
so this...
Array(6).fill(5).map((x, y) => x + y)
will should you an array like[5, 6, 7, 8, 9, 10]
to traverse.
I think this should be a more efficient answer.
本文标签:
版权声明:本文标题:javascript - How can I specify the starting value for a range when using the v-for directive in Vue.js v2.x? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744917696a2632097.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论