3
votes

I'm using VueJS, Bootstrap Vue Table and Pagination to show a list of users with pagination. The data seems to be loaded correctly but page 2 of the pagination does not show any data.

I have a Users component that pass the necessary data for the C-table bootstrap component, like this:

Users.vue

<c-table
                :tableData="tableData"
                :fields="fields"
/>
<script>
import cTable from '../base/Table.vue'
import UserAPI from '../../api/user.js'
export default {
    created () {
        var vueComponent = this
        UserAPI.getUsers()
        .then(response => {
            if (response.data.success) {
                vueComponent.tableData = response.data.data
            } 
        })
    }
}
</script>

And in Table component, I try to render the table with pagination with the data provided from the Users component.

Table.vue

<b-table
        :items="tableData.data"
        :fields="fields"
        :current-page="tableData.current_page"
        :per-page="tableData.per_page"
>
</b-table>

<nav>
   <b-pagination
     v-model="tableData.current_page"
     :total-rows="tableData.total"
     :per-page="tableData.per_page"
     prev-text="Prev"
     next-text="Next"
     hide-goto-end-buttons
    />
</nav>

<script>
export default {
    name : 'cTable',
    props: {
         tableData: {
            type : Object,
            default: {},
        },
        fields: {
            type : Array,
            default: [],
        },
    },
    watch: {
        'tableData.current_page': function (newVal, oldVal) {
            var vueComponent = this
            if (oldVal && newVal) {
                axios.get(this.tableData.path + '?page=' + newVal)
                .then(response => {
                    if (response.data && response.data.success) {
                        vueComponent.tableData = response.data.data
                        debugger
                    }
                })
            }
        }
    }
}

So when I first load the page, it shows the correct list of users, but when I switch to page 2, at the debugger point although it retrieves data correctly from the server, it shows nothing on the table. Also I got this warning:

Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "tableData"

Here is the data of tableData at the debugger point:

{  
      "current_page":2,
      "data":[  
         {  
            "id":3,
            "email":"[email protected]",
            "email_verified_at":"2019-04-13 01:27:14",
            "active":2,
            "created_at":"2019-04-13 01:26:53",
            "updated_at":"2019-04-13 01:27:14",
            "deleted_at":null,
         },
         {  
            "id":4,
            "email":"[email protected]",
            "email_verified_at":"2019-04-13 01:27:14",
            "active":0,
            "created_at":"2019-04-13 01:26:53",
            "updated_at":"2019-04-13 01:27:14",
            "deleted_at":null,
         }
      ],
      "first_page_url":"http:\/\/localhost:8000\/api\/users?page=1",
      "from":3,
      "last_page":3,
      "last_page_url":"http:\/\/localhost:8000\/api\/users?page=3",
      "next_page_url":"http:\/\/localhost:8000\/api\/users?page=3",
      "path":"http:\/\/localhost:8000\/api\/users",
      "per_page":2,
      "prev_page_url":"http:\/\/localhost:8000\/api\/users?page=1",
      "to":4,
      "total":6
}

I must be doing something wrong, but I couldn't figure out where is it. Why the changed data didn't reflect on the table?

3

3 Answers

4
votes

I changed per-page="0" to <b-table> instead of per-page="tableData.per_page" and it worked.

1
votes

Move:

current-page="tableData.current_page"

from b-table to b-pagination and add @input="youTableData" to b-pagination.

0
votes

There is a bug in Bootstrap Vue's Pagination Component. Two way data binding i.e., v-model does not work, so even though it shows the page is changed in the User interface, it inherently does not change the bounded variable. I found a work around fix.

Remove the

v-model="currentPage"

on the pagination component and replace it with this

@change="currentPage = $event"

Doing this will add an event listener that updates the variable from the event. Directly from the DOM value property to the currentPage variable