当前位置:首页 > VUE

vue分页功能实现

2026-02-20 13:56:44VUE

Vue 分页功能实现

基础分页组件实现

创建分页组件 Pagination.vue,包含页码按钮、上一页/下一页逻辑:

<template>
  <div class="pagination">
    <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
    <span v-for="page in pages" :key="page">
      <button 
        @click="goToPage(page)"
        :class="{ active: currentPage === page }"
      >{{ page }}</button>
    </span>
    <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: {
    prevPage() {
      this.$emit('page-changed', this.currentPage - 1)
    },
    nextPage() {
      this.$emit('page-changed', this.currentPage + 1)
    },
    goToPage(page) {
      this.$emit('page-changed', page)
    }
  }
}
</script>

<style>
.pagination button {
  margin: 0 5px;
}
.pagination button.active {
  background-color: #42b983;
  color: white;
}
</style>

在父组件中使用分页

实现数据分页逻辑,监听分页组件事件:

<template>
  <div>
    <ul>
      <li v-for="item in paginatedItems" :key="item.id">{{ item.name }}</li>
    </ul>
    <Pagination
      :total-items="items.length"
      :items-per-page="itemsPerPage"
      :current-page="currentPage"
      @page-changed="handlePageChange"
    />
  </div>
</template>

<script>
import Pagination from './Pagination.vue'

export default {
  components: { Pagination },
  data() {
    return {
      items: [], // 从API获取或本地数据
      itemsPerPage: 10,
      currentPage: 1
    }
  },
  computed: {
    paginatedItems() {
      const start = (this.currentPage - 1) * this.itemsPerPage
      const end = start + this.itemsPerPage
      return this.items.slice(start, end)
    }
  },
  methods: {
    handlePageChange(page) {
      this.currentPage = page
    }
  }
}
</script>

高级分页功能扩展

添加页码省略功能,避免过多页码按钮:

// 在Pagination组件的pages计算属性中修改
pages() {
  const current = this.currentPage
  const total = this.totalPages
  const range = []

  // 总是显示第一页
  range.push(1)

  // 当前页前后各显示2页
  for (let i = Math.max(2, current - 2); i <= Math.min(current + 2, total - 1); i++) {
    range.push(i)
  }

  // 总是显示最后一页
  if (total > 1) {
    range.push(total)
  }

  // 添加省略号逻辑
  const filtered = []
  range.forEach((page, i) => {
    if (i > 0 && page - range[i-1] > 1) {
      filtered.push('...')
    }
    filtered.push(page)
  })

  return filtered
}

与API结合的分页实现

当数据来自后端API时,修改获取数据方法:

methods: {
  async fetchData(page = 1) {
    const response = await axios.get('/api/items', {
      params: {
        page,
        per_page: this.itemsPerPage
      }
    })
    this.items = response.data.items
    this.totalItems = response.data.total_count
  },
  handlePageChange(page) {
    this.currentPage = page
    this.fetchData(page)
  }
},
created() {
  this.fetchData()
}

分页样式优化

添加更美观的样式方案:

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

.pagination button {
  padding: 5px 10px;
  margin: 0 2px;
  border: 1px solid #ddd;
  background: white;
  cursor: pointer;
  border-radius: 3px;
}

.pagination button:hover:not(:disabled) {
  background: #f5f5f5;
}

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

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

vue分页功能实现

标签: 分页功能
分享给朋友:

相关文章

vue实现按钮实现点击功能

vue实现按钮实现点击功能

Vue 按钮点击功能实现 在 Vue 中实现按钮点击功能可以通过多种方式完成,以下是常见的实现方法: 方法一:使用 v-on 指令 通过 v-on:click 或简写 @click 绑定事件:…

vue实现审核功能

vue实现审核功能

实现审核功能的基本思路 审核功能通常涉及状态管理、权限控制和操作记录。Vue中可以通过组件化设计、状态管理库(如Vuex或Pinia)和后端API配合实现。 审核状态管理 使用Vuex或Pinia…

分页实现vue

分页实现vue

分页实现方法(Vue) 使用Element UI分页组件 安装Element UI库后,可以直接使用其分页组件。在模板中添加el-pagination,通过v-model绑定当前页码,设置page-s…

php实现linux关机重启功能

php实现linux关机重启功能

通过PHP执行Linux关机或重启命令 PHP可以通过exec()、shell_exec()或system()等函数调用系统命令实现关机或重启功能,但需注意权限问题。 使用exec函数执行命令 //…

php分页代码简单实现

php分页代码简单实现

基本分页实现 在PHP中实现分页功能通常需要结合MySQL的LIMIT子句。以下是一个基础实现示例: <?php // 数据库连接 $conn = mysqli_connect("local…

h5实现左右分页

h5实现左右分页

实现左右分页的HTML5方法 在HTML5中实现左右分页效果可以通过多种方式完成,以下是几种常见的方法: 使用CSS Flexbox布局 Flexbox是一种现代的布局方式,可以轻松实现左右分页效…