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

How to prevent Bootstrap-Vue pagination from automatically resetting

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

其中之一,我有一个 table 并且我使用 bootstrap-pagination 组件进行分页,所以当用户更改页面时,我将请求发送到服务器以获取新数据.我在 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>

和 javascript 中的代码:

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

    ...

    watch: {
        currentPage: function(){
                this.$store.commit('updateCurrentPage', this.currentPage);
                this.getData();
              },
    }

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

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

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

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

另一种解决方法是在分页元素上放置一个 v-if 以检查是否已设置 totalRows。这会导致分页元素在 totalRows 值已知后呈现。当您使用 AJAX 加载数据并且提前不知道行数时,它很有用。

例如

<b-pagination
  v-if="totalRows > 0"
  v-model="currPage"
  :total-rows="totalRows"
  :per-page="perPage"
  aria-controls="my-table"
  size="sm"
  limit="7"
  first-number
  last-number
  >
</b-pagination>