当前位置:首页 > VUE

用vue实现分页效果

2026-02-23 12:00:03VUE

使用Vue实现分页效果

基础分页组件实现

创建一个简单的分页组件,包含页码按钮、上一页和下一页按钮。以下是一个基本实现:

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

<script>
export default {
  props: {
    totalItems: {
      type: Number,
      required: true
    },
    itemsPerPage: {
      type: Number,
      default: 10
    },
    currentPage: {
      type: Number,
      default: 1
    }
  },
  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() {
      if (this.currentPage > 1) {
        this.$emit('page-changed', this.currentPage - 1)
      }
    },
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.$emit('page-changed', this.currentPage + 1)
      }
    },
    goToPage(page) {
      this.$emit('page-changed', page)
    }
  }
}
</script>

<style>
.pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
.pagination button {
  margin: 0 5px;
  padding: 5px 10px;
  cursor: pointer;
}
.pagination button.active {
  background-color: #42b983;
  color: white;
}
.pagination button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}
</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: 5,
      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>

高级分页功能

对于大型数据集,可以添加以下改进:

限制显示的页码数量

修改分页组件的pages计算属性:

用vue实现分页效果

pages() {
  const range = []
  const maxVisible = 5 // 最多显示5个页码
  let start = 1

  if (this.totalPages > maxVisible) {
    start = Math.min(
      Math.max(1, this.currentPage - Math.floor(maxVisible / 2)),
      this.totalPages - maxVisible + 1
    )
  }

  const end = Math.min(start + maxVisible - 1, this.totalPages)

  for (let i = start; i <= end; i++) {
    range.push(i)
  }

  return range
}

添加省略号表示更多页码

在模板中添加条件渲染:

<button v-if="showStartEllipsis" disabled>...</button>
<button 
  v-for="page in visiblePages" 
  :key="page" 
  @click="goToPage(page)"
  :class="{ active: currentPage === page }"
>
  {{ page }}
</button>
<button v-if="showEndEllipsis" disabled>...</button>

添加跳转到指定页功能

用vue实现分页效果

在分页组件中添加输入框:

<input 
  type="number" 
  v-model.number="inputPage" 
  min="1" 
  :max="totalPages" 
  @keyup.enter="goToInputPage"
>
<button @click="goToInputPage">跳转</button>

服务器端分页

当处理大量数据时,通常使用服务器端分页:

methods: {
  async fetchData(page) {
    try {
      const response = await axios.get('/api/items', {
        params: {
          page,
          per_page: this.itemsPerPage
        }
      })
      this.items = response.data.items
      this.totalItems = response.data.total
    } catch (error) {
      console.error(error)
    }
  },
  handlePageChange(page) {
    this.currentPage = page
    this.fetchData(page)
  }
}

使用第三方库

可以考虑使用现成的分页库如vuejs-paginate

npm install vuejs-paginate

使用示例:

import Paginate from 'vuejs-paginate'

export default {
  components: {
    Paginate
  },
  methods: {
    handlePageChange(page) {
      this.currentPage = page
    }
  }
}
<paginate
  :page-count="totalPages"
  :click-handler="handlePageChange"
  :prev-text="'上一页'"
  :next-text="'下一页'"
  :container-class="'pagination'"
  :page-class="'page-item'"
>
</paginate>

这些方法提供了从基础到高级的分页实现方案,可以根据项目需求选择合适的实现方式。

标签: 分页效果
分享给朋友:

相关文章

vue  实现分页

vue 实现分页

实现分页功能 在Vue中实现分页功能通常需要结合后端API或前端数据处理。以下是两种常见的实现方式: 前端分页实现 适用于数据量较小的情况,所有数据一次性加载到前端后进行分页处理: <…

php分页代码简单实现

php分页代码简单实现

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

vue实现框架效果

vue实现框架效果

Vue实现框架效果的方法 使用Vue实现框架效果可以通过多种方式完成,包括使用Vue Router、动态组件或第三方UI库。以下是几种常见的方法: 使用Vue Router实现布局框架 Vue Ro…

jquery分页

jquery分页

jQuery分页的实现方法 jQuery分页可以通过多种方式实现,以下是几种常见的方法: 使用插件实现分页 jQuery有许多优秀的分页插件,如jQuery Pagination、DataTa…

分页用vue实现

分页用vue实现

实现分页的基本思路 在Vue中实现分页功能,通常需要以下几个核心部分:数据列表、当前页码、每页显示数量、总页数计算、页码切换逻辑。以下是一个完整的实现方案。 分页组件代码示例 <templat…

vue实现tag效果

vue实现tag效果

实现基本标签结构 使用 Vue 的模板语法创建标签容器和单个标签元素。通过 v-for 动态渲染标签列表,结合 v-bind:key 确保渲染性能。 <template> &l…