当前位置:首页 > VUE

vue中实现分页

2026-03-10 01:13:07VUE

vue中实现分页的方法

使用Element UI的分页组件

Element UI提供了现成的分页组件el-pagination,可以快速实现分页功能。安装Element UI后,在组件中引入并使用。

vue中实现分页

<template>
  <div>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      total: 100
    }
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchData();
    },
    fetchData() {
      // 根据currentPage和pageSize获取数据
    }
  }
}
</script>

自定义分页组件

如果需要更灵活的分页逻辑,可以手动实现分页组件。通过计算属性对数据进行分页处理。

vue中实现分页

<template>
  <div>
    <div v-for="item in paginatedData" :key="item.id">
      {{ item.name }}
    </div>
    <div class="pagination">
      <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
      <span>第 {{ currentPage }} 页</span>
      <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      data: [], // 原始数据
      currentPage: 1,
      itemsPerPage: 10
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.data.length / this.itemsPerPage);
    },
    paginatedData() {
      const start = (this.currentPage - 1) * this.itemsPerPage;
      const end = start + this.itemsPerPage;
      return this.data.slice(start, end);
    }
  },
  methods: {
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--;
      }
    },
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++;
      }
    }
  }
}
</script>

结合后端API实现分页

实际项目中,分页通常与后端API配合使用。前端传递页码和每页条数,后端返回对应数据。

<template>
  <div>
    <el-pagination
      @current-change="handlePageChange"
      :current-page="currentPage"
      :page-size="pageSize"
      :total="total"
      layout="prev, pager, next">
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      total: 0,
      list: []
    }
  },
  mounted() {
    this.getList();
  },
  methods: {
    getList() {
      axios.get('/api/list', {
        params: {
          page: this.currentPage,
          size: this.pageSize
        }
      }).then(res => {
        this.list = res.data.list;
        this.total = res.data.total;
      });
    },
    handlePageChange(page) {
      this.currentPage = page;
      this.getList();
    }
  }
}
</script>

使用第三方库vue-awesome-paginate

如果需要更多功能,可以使用专门的分页库如vue-awesome-paginate。安装后引入并使用。

<template>
  <div>
    <vue-awesome-paginate
      :total-items="total"
      :items-per-page="perPage"
      :max-pages-shown="5"
      v-model="currentPage"
      :on-click="onClickHandler"
    />
  </div>
</template>

<script>
import { VueAwesomePaginate } from 'vue-awesome-paginate';

export default {
  components: {
    VueAwesomePaginate
  },
  data() {
    return {
      currentPage: 1,
      perPage: 10,
      total: 100
    }
  },
  methods: {
    onClickHandler(page) {
      this.currentPage = page;
      this.fetchData();
    }
  }
}
</script>

以上方法可以根据项目需求选择适合的分页实现方式。

标签: 分页vue
分享给朋友:

相关文章

vue实现弹窗

vue实现弹窗

Vue 实现弹窗的常见方法 使用组件化方式封装弹窗 创建一个独立的弹窗组件(如 Modal.vue),通过 v-if 或 v-show 控制显示隐藏: <template> <…

vue实现点击

vue实现点击

实现点击事件的基本方法 在Vue中实现点击事件可以通过v-on指令或@简写方式绑定。以下是常见的点击事件绑定方法: <template> <button v-on:click=…

vue实现autocomplete

vue实现autocomplete

Vue 实现 Autocomplete 功能 Vue 中实现 Autocomplete(自动完成)功能可以通过自定义组件或使用现有库完成。以下是几种常见方法: 方法一:使用 Vue 原生实现 创建…

vue实现tag

vue实现tag

Vue 实现标签(Tag)功能 在 Vue 中实现标签(Tag)功能可以通过多种方式完成,以下是一些常见的方法和实现步骤: 使用动态组件和 v-for 通过 v-for 指令动态渲染标签列表,结合样…

vue实现granfana

vue实现granfana

Vue 实现类似 Grafana 的仪表盘 要在 Vue 中实现类似 Grafana 的仪表盘功能,需要结合数据可视化库、状态管理和 UI 组件。以下是关键步骤和推荐工具: 数据可视化库 使用 E…

vue轮询实现

vue轮询实现

Vue 轮询实现方法 在 Vue 中实现轮询可以通过以下几种方式: 使用 setInterval data() { return { pollInterval: null }…