我有一个需要以页面格式显示的javascript对象。例如,从下面的对象中,我需要基于perPage
变量的值将此页面显示为页面。
{
"fruits":[
{
"from":"Shanghai",
"to":"Houston",
"createdOn":"2019-02-20 17:02:45",
"threadId":"1234564534"
},
{
"from":"Mumbai",
"to":"Texas",
"createdOn":"2019-02-22 17:02:45",
"threadId":"223455678"
}
],
"vegetables":[{
"from":"Barcelona",
"to":"Milan",
"createdOn":"2019-01-20 10:02:45",
"threadId":"45673456"
}],
"paper":[{
"from":"Kualalumpur",
"to":"Singapore",
"createdOn":"2019-02-01 12:02:45",
"threadId":"234222345"
},
{
"from":"Singapore",
"to":"Vancover",
"createdOn":"2019-01-20 11:02:45",
"threadId":"6756434343"
}],
"books":[{
"from":"Jibooty",
"to":"Ahmedabad",
"createdOn":"2019-02-10 17:02:45",
"threadId":"23456789"
}],
"toys":[{
"from":"Shanghai",
"to":"Houston",
"createdOn":"2019-02-20 14:02:45",
"threadId":"123434343"
}],
"electronics":[{
"from":"Somalia",
"to":"Angora",
"createdOn":"2019-02-20 17:02:45",
"threadId":"667676767"
}]
}
如果perPage
的值为5,则应显示如下
<div class="page">
fruits
vegetables
paper
books
toys
</div>
<div class="page">
electronics
</div>
,如果perPage
的值为2,将有三个div,类别为page
。 fruits and vegetables
将显示在第一div
中,paper and books
将显示在第二div
中,toys and electronics
将显示在第三格中。
我已经在vue js中为此准备了基本代码,但是在某些情况下失败了。有人可以看看这个,让我知道哪里出了问题吗?
<div class="wrapper">
<div v-for="(eachWidget, widgetName, index) in widgetData">
<div v-bind:class="((index != 0) && ((index%perPage) == 0))?'page':''" >
<div class="widget">
<p>{{ widgetName}}</p>
<p class="card" v-for="(eachItem, index) in eachWidget">{{eachItem.from}}</p>
</div>
</div>
答案 0 :(得分:0)
首先,我根据pages
的长度和items
的个数计算了perpage
个计数。
之后,我为每个页面列出items
,并在index
中检查item
中的v-if
:
new Vue({
el: "#app",
data: {
perpage: 5,
items: {
"fruits": [{
"from": "Shanghai",
"to": "Houston",
"createdOn": "2019-02-20 17:02:45",
"threadId": "1234564534"
}, {
"from": "Mumbai",
"to": "Texas",
"createdOn": "2019-02-22 17:02:45",
"threadId": "223455678"
}],
"vegetables": [{
"from": "Barcelona",
"to": "Milan",
"createdOn": "2019-01-20 10:02:45",
"threadId": "45673456"
}],
"paper": [{
"from": "Kualalumpur",
"to": "Singapore",
"createdOn": "2019-02-01 12:02:45",
"threadId": "234222345"
}, {
"from": "Singapore",
"to": "Vancover",
"createdOn": "2019-01-20 11:02:45",
"threadId": "6756434343"
}],
"books": [{
"from": "Jibooty",
"to": "Ahmedabad",
"createdOn": "2019-02-10 17:02:45",
"threadId": "23456789"
}],
"toys": [{
"from": "Shanghai",
"to": "Houston",
"createdOn": "2019-02-20 14:02:45",
"threadId": "123434343"
}],
"electronics": [{
"from": "Somalia",
"to": "Angora",
"createdOn": "2019-02-20 17:02:45",
"threadId": "667676767"
}]
}
},
computed: {
length() {
return Object.keys(this.items).length
},
pages() {
return Math.ceil(this.length / this.perpage)
}
}
})
// just to silent vue
Vue.config.productionTip = false;
Vue.config.devtools=false;
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<label>per page:</label>
<input type="number" v-model="perpage" min="1" :max="length">
<ul v-for="n in pages">
<li v-for="(item, title, index) in items"
v-if="index >= perpage * (n-1) && index < perpage * n">
{{ title }}
</li>
</ul>
</div>
希望能提供帮助