# Vue、ElementUI 表格分页实现

Vue—Element表格分页实现


  1. 后端Json数据返回如下:
{"code":200,"data":{"list":[{"password":"21232f297a57a5a743894a0e4a801fc3","role_id":1,"id":"0","account":"admin","username":"admin"},{"password":"21232f297a57a5a743894a0e4a801fc3","role_id":2,"id":"1","account":"test","username":"test"},{"password":"21232f297a57a5a743894a0e4a801fc3","role_id":1,"id":"10","account":"admin","username":"admin"},{"password":"21232f297a57a5a743894a0e4a801fc3","role_id":2,"id":"11","account":"test","username":"test"},{"password":"1213","role_id":2,"id":"12","account":"test","username":"test"},{"password":"21232f297a57a5a743894a0e4a801fc3","role_id":1,"id":"2","account":"admin","username":"admin"},{"password":"21232f297a57a5a743894a0e4a801fc3","role_id":2,"id":"3","account":"test","username":"test"},{"password":"21232f297a57a5a743894a0e4a801fc3","role_id":1,"id":"4","account":"admin","username":"admin"},{"password":"21232f297a57a5a743894a0e4a801fc3","role_id":2,"id":"5","account":"test","username":"test"},{"password":"21232f297a57a5a743894a0e4a801fc3","role_id":1,"id":"6","account":"admin","username":"admin"}],"pageNumber":1,"pageSize":10,"totalPage":2,"totalRow":13,"firstPage":true,"lastPage":false}}
  1. Element UI表格样式引入
<template>
  <center>
    <el-table :data="tableData.filter(data => !search || data.name.toLowerCase().includes(search.toLowerCase()))"
              style="width: 80%;">
      <el-table-column label="序号"
                       width="180"
                       align="center">
        <template slot-scope="scope">
          <span v-text="getIndex(scope.$index)"></span>
        </template>
      </el-table-column>
      <el-table-column label="账号"
                       prop="account">
      </el-table-column>
      <el-table-column label="姓名"
                       prop="username">
      </el-table-column>
      <el-table-column align="right">
        <template slot="header"
                  slot-scope="scope">
          <el-input v-model="search"
                    size="mini"
                    placeholder="输入关键字搜索" />
        </template>
        <template slot-scope="scope">
          <el-button size="mini"
                     @click="handleEdit(scope.$index, scope.row)">Edit</el-button>
          <el-button size="mini"
                     type="danger"
                     @click="handleDelete(scope.$index, scope.row)">Delete</el-button>
        </template>
      </el-table-column>
    </el-table>
	//分页方法
    <el-pagination @size-change="handleSizeChange"
                   @current-change="handleCurrentChange"
                   :current-page="pageNumber"
                   :page-sizes="[10, 20, 30, 40]"
                   :page-size="pageSize"
                   layout="total, sizes, prev, pager, next, jumper"
                   :total="this.totalRow">
    </el-pagination>
  </center>
</template>
  1. 请求分页的数据
<script>
import { getUserList } from '@/api/user'
export default {
  data () {
    return {
      tableData: [],
      search: '',
      pageNumber: 1,//当前页
      pageSize: 10,//每页大小
      totalRow: 0,//总条数
      totalPage: 0,//总页数
      input: ''
    }
  },

  methods: {
    handleEdit (index, row) {
      console.log(index, row);
    },
    handleDelete (index, row) {
      console.log(index, row);
    },
    //分页点击方法
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`);
      this.pageSize = val
      var params = {
        pageNumber: this.pageNumber,
        pageSize: this.pageSize
      }
      this.findAll(params)
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`);
      this.pageNumber = val
      var params = {
        pageNumber: this.pageNumber,
        pageSize: this.pageSize
      }
      this.findAll(params)
    },
    //分页方法
    findAll (params) {
      //获取数据接口(调用封装的接口)
      getUserList('/user/getUserList', params).then(res => {
        if (res.code === 200) {
          this.tableData = res.data.list
          this.totalRow = res.data.totalRow
          console.log(this.tableData.length)
        } else if (res.code === 500) {
          this.$message(res.data)
        }
      })
    },
    //添加表格序号
    getIndex ($index) {
      return (this.pageNumber - 1) * this.pageSize + $index + 1
    }
  },
  //初始化的时候初始化表格大小
  created () {
    var params = {
      pageNumber: this.pageNumber,
      pageSize: this.pageSize
    }
    this.findAll(params)
  }

}
</script>
Vue ElementUI提供了非常方便的表格组件,同时也支持前端分页功能。以下是一个简单的示例代码,演示如何在Vue ElementUI实现前端分页: ```vue <template> <div> <el-table :data="tableData" :page-size="pageSize" :current-page="currentPage" :total="total" @current-change="handleCurrentChange"> <el-table-column prop="name" label="Name"></el-table-column> <el-table-column prop="age" label="Age"></el-table-column> <el-table-column prop="gender" label="Gender"></el-table-column> </el-table> </div> </template> <script> export default { data() { return { tableData: [], // 表格数据 pageSize: 10, // 每页显示条数 currentPage: 1, // 当前页数 total: 0 // 总条数 }; }, mounted() { // 初始化数据 this.getTableData(); }, methods: { getTableData() { // 发送请求获取数据,这里使用假数据 const data = [ { name: "Alice", age: 18, gender: "female" }, { name: "Bob", age: 20, gender: "male" }, { name: "Charlie", age: 22, gender: "male" }, { name: "David", age: 24, gender: "male" }, { name: "Emily", age: 26, gender: "female" }, { name: "Frank", age: 28, gender: "male" }, { name: "Grace", age: 30, gender: "female" }, { name: "Henry", age: 32, gender: "male" }, { name: "Iris", age: 34, gender: "female" }, { name: "Jack", age: 36, gender: "male" }, { name: "Kate", age: 38, gender: "female" }, { name: "Leo", age: 40, gender: "male" } ]; // 设置总条数 this.total = data.length; // 计算起始索引和终止索引 const startIndex = (this.currentPage - 1) * this.pageSize; const endIndex = startIndex + this.pageSize; // 获取当前页的数据 this.tableData = data.slice(startIndex, endIndex); }, handleCurrentChange(currentPage) { // 当前页数发生变化时重新获取数据 this.currentPage = currentPage; this.getTableData(); } } }; </script> ``` 在上述代码中,我们使用了Vue ElementUI的el-table组件来展示表格数据,通过设置pageSize和currentPage属性来控制每页显示条数和当前页数,使用@current-change事件来监听当前页数的变化,然后再通过计算得到当前页的数据,最后渲染到表格中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

全栈程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值