vue项目中element表格分页里自定义全部后用懒加载方式获取数据

效果图
在这里插入图片描述

1、element表格上加上ref属性

<el-table  :data="tableData" ref="multipleTable"></table>                

2、element分页里自定义‘全部’,主要是通过设置layout,在layout里面加上slot可以实现自定义

<el-pagination
   @current-change="currentChange"
    :current-page="page.pageNum"
    :page-size="page.pageSize"
    :layout="page.layout"
    :total="page.total"
  >
    <el-select
      class="pageSelectContainer"
      v-model="curElPage"
      @change="changeSizes"
      placeholder="请选择"
    >
      <el-option
        v-for="item in optionsPage"
        :key="item.value"
        :label="item.label"
        :value="item.value"
      >
      </el-option>
    </el-select>
  </el-pagination>

optionsPage: [
    {
        label: "10条/页",
        value: 10,
      },
      {
        label: "20条/页",
        value: 20,
      },
      {
        label: "30条/页",
        value: 30,
      },
      {
        label: "全部",
        value: 0,
      },
    ],
    curElPage: 20, //默认每页显示20条
    allNum: 0,
    laypages: 0,
    domTab: null,
    page: {
      total: 0,
      pageNum: 1,
      pageSize: 20,
      // layout: "total, sizes, prev, pager, next,jumper"
      layout: "total, slot, prev, pager, next,jumper",  // 加上slot自定义
    },
    arrTab: [],

//改变每页显示多少条
    changeSizes(val) {
      this.curElPage = val;
      this.page.pageNum = 1;
      if (val == 0) {
        this.page.layout = "total, slot";
      } else {
        this.page.layout = "total, slot, prev, pager, next,jumper";
      }
      this.sizeChange(val);
    },
    sizeChange(val) {
      var num = 0;
      if (val == 0) {
        //选择全部的时候先获取40条的
        num = 40;
        this.allNum = num;
      } else {
        num = val;
      }
      this.page.pageSize = num;
      this.getList();
    },
    // 翻页
    currentChange(val) {
      this.page.pageNum = val;
      this.getList();
    },

3、获取列表时监听element表格实现懒加载,注意removeEventListener清除下监听滚动并且不能使用匿名函数(这里写的是listenerScroll()),不然清不掉

getList(){
	let params = {
	   offset: (this.page.pageNum - 1) * this.page.pageSize,
	   limit: this.page.pageSize,
	  }
  	this.$store.dispatch("queryListCommon", params).then((res) => {
	  	this.page.total = res.data.data.count;
         if (this.allNum != 0) {
           this.laypages = res.data.data.count / this.allNum;
         }
         this.tableData = res.data.data.list;
         this.$nextTick(() => {
            if (this.curElPage == 0) {
              if (this.arrTab && this.arrTab.length != 0) {
              // 拼接数据
                let arr = [...this.arrTab, ...this.tableData];
                this.tableData = arr;
              }
              // 找到表格的滚动条
              this.domTab = this.$refs.multipleTable.bodyWrapper;
              this.domTab.addEventListener("scroll", this.listenerScroll, true);
              // 清除下监听滚动,不然切换别的后再次切换到全部会出问题
              this.domTab.removeEventListener(
                "scroll",
                this.listenerScroll,
                false
              ); 
            } else {
              this.arrTab = [];
            }
          });
  	})
},
listenerScroll() {
      if (this.page.pageNum < this.laypages && this.curElPage == 0) {
        var seeHeight = this.domTab.clientHeight; //可见区域高度
        var nrHeight = this.domTab.scrollHeight; //内容区高度
        var scrollHieght = this.domTab.scrollTop;
        if (nrHeight - seeHeight === scrollHieght) {
          this.arrTab = [...this.tableData];
          this.page.pageNum = this.page.pageNum + 1;
          this.getList();
        }
      }
    },


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值