当前位置:首页 > VUE

用vue怎样实现分页

2026-02-23 15:51:23VUE

使用 Vue 实现分页的方法

基本分页组件实现

在 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: {
      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 button {
  margin: 0 5px;
  padding: 5px 10px;
}
.pagination button.active {
  background-color: #42b983;
  color: white;
}
.pagination button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
</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>

使用第三方库

对于更复杂的分页需求,可以考虑使用现成的 Vue 分页组件库:

  1. vue-paginate:

    用vue怎样实现分页

    npm install vue-paginate

    使用示例:

    import VuePaginate from 'vue-paginate'
    Vue.use(VuePaginate)
    
    // 模板中使用
    <paginate
      :page-count="totalPages"
      :click-handler="handlePageChange"
      :prev-text="'上一页'"
      :next-text="'下一页'"
      :container-class="'pagination'"
    >
    </paginate>
  2. v-pagination:

    用vue怎样实现分页

    npm install v-pagination

    使用示例:

    import VPagination from 'v-pagination'
    
    // 注册组件
    components: {
      VPagination
    }
    
    // 模板中使用
    <v-pagination
      v-model="currentPage"
      :page-count="totalPages"
      :classes="bootstrapPaginationClasses"
      :labels="customLabels"
    />

服务器端分页

当数据量很大时,建议实现服务器端分页:

methods: {
  async fetchData(page = 1) {
    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)
  }
},
created() {
  this.fetchData()
}

优化分页显示

对于大量页数的情况,可以优化只显示部分页码:

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

    let start = Math.max(2, current - delta)
    let end = Math.min(total - 1, current + delta)

    if (current - delta <= 2) {
      end = 2 + delta * 2
    }

    if (current + delta >= total - 1) {
      start = total - 1 - delta * 2
    }

    range.push(1)

    if (start > 2) {
      range.push('...')
    }

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

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

    if (total > 1) {
      range.push(total)
    }

    return range
  }
}

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

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

相关文章

vue实现单据

vue实现单据

Vue 实现单据功能的方法 使用 Vue 实现单据功能通常涉及表单设计、数据绑定、验证和提交等环节。以下是具体实现方案: 表单设计与数据绑定 使用 Vue 的 v-model 指令实现表单数据的双…

vue实现swipe

vue实现swipe

Vue实现Swipe功能的方法 使用第三方库(推荐) Vue生态中有多个成熟的轮播/滑动组件库,例如vue-awesome-swiper或swiper/vue。以下是基于swiper/vue的实现示例…

vue实现布局

vue实现布局

Vue 实现布局方法 Vue 提供了多种方式实现页面布局,可以通过组件化、CSS 框架或自定义样式完成。以下是几种常见方法: 使用 CSS Flexbox 或 Grid 通过 Vue 单文件组件结合…

vue 实现fadeout

vue 实现fadeout

Vue 实现淡出效果 在 Vue 中实现淡出(fade-out)效果可以通过 CSS 过渡或动画结合 Vue 的动态渲染功能完成。以下是几种常见方法: 使用 Vue 过渡(Transition)组件…

vue拖动实现

vue拖动实现

实现 Vue 拖动功能的方法 使用 HTML5 拖放 API 在 Vue 中可以利用 HTML5 的原生拖放 API 实现拖动功能。通过 draggable 属性标记可拖动元素,结合 @dragsta…

vue实现 hover

vue实现 hover

实现 Vue 中的 hover 效果 在 Vue 中实现 hover 效果可以通过多种方式完成,以下是常见的几种方法: 使用 CSS 伪类 最简单的方式是直接使用 CSS 的 :hover 伪类,无…