vue如何按页码或prev next点击显示内容?

How to display content by page number or prev next clicked on the vue?(vue如何按页码或prev next点击显示内容?)

本文介绍了vue如何按页码或prev next点击显示内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

My vue component like this :

<template>
    ...
        <b-col v-for="item in items"
               v-bind:key="item.id"
               cols="2">
            ...
            <strong slot="header" class="text-dark" :title="item.tittle" >{{item.tittle}}</strong><br/>
            ...
            <strong class="bg-warning text-dark"><i class="fa fa-money"></i> <b>{{item.price}}</b></strong><br/>
            ...
        </b-col>
    ...
        <b-pagination size="sm" :total-rows="itemsPagination.total" :per-page="itemsPagination.per_page" v-model="itemsPagination.current_page" prev-text="Prev" next-text="Next" hide-goto-end-buttons/>
    ...
</template>
<script>
    export default {
        ...
        data () {
          return{
              items: '',
              itemsPagination: ''
          }
        },
        mounted: function () {
            this.getItems()
        },
        methods: {
            getItems() {
                let params = []
                ...
                let request = new Request(ApiUrls.items + '?' + params.join('&'), {
                    method: 'GET',
                    headers: new Headers({
                        'Authorization': 'bearer ' + this.$session.get(SessionKeys.ApiTokens),
                        'Content-Type': 'text/plain'
                    })
                })
                fetch(request).then(r=> r.json())
                    .then(r=> {
                        this.items = r.data
                        this.itemsPagination = r
                    })
                    .catch(e=>console.log(e))
            }
        }
    }
</script>

If I console.log(this.itemsPagination), the result in console like this :

My view of pagination in my application like this :

If the script executed, it will display content of item in page 1. But if I click page 2 etc, the content of item is not change. I'm still confused to make it work well

How can I solve this problem?

Update

I using coreui

https://coreui.io/vue/demo/#/base/paginations

https://github.com/coreui/coreui-free-vue-admin-template/blob/master/src/views/base/Paginations.vue

https://bootstrap-vue.js.org/docs/components/pagination

解决方案

You are trying to v-model="itemsPagination.current_page", but you initialize itemsPagination as an empty string:

    data () {
      return{
          items: '',
          itemsPagination: ''
      }
    }

Vue cannot detect property addition or deletion, so nothing reacts. You need to initialize itemsPagination as an object that contains (at least) current_page:

    data () {
      return{
          items: '',
          itemsPagination: {
            current_page: 1
          }
      }
    }

Update: You can actually edit the example here. Double-click the upper right corner to edit it, and paste in this code:

<template>
  <div>
    <h6>Default</h6>
    <b-pagination size="md" :total-rows="100" v-model="itemsPagination.current_page" :per-page="10">
    </b-pagination>
    <br>

    <h6>Small</h6>
    <b-pagination size="sm" :total-rows="100" v-model="itemsPagination.current_page" :per-page="10">
    </b-pagination>
    <br>

    <h6>Large</h6>
    <b-pagination size="lg" :total-rows="100" v-model="itemsPagination.current_page" :per-page="10">
    </b-pagination>
    <br>

    <div>currentPage: {{itemsPagination.current_page}}</div>
  </div>    
</template>

<script>
export default {
  data () {
    return {
      itemsPagination: {
        current_page: 1
      }
    }
  }
}
</script>

<!-- pagination-1.vue -->

这篇关于vue如何按页码或prev next点击显示内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:vue如何按页码或prev next点击显示内容?

基础教程推荐