Content: Real pagination

Created on 25 Jul 2020  路  4Comments  路  Source: nuxt/content

So, zero information in internet about paginaion with this. The only one example is with surround filter, but that only gives us previous and next links. I'm talking about mature pagination, with page links, with current page number in url, etc. The one that every blog uses. Is it even possible, and if it is, then can someone provide an example?

question

Most helpful comment

Full (I think bug-free) solution:

<template lang="pug">
  v-container
    v-list(three-line)
      v-list-item(v-for="item in posts" @click="$router.push(`/blog/${item.slug}`)" :key="item.slug")
        v-list-item-avatar
          v-img(:src="item.featuredImage")
        v-list-item-content
          v-list-item-title 
            span(style="margin-right: 20px;") {{item.title}}
            v-chip(color="secondary" v-for="tag in item.tags" :key="tag") {{ tag }}
          v-list-item-subtitle {{item.description}}
    v-pagination(
      v-model="page"
      :length="pageCount"
      :total-visible="7"
    )
</template>
<script>
const pagination = {
  getPostsOfPage($content, page) {
    return $content('blog')
      .only(['title', 'tags', 'description', 'featuredImage', 'slug'])
      .sortBy('published', 'desc')
      .skip(10 * (page - 1))
      .limit(10)
      .fetch()
  },

  async getNumberOfPages($content) {
    return Math.ceil((await $content('blog').only([]).fetch()).length / 10)
  },
}

export default {
  async asyncData({ $content, query }) {
    const page = parseInt(query.page || '1') || 1

    const [posts, pageCount] = await Promise.all([
      pagination.getPostsOfPage($content, page),
      pagination.getNumberOfPages($content),
    ])

    return { posts, page, pageCount }
  },
  data() {
    return {
      posts: [],
      page: 1,
      pageCount: 1,
    }
  },
  watch: {
    async page() {
      this.$router.replace({ path: '/blog', query: { page: this.page } })
      this.posts = await pagination.getPostsOfPage(this.$content, this.page)
    },
  },
}
</script>

All 4 comments

I'm currently working on this in my own project you can have a look here: https://github.com/mathe42/EC-Website-test/blob/beitraege/pages/blog/index.vue It's currently buggy when not starting on the first page but you might get an idea.

When I'm happy with my solution I might create a PR for the docs.

(Note that this project is in german so just ignore the head part ;) )

Thank you mister. I will try it out. Also when you feel its ready and free of bugs, please inform about it here, so we can link it as an answer and close the question.

Full (I think bug-free) solution:

<template lang="pug">
  v-container
    v-list(three-line)
      v-list-item(v-for="item in posts" @click="$router.push(`/blog/${item.slug}`)" :key="item.slug")
        v-list-item-avatar
          v-img(:src="item.featuredImage")
        v-list-item-content
          v-list-item-title 
            span(style="margin-right: 20px;") {{item.title}}
            v-chip(color="secondary" v-for="tag in item.tags" :key="tag") {{ tag }}
          v-list-item-subtitle {{item.description}}
    v-pagination(
      v-model="page"
      :length="pageCount"
      :total-visible="7"
    )
</template>
<script>
const pagination = {
  getPostsOfPage($content, page) {
    return $content('blog')
      .only(['title', 'tags', 'description', 'featuredImage', 'slug'])
      .sortBy('published', 'desc')
      .skip(10 * (page - 1))
      .limit(10)
      .fetch()
  },

  async getNumberOfPages($content) {
    return Math.ceil((await $content('blog').only([]).fetch()).length / 10)
  },
}

export default {
  async asyncData({ $content, query }) {
    const page = parseInt(query.page || '1') || 1

    const [posts, pageCount] = await Promise.all([
      pagination.getPostsOfPage($content, page),
      pagination.getNumberOfPages($content),
    ])

    return { posts, page, pageCount }
  },
  data() {
    return {
      posts: [],
      page: 1,
      pageCount: 1,
    }
  },
  watch: {
    async page() {
      this.$router.replace({ path: '/blog', query: { page: this.page } })
      this.posts = await pagination.getPostsOfPage(this.$content, this.page)
    },
  },
}
</script>

@mathe42 Thanks for your example, would you mind making a PR to add it to the snippets section on the documentation?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

giticon picture giticon  路  3Comments

LukaszRados picture LukaszRados  路  3Comments

pxwee5 picture pxwee5  路  4Comments

dolbex picture dolbex  路  3Comments

emp1tsu picture emp1tsu  路  3Comments