当前位置:首页 > VUE

分页实现vue

2026-03-27 12:39:27VUE

分页实现(Vue)

使用Element UI的分页组件

安装Element UI库(若未安装):

npm install element-ui

在Vue项目中引入分页组件:

import { Pagination } from 'element-ui';
Vue.use(Pagination);

模板中使用分页组件:

<template>
  <el-pagination
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
    :current-page="currentPage"
    :page-sizes="[10, 20, 50, 100]"
    :page-size="pageSize"
    layout="total, sizes, prev, pager, next, jumper"
    :total="total">
  </el-pagination>
</template>

数据与方法定义:

分页实现vue

data() {
  return {
    currentPage: 1,
    pageSize: 10,
    total: 0,
    list: []
  };
},
methods: {
  handleSizeChange(val) {
    this.pageSize = val;
    this.fetchData();
  },
  handleCurrentChange(val) {
    this.currentPage = val;
    this.fetchData();
  },
  fetchData() {
    // 模拟API请求
    const start = (this.currentPage - 1) * this.pageSize;
    const end = start + this.pageSize;
    this.list = mockData.slice(start, end);
    this.total = mockData.length;
  }
}

自定义分页组件

创建自定义分页逻辑:

<template>
  <div class="pagination">
    <button 
      @click="prevPage" 
      :disabled="currentPage === 1">
      上一页
    </button>
    <span>第{{ currentPage }}页/共{{ totalPages }}页</span>
    <button 
      @click="nextPage" 
      :disabled="currentPage === totalPages">
      下一页
    </button>
  </div>
</template>

逻辑实现:

分页实现vue

props: {
  totalItems: {
    type: Number,
    required: true
  },
  itemsPerPage: {
    type: Number,
    default: 10
  }
},
data() {
  return {
    currentPage: 1
  };
},
computed: {
  totalPages() {
    return Math.ceil(this.totalItems / this.itemsPerPage);
  }
},
methods: {
  prevPage() {
    if (this.currentPage > 1) {
      this.currentPage--;
      this.$emit('page-changed', this.currentPage);
    }
  },
  nextPage() {
    if (this.currentPage < this.totalPages) {
      this.currentPage++;
      this.$emit('page-changed', this.currentPage);
    }
  }
}

后端API分页处理

前端请求参数:

axios.get('/api/data', {
  params: {
    page: this.currentPage,
    size: this.pageSize
  }
})

后端响应结构示例:

{
  "data": [],
  "total": 100,
  "currentPage": 1,
  "pageSize": 10
}

分页样式优化

添加CSS样式增强用户体验:

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
.pagination button {
  margin: 0 10px;
  padding: 5px 15px;
  border: 1px solid #ddd;
  background: #fff;
  cursor: pointer;
}
.pagination button:disabled {
  color: #ccc;
  cursor: not-allowed;
}

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

相关文章

vue实现ai

vue实现ai

Vue 实现 AI 功能 Vue.js 可以通过集成第三方 AI 服务或本地 AI 模型来实现 AI 功能。以下是几种常见的方法: 集成第三方 AI API 使用 Vue 调用如 OpenAI、Go…

vue实现浮标

vue实现浮标

Vue 实现浮动按钮(浮标) 使用 Vue 实现浮动按钮可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 定位和 Vue 组件 创建 Vue 组件并配合 CSS 固定定位实现浮动按钮:…

lua 实现vue

lua 实现vue

在 Lua 中实现类似 Vue 的响应式框架需要模拟 Vue 的核心功能,包括数据绑定、模板渲染和依赖追踪。以下是实现的关键思路和代码示例: 响应式数据绑定 使用 Lua 的 __index 和 _…

vue 实现table

vue 实现table

Vue 实现 Table 的方法 使用原生 HTML 表格 通过 Vue 的 v-for 指令动态渲染表格数据,适合简单表格场景。 <template> <table>…

vue实现音乐

vue实现音乐

Vue实现音乐播放功能 使用Vue实现音乐播放功能可以通过多种方式完成,以下是几种常见的实现方法: 使用HTML5 Audio API 在Vue组件中直接使用HTML5的Audio对象来控制音乐播放…

vue实现回复

vue实现回复

Vue 实现回复功能 在 Vue 中实现回复功能通常涉及前端表单交互、数据绑定和后端通信。以下是实现回复功能的详细步骤: 数据绑定与表单设计 使用 Vue 的 v-model 绑定回复框的输入内容,…