当前位置:首页 > VUE

vue elementui实现分页

2026-01-15 02:12:56VUE

Vue + ElementUI 分页实现方法

安装与引入

确保已安装 ElementUI,可通过 npm 或 yarn 安装:

npm install element-ui

main.js 中全局引入:

import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

Vue.use(ElementUI);

基础分页组件

使用 <el-pagination> 组件实现分页:

<template>
  <div>
    <el-table :data="currentPageData" border>
      <!-- 表格列定义 -->
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
    </el-table>

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

数据与逻辑处理

在 Vue 实例中定义分页相关数据和方法:

<script>
export default {
  data() {
    return {
      tableData: [],       // 原始数据
      currentPage: 1,      // 当前页码
      pageSize: 10,        // 每页条数
      total: 0,            // 总数据量
      currentPageData: []  // 当前页数据
    };
  },
  methods: {
    // 获取分页数据(模拟API请求)
    fetchData() {
      // 模拟异步请求
      setTimeout(() => {
        this.tableData = Array.from({length: 100}, (_, i) => ({
          name: `用户${i+1}`,
          age: Math.floor(Math.random() * 30) + 20
        }));
        this.total = this.tableData.length;
        this.updatePageData();
      }, 500);
    },

    // 更新当前页数据
    updatePageData() {
      const start = (this.currentPage - 1) * this.pageSize;
      const end = start + this.pageSize;
      this.currentPageData = this.tableData.slice(start, end);
    },

    // 每页条数变化
    handleSizeChange(val) {
      this.pageSize = val;
      this.updatePageData();
    },

    // 页码变化
    handleCurrentChange(val) {
      this.currentPage = val;
      this.updatePageData();
    }
  },
  mounted() {
    this.fetchData();
  }
};
</script>

后端分页对接

实际项目中通常需要对接后端分页接口:

methods: {
  async fetchData() {
    try {
      const res = await axios.get('/api/data', {
        params: {
          page: this.currentPage,
          size: this.pageSize
        }
      });
      this.tableData = res.data.list;
      this.total = res.data.total;
    } catch (error) {
      console.error(error);
    }
  }
}

样式自定义

可通过 CSS 调整分页组件样式:

<style scoped>
.el-pagination {
  margin-top: 20px;
  text-align: right;
}
</style>

功能扩展

实现带搜索的分页:

<el-input v-model="searchQuery" placeholder="搜索..." @change="handleSearch"></el-input>
methods: {
  handleSearch() {
    this.currentPage = 1;
    this.fetchData();
  }
}

vue elementui实现分页

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

相关文章

vue实现条件判断

vue实现条件判断

Vue 条件判断实现方法 Vue 提供了多种方式实现条件判断,可以根据不同场景选择合适的方式。 v-if / v-else-if / v-else 这是 Vue 中最常用的条件渲染指令,根据表达式…

vue怎么实现页面返回

vue怎么实现页面返回

Vue 实现页面返回的方法 在 Vue 中实现页面返回功能,可以通过以下几种方式完成,具体取决于项目使用的路由模式和技术栈。 使用 Vue Router 的编程式导航 通过 this.$route…

vue单页面实现登录

vue单页面实现登录

实现登录功能的基本流程 在Vue单页面应用中实现登录功能,通常需要以下几个关键步骤: 创建登录表单组件 使用Vue的模板语法创建包含用户名和密码输入框的表单,并绑定数据到组件的data属性。 <…

vue实现简单的弹窗

vue实现简单的弹窗

使用 Vue 实现简单弹窗 组件基础结构 创建一个名为 Modal.vue 的组件文件,包含模板、脚本和样式部分: <template> <div class="mod…

vue实现一个系统

vue实现一个系统

Vue 系统实现步骤 项目初始化与配置 使用 Vue CLI 或 Vite 创建项目: npm init vue@latest my-system 选择需要的配置(Router、Pinia 等)。…

vue实现发送值接受值

vue实现发送值接受值

Vue 实现父子组件传值 父组件向子组件传值(props) 父组件通过 v-bind 或简写 : 向子组件传递数据。子组件通过 props 接收数据。 父组件代码: <templat…