0
votes

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 :

enter image description here

My view of pagination in my application like this :

enter image description here

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

3
Can you provide the code of b-pagination + what is the network response when pressing next or 2Frnak
@Frank Provost I use coreui. Look at this : coreui.io/vue/demo/#/base/paginations and github.com/coreui/coreui-free-vue-admin-template/blob/master/…. Seems it's automaticSuccess Man

3 Answers

1
votes

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 -->
0
votes

I believe, you don't update items reactivly.

Can you try follow:

data () {
          return{
              container:{ 
              // for Vue.set I didnt found how to do it without nested fields for data object, so did like that on my own project
                  items: '',
                  itemsPagination: ''
              }
          }
        },

And then in fetch:

Vue.set(this.container, 'items', r.data); 
// or this.$set, if you dont use arrow function here, and not Vue accessable

see https://vuejs.org/v2/guide/reactivity.html

If that not helps, check for console.log(this) inside of your fetch if it your component

0
votes

Maybe this is not the best way to answer you (redirecting to a video), but i think this video will be clearer than me about what is happening, and how to solve it .

https://youtu.be/7lpemgMhi0k?t=17m42s