How to paginate subsections of an array property in Vue.js? How to paginate subsections of an array property in Vue.js? vue.js vue.js

How to paginate subsections of an array property in Vue.js?


Since you have Vuex on board, a getter seems easiest.

export const getters = {  pagedItems: state => {    return pageNo => {      const pageSize = state.pageSize || 10      const items = state.items || []      return items.slice(pageNo * pageSize, pageSize)     }  }}

Default values (e.g state.items || []) are there to stop the calculation erroring before initial load completes.

Use it on the component in a computed property, which will make it reactive when pageNo changes,

computed: {  pagedItems() {    return this.$store.getters['pagedItems'](this.pageNo)  },},

It just occurred to me that if you are filtering, you probably want to apply that before the pagination otherwise the display may not be be consistent in size (e.g page 1 filters to 4 items, page 2 to 6 items).

Depends on your exact filter, should be easy to add a getter for filteredItems and use that as source for pagedItems.


well, i would just divide the number of items by the number of data i want to display per page with the rest operator and create number of pages + 1, of course with some validations to empty data and so on.

Imagine you recieve an object that contains lists, this lists represent all the arrays with your data, each array is a row.

Just get the length, divide it with module operator and add one more, in your case, if you have 52 items, and want to have 10 per page:

52 % 10 = 252 / 10 = 5

you need 5 pages + 1 for the 2 items.

so i would do something like this:

const NUMBER_ITEMS_PER_PAGE = 10;const numberItems = list.length;const pages = numberItems / NUMBER_ITEMS_PER_PAGEif(numberItems % NUMBER_ITEMS_PER_PAGE > 0) {  pages++;}function buildPages(numberPages) {  const pageObj = {}  for(var i = 0; i < pages; i++) {    pageObj.page[i+1]    const arr = []    for(var j = 0; j < (NUMBER_ITEMS_PER_PAGE) * (i + 1); j++) {      arr.push(lists[i])     }   pageObj.page[i+1] = arr;  }}

of course this is just one possible solution, but i think this can let you start in some way, the code is just to help. good luck