JavaScript中的分页数组项

时间:2019-02-28 05:56:12

标签: javascript vue.js

我有一个需要以页面格式显示的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,类别为pagefruits 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>

  

1 个答案:

答案 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>

希望能提供帮助

相关问题