如何防止 Bootstrap-Vue 分页自动重置

Mih*_*vic 2 vue.js vuex bootstrap-vue

我有多个视图,我想保存每个视图的状态,这样用户就不必再次执行相同的操作。

在其中一个上,我有一个表,并使用引导分页组件进行分页,因此当用户更改页面时,我将请求发送到服务器以获取新数据。我在 Vuex 中存储排序和过滤参数,效果很好。

但是当我想像这样保存当前页面时,每次更改视图并返回时,bootstrap-pagination 都会将其更改为 1。是否可以阻止这种行为?

以下是 HTML 代码:

<b-pagination v-if="currentPage!=-1"
            v-model="currentPage"
            :total-rows="totalRows"
            :per-page="perPage"
            aria-controls="my-table"
            pils
            align="center"
            class="ml-auto mr-auto"
          ></b-pagination>
Run Code Online (Sandbox Code Playgroud)

和 JavaScript 代码:

  data: function () {
      return {
        ...
        currentPage: this.$store.state.currentPage,
        ...
      }
    },

    ...

    watch: {
        currentPage: function(){
                this.$store.commit('updateCurrentPage', this.currentPage);
                this.getData();
              },
    }
Run Code Online (Sandbox Code Playgroud)

Hiw*_*iws 5

您面临的问题很可能与数据加载和设置的顺序有关。

当您的组件初始化时,您currentPage立即设置页面属性,而(我猜测)totalRows在从数据库获取数据后的某个时间设置该属性。

这会导致问题,因为分页会认为有 0 行。因此,当您将其设置为第 5 页时,它将检查perPagetotalRows属性,并且由于还没有第 5 页,因此它会自动设置currentPage回第 1 页。

相反,我建议您currentPage在检索数据并设置totalRows属性后将其设置为商店中的值。这应该可以解决您遇到的问题。