element ui 配合vue脚手架实现分页功能

一、页面效果
在这里插入图片描述
二、stulist组件代码

<template>
  <div class="container">
    <!-- 搜索框 -->
    <div style="margin-bottom: 20px">
      <el-input
        placeholder="请输入内容"
        prefix-icon="el-icon-search"
        style="width: 300px"
        v-model="searchContent"
      ></el-input>
      <el-button type="primary" @click="SearcHandle">搜索</el-button>
    </div>
    <!-- 学生数据表格 -->
    <el-table :data="tableData" border style="width: 100%" fit>
      <el-table-column type="index" :index="getIndex" label="序号">
      </el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
      <el-table-column prop="gender" label="性别"></el-table-column>
      <el-table-column prop="education" label="学历"></el-table-column>
      <el-table-column prop="phone" label="手机"></el-table-column>
      <el-table-column prop="email" label="邮箱"></el-table-column>
      <el-table-column label="操作" min-width="100">
        <template slot-scope="scope">
          <el-button
            type="success"
            size="small"
            @click="handleEdit(scope.$index, scope.row)"
            >更新</el-button
          >
          <el-button
            type="danger"
            size="small"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[3, 5, 10, 20]"
      :page-size="limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalCount"
    >
    </el-pagination>
  </div>
</template>
<script>
export default {
  data() {
    return {
      tableData: [],
      totalCount: 0,
      limit: 3,
      currentPage: 1,
      searchContent: "",
    };
  },
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.limit = val;
      this.pageHttp();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.pageHttp();
    },
    // 处理序列号
    getIndex(index) {
      return (this.currentPage - 1) * this.limit + index + 1;
    },
    // 发送请求,获取数据
    pageHttp() {
      // stuhttp 这个是封装的axios请求
      this.stuhttp
        .get(`/users?_page=${this.currentPage}&_limit=${this.limit}`)
        .then((res) => {
          this.tableData = res.data;
          this.totalCount = ~~res.total;
        });
    },
    // 搜索事件函数
    SearcHandle() {
      this.stuhttp
        .get(
          `/users?name_like=${this.searchContent}&_page=1&_limit=${this.limit}`
        )
        .then((res) => {
          this.tableData = res.data;
          this.totalCount = ~~res.total;
        });
    },
    handleEdit(index, row) {
      console.log(index, row);
      this.$router.push(`/addStu/${row.id}`);
    },
    handleDelete(index, row) {
      console.log(index, row);
      this.$confirm(`是否要删除这名学生:${row.name} `, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.stuhttp.delete(`/users/${row.id}`).then(() => {
            this.pageHttp();
          });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  mounted(){
     this.stuhttp.get(`/users?_page=1&_limit=${this.limit}`).then((res) => {
      this.tableData = res.data;
      this.totalCount = ~~res.total;
    });
  },
  computed: {},
};
</script>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值