当前位置:首页 > VUE

vue实现分页组件思路

2026-02-24 06:24:52VUE

基础分页组件实现

创建一个基础分页组件需要定义当前页码、总页数、页码按钮等核心功能。使用v-for渲染页码按钮,通过v-modelprops管理当前页状态。

<template>
  <div class="pagination">
    <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
    <button 
      v-for="page in pages" 
      :key="page"
      @click="changePage(page)"
      :class="{ active: page === currentPage }"
    >
      {{ page }}
    </button>
    <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
  </div>
</template>

<script>
export default {
  props: {
    totalItems: Number,
    itemsPerPage: Number,
    currentPage: Number
  },
  computed: {
    totalPages() {
      return Math.ceil(this.totalItems / this.itemsPerPage)
    },
    pages() {
      const range = []
      for (let i = 1; i <= this.totalPages; i++) {
        range.push(i)
      }
      return range
    }
  },
  methods: {
    changePage(page) {
      this.$emit('page-changed', page)
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.$emit('page-changed', this.currentPage - 1)
      }
    },
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.$emit('page-changed', this.currentPage + 1)
      }
    }
  }
}
</script>

优化页码显示逻辑

当页码过多时,需要优化显示方式,常见方案是显示当前页前后几页,并用省略号表示跳页。

vue实现分页组件思路

computed: {
  pages() {
    const current = this.currentPage
    const total = this.totalPages
    const range = []

    // 总页数<=7时显示全部
    if (total <= 7) {
      for (let i = 1; i <= total; i++) {
        range.push(i)
      }
    } 
    // 其他情况使用智能截断
    else {
      const left = Math.max(2, current - 2)
      const right = Math.min(total - 1, current + 2)

      range.push(1)
      if (left > 2) range.push('...')

      for (let i = left; i <= right; i++) {
        range.push(i)
      }

      if (right < total - 1) range.push('...')
      range.push(total)
    }

    return range
  }
}

集成到数据列表

将分页组件与数据列表结合使用,通常需要计算当前页的数据切片。

vue实现分页组件思路

computed: {
  paginatedData() {
    const start = (this.currentPage - 1) * this.itemsPerPage
    const end = start + this.itemsPerPage
    return this.allData.slice(start, end)
  }
}

添加自定义配置项

通过props允许自定义每页显示数量、可见页码数等参数,增强组件灵活性。

props: {
  totalItems: Number,
  itemsPerPage: {
    type: Number,
    default: 10
  },
  visiblePages: {
    type: Number,
    default: 5
  },
  currentPage: {
    type: Number,
    default: 1
  }
}

样式优化建议

为分页组件添加基础样式,提升视觉体验。

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

.pagination button {
  margin: 0 5px;
  padding: 5px 10px;
  border: 1px solid #ddd;
  background: #fff;
  cursor: pointer;
}

.pagination button.active {
  background: #42b983;
  color: white;
  border-color: #42b983;
}

.pagination button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

标签: 分页组件
分享给朋友:

相关文章

vue实现组件

vue实现组件

Vue 实现组件的方法 Vue 中实现组件可以通过多种方式,包括全局注册、局部注册、单文件组件(SFC)等。以下是常见的实现方法。 全局注册组件 全局注册的组件可以在任何 Vue 实例或组件中使用。…

vue 实现组件刷新

vue 实现组件刷新

组件局部刷新 在Vue中实现组件刷新可以通过强制重新渲染组件来实现。常用的方法有以下几种: 使用v-if指令 通过切换v-if条件触发组件的销毁和重建 <template> <…

vue实现后台分页

vue实现后台分页

Vue 实现后台分页的方法 分页组件与数据绑定 在 Vue 中实现后台分页通常需要结合分页组件和后端 API。使用 el-pagination(Element UI)或自定义分页组件,通过 v-mod…

vue table组件实现

vue table组件实现

Vue 表格组件实现方案 基础表格实现 使用<table>标签结合v-for指令渲染数据是最简单的实现方式: <template> <table> &…

react如何删除组件

react如何删除组件

删除 React 组件的几种方法 条件渲染法 通过状态控制组件是否渲染。当状态为 false 时,组件会被移除: const [showComponent, setShowComponent] =…

vue怎样实现分页

vue怎样实现分页

Vue实现分页的方法 使用第三方库(如Element UI或Ant Design Vue) Element UI的Pagination组件可以直接集成到Vue项目中。安装Element UI后,在组件…