jose
jose

Reputation: 1562

Quasar QList pagination with QPagination component and laravel paginate() method

I want to paginate a list made with the QList component

With the laravel paginate() method I receive on the client side the following data necessary for pagination:

current_page:
data:[]
first_page_url:
from:
last_page:
last_page_url:
next_page_url:
path:
per_page:
prev_page_url:
to:
total:

I'm getting started with Quasar framework and I want a simple QList pagination solution.

Apparently the QPagination component will be suitable for this purpose.

I would appreciate suggestions on how to implement a working QPagination or other simple solution.

EDITED

Some code to demonstrate what I am trying to achieve

<template>
  <q-page class="fit row wrap justify-center items-start content-start" style="overflow: hidden;">
        <div class="q-pa-md" style="width: 450px;"> 
            <q-list bordered separator v-for="(post, index) in posts" :key="index">              
              <q-item clickable v-ripple>
                  <q-item-section>
                        <q-item-label> {{ post.title }} | {{ index }}</q-item-label>
                        <q-item-label caption> {{ post.content }}</q-item-label>
                   </q-item-section>
              </q-item>

            </q-list>

            <div class="q-pa-lg flex flex-center">
                    <q-pagination
                          v-model="page"
                          :min="currentPage" 
                          :max="totalPages"
                          :input="true"
                          input-class="text-orange-10"
                    >
                    </q-pagination>
                </div>
          </div>
  </q-page>
</template>

<script>
export default {

   data() {
        return {
            posts : [{
                id: 1,
                title: "title one",
                content: "This is content one"
            },{
                id:2,
                title: "title two",
                content: "This is content two"
            },{
                id:3,
                title: "title three",
                content: "This is content three"
            }],

            page: 1,
            currentPage:0,
            nextPage: null,
            totalPages:5,
        }
   }
}
</script>

EDITED » To better illustrate the problem to solve:

<template>
  <q-page class="fit row wrap justify-center items-start content-start" style="overflow: hidden;">
        <div class="q-pa-md" style="width: 450px;"> 
            <q-list bordered separator v-for="(post, index) in posts" :key="index">              
              <q-item clickable v-ripple>
                  <q-item-section>
                        <q-item-label> {{ post.title }} | {{ index }}</q-item-label>
                        <q-item-label caption> {{ post.content }}</q-item-label>
                   </q-item-section>
              </q-item>

            </q-list>

            <div class="q-pa-lg flex flex-center">
                    <q-pagination
                          v-model="page"
                          :min="currentPage" 
                          :max="totalPages"
                          :input="true"
                          input-class="text-orange-10"
                    >
                    </q-pagination>
                </div>
          </div>


        <div class="flex justify-center" >
            <button v-if="prevPage" class="text-grey-darker text-sm" style="margin-right:10px;" @click="goPrevPage(prevPage)">
                Prev. Page | Pag. {{currentPage}} of {{totalPages}}
            </button>
             <button  v-if="nextPage"  class="text-grey-darker text-sm" @click="goNextPage(nextPage)">
                Next Page | Pag. {{currentPage}} of {{totalPages}}
            </button>
         </div>


  </q-page>
</template>

<script>
export default {

   data() {
        return {
            posts : {},

             page: 0,
             currentPage:0,
             totalPages:null,
             totalPosts:0,
             nextPage: null,
             prevPage: null
        }
   },

   mounted() {
    this.getData();
   },

   methods:{
     getData(){
        this.$axios.get(`/listposts')
            .then((response) => {
                if (response.data.success) {

                   this.posts= response.data.posts.data;

                   this.page = response.data.posts.currentPage;
                   this.currentPage = response.data.posts.currentPage;
                   this.totalPages = response.data.posts.last_page;
                   this.totalPosts = response.data.posts.total;
                   this.page = response.data.posts.current_page;
                   this.nextPage = response.data.posts.next_page_url;
                   this.prevPage = response.data.posts.prev_page_url;

              } else { }
            })
            .catch(error => { 
          });
        }
     },

     goNextPage(urlNextPage){
        this.$axios.get(urlNextPage)
            .then((response) => {
                if (response.data.success) {

                   this.posts= response.data.posts.data;

                   this.page = response.data.posts.currentPage;
                   this.currentPage = response.data.posts.currentPage;
                   this.totalPages = response.data.posts.last_page;
                   this.totalPosts = response.data.posts.total;
                   this.page = response.data.posts.current_page;
                   this.nextPage = response.data.posts.next_page_url;
                   this.prevPage = response.data.posts.prev_page_url;

              } else { }
            })
            .catch(error => { 
          });
     }

     goPrevPage(urlPrevPage){
      this.$axios.get(urlPrevPage)
            .then((response) => {
                if (response.data.success) {

                   this.posts= response.data.posts.data;

                   this.page = response.data.posts.currentPage;
                   this.currentPage = response.data.posts.currentPage;
                   this.totalPages = response.data.posts.last_page;
                   this.totalPosts = response.data.posts.total;
                   this.page = response.data.posts.current_page;
                   this.nextPage = response.data.posts.next_page_url;
                   this.prevPage = response.data.posts.prev_page_url;

              } else { }
            })
            .catch(error => { 
          });
     }

   }
}
</script>

EDITED with working solution

<template>
  <q-page class="fit row wrap justify-center items-start content-start" style="overflow: hidden;">
        <div class="q-pa-md" style="width: 450px;"> 
            <q-list bordered separator v-for="(post, index) in posts" :key="index">              
              <q-item clickable v-ripple>
                  <q-item-section>
                        <q-item-label> {{ post.title }} | {{ index }}</q-item-label>
                        <q-item-label caption> {{ post.content }}</q-item-label>
                   </q-item-section>
              </q-item>

            </q-list>

            <div class="q-pa-lg flex flex-center">
                    <q-pagination
                          v-model="page"
                          :min="currentPage" 
                          :max="lastPage"
                          input
                          input-class="text-orange-10"
                          @input="goAnotherPage"
                    >
                    </q-pagination>
                </div>
          </div>
  </q-page>
</template>

<script>
export default {

   data() {
        return {
            posts : {},

            page: 0,
            currentPage:0,
            lastPage:0        
        }
   },

   mounted() {
    this.getData();
   },

   methods:{
     getData(){
        this.$axios.get('/listposts')
            .then((response) => {
                if (response.data.success) {
                   this.posts= response.data.posts.data;
                   this.page = response.data.posts.currentPage;
                   this.currentPage = response.data.posts.currentPage;
                   this.lastPage = response.data.posts.last_page;
              } else { }
            })
            .catch(error => { 
          });
        }
     },

    goAnotherPage(page) { 
      this.paginationUrl="http://my_app/api/listposts?page="+this.page;
      this.$axios.get(this.paginationUrl)
        .then((response) => {
            if (response.data.success) {
              this.posts= response.data.posts.data;
            } else { }
        })
            .catch(error => { 
     });
   }
}
</script>

Upvotes: 1

Views: 5740

Answers (1)

Pratik Patel
Pratik Patel

Reputation: 6978

Just use the computed property for getting data based on pagination.

getData(){
  return this.posts.slice((this.page-1)*this.totalPages,(this.page-1)*this.totalPages+this.totalPages)
}

<q-list bordered separator v-for="(post, index) in getData" :key="index">
    <q-item clickable v-ripple>
        <q-item-label> {{ post.title }} | {{ index }}</q-item-label>
        <q-item-label caption> {{ post.content }}</q-item-label>
    </q-item>
</q-list>

Working codepen - https://codepen.io/Pratik__007/pen/PowpOeL

Upvotes: 1

Related Questions