当前位置:首页 > VUE

vue 分页功能实现

2026-02-17 02:41:02VUE

Vue 分页功能实现

分页组件基础结构

创建一个独立的 Pagination.vue 组件,包含页码按钮、上一页/下一页按钮和页码范围显示。核心模板结构如下:

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

计算属性处理页码逻辑

通过计算属性动态生成页码范围,避免显示过多页码按钮:

vue 分页功能实现

computed: {
  pages() {
    const range = [];
    const start = Math.max(1, this.currentPage - 2);
    const end = Math.min(this.totalPages, this.currentPage + 2);

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

父组件数据交互

父组件通过 props 传递总数据量和当前页码,通过事件触发页码变化:

props: {
  totalItems: {
    type: Number,
    required: true
  },
  itemsPerPage: {
    type: Number,
    default: 10
  },
  currentPage: {
    type: Number,
    required: true
  }
},
methods: {
  changePage(page) {
    this.$emit('page-changed', page);
  }
}

数据分页处理

在父组件中使用分页组件,并实现数据切片逻辑:

vue 分页功能实现

data() {
  return {
    allData: [], // 全部数据
    currentPage: 1,
    perPage: 10
  };
},
computed: {
  paginatedData() {
    const start = (this.currentPage - 1) * this.perPage;
    const end = start + this.perPage;
    return this.allData.slice(start, end);
  }
}

样式优化

添加基础样式增强分页组件视觉效果:

.pagination button {
  margin: 0 5px;
  padding: 5px 10px;
  border: 1px solid #ddd;
}
.pagination button.active {
  background-color: #42b983;
  color: white;
}
.pagination button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

完整组件示例

整合后的分页组件完整代码:

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

<script>
export default {
  props: {
    totalItems: Number,
    itemsPerPage: {
      type: Number,
      default: 10
    },
    currentPage: {
      type: Number,
      required: true
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.totalItems / this.itemsPerPage);
    },
    pages() {
      const range = [];
      const start = Math.max(1, this.currentPage - 2);
      const end = Math.min(this.totalPages, this.currentPage + 2);

      for (let i = start; i <= end; i++) {
        range.push(i);
      }
      return range;
    }
  },
  methods: {
    changePage(page) {
      this.$emit('page-changed', page);
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.changePage(this.currentPage - 1);
      }
    },
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.changePage(this.currentPage + 1);
      }
    }
  }
};
</script>

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

相关文章

vue实现拨号功能

vue实现拨号功能

Vue实现拨号功能的方法 使用HTML5的tel协议 在Vue模板中直接使用<a>标签的tel:协议实现拨号功能。这种方法适用于移动端浏览器,点击后会调起系统的拨号界面。 <tem…

vue实现下拉分页思想

vue实现下拉分页思想

实现下拉分页的基本思想 下拉分页(Infinite Scroll)是一种常见的分页加载方式,当用户滚动到页面底部时自动加载下一页数据。Vue 中可以通过监听滚动事件或使用第三方库实现。 监听滚动事件…

js实现分页

js实现分页

分页的基本实现思路 在JavaScript中实现分页功能通常需要结合前端和后端逻辑。前端负责渲染分页控件和处理用户交互,后端负责提供分页数据。 前端分页实现 纯前端分页适用于数据量较小的情况,可以直…

uniapp实现支付功能

uniapp实现支付功能

支付功能实现概述 在UniApp中实现支付功能通常需要对接第三方支付平台(如微信支付、支付宝支付等)。以下是基于微信支付和支付宝支付的通用实现流程。 微信支付实现步骤 1. 配置支付权限 在微信开放…

h5实现点击复制功能

h5实现点击复制功能

使用document.execCommand方法(兼容旧浏览器) 通过document.execCommand('copy')实现复制功能,需先选中文本内容。创建一个隐藏的textarea或input…

vue实现功能插件

vue实现功能插件

Vue 功能插件实现方法 插件基本结构 Vue 插件通常需要暴露一个 install 方法,该方法接收 Vue 构造函数作为参数。基本结构如下: const MyPlugin = { insta…