# 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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

全栈程序员

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

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

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

打赏作者

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

抵扣说明:

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

余额充值