1、滚动条懒加载实现分页数据获取
- 首先在文件夹directive下的directive.js文件夹中定义全局指令 如下:
'use strict';
import Vue from 'vue'
Vue.directive('focus', {
inserted: function (el) {
el.focus();
}
})
Vue.directive('loadmore', {
bind(el, binding) {
const selectWrap = el.querySelector('.el-table__body-wrapper')
selectWrap.addEventListener('scroll', function () {
let sign = 100
const scrollDistance = this.scrollHeight - this.scrollTop - this.clientHeight
if (scrollDistance <= sign) {
binding.value()
}
})
}
})
Vue.directive('dialogDragWidth', {
bind(el, binding, vnode, oldVnode) {
const dragDom = binding.value.$el.querySelector('.el-dialog');
el.onmousedown = (e) => {
const disX = e.clientX - el.offsetLeft;
document.onmousemove = function(e) {
e.preventDefault();
const l = e.clientX - disX;
dragDom.style.width = `${l}px`;
}
document.onmouseup = function(e) {
document.onmousemove = null;
document.onmouseup = null;
}
}
}
})
Vue.directive('dialogDrag', {
bind(el, binding, vnode, oldVnode) {
const dialogHeaderEl = el.querySelector('.el-dialog__header');
const dragDom = el.querySelector('.el-dialog');
dialogHeaderEl.style.cursor = 'move';
const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null);
dialogHeaderEl.onmousedown = (e) => {
let oevent = e || window.event;
const disX = oevent.clientX - dialogHeaderEl.offsetLeft;
const disY = oevent.clientY - dialogHeaderEl.offsetTop;
let styL=0, styT=0;
if (sty.left.includes('%')) {
styL = +document.body.clientWidth * (+sty.left.replace(/\%/g, '') / 100);
styT = +document.body.clientHeight * (+sty.top.replace(/\%/g, '') / 100);
} else {
styL = sty.left!='auto'?(+sty.left.replace(/\px/g, '')):(dialogHeaderEl.offsetLeft);
styT = sty.top!='auto'?(+sty.top.replace(/\px/g, '')):(dialogHeaderEl.offsetTop);
}
document.onmousemove = function(e) {
let oevent = e || window.event;
const l = oevent.clientX - disX;
const t = oevent.clientY - disY;
dragDom.style.left = `${l + styL}px`;
dragDom.style.top = `${t + styT}px`;
}
document.onmouseup = function(e) {
document.onmousemove = null;
document.onmouseup = null;
}
}
}
})
- 第二步 在el-table中使用指令定义方法、并且在data中定义所需变量
<el-table
:default-sort="{prop: 'date', order: 'descending'}"
size='small'
ref="multipleTable"
:data="tableData"
v-loadmore="loadMore"
v-loading="loadingBox"
tooltip-effect="dark"
:row-class-name="tableRowClassName"
style="width:100%"
height="90%"
@selection-change="handleSelectionChange">
- 第三步 根据所给的接口地址以及需要传进接口的参数进行第一页数据的获取
showlist() {
var th = this;
let jsonR = {
roleName: this.roleName,
keyWord: this.keyWord,
page:0,
pageSize:20,
bBuyer:'10'
}
if ( JSON.parse(localStorage.getItem('userInfo')).datas.data.platformType != 'Z')
jsonR.frspId = JSON.parse(localStorage.getItem('userInfo')).datas.data.tspId ;
this.$http({
method: 'get',
data: jsonR,
url: '/api' + this.$api.partner.addressList,
success: function (res) {
th.tableData = res.datas.data;
th.loadSign = true;
th.loadingBox = false;
th.totalPage = parseInt(res.datas.total/20) ;
}
})
},
loadMore(){
let that=this;
if (this.loadSign) {
this.loadSign = false
this.loadingBox=false
this.page++
if (this.page > this.totalPage) {
return
}
setTimeout(() => {
that.loadSign = true;
that.loadingBox=true;
let jsonR = {
roleName: that.roleName,
keyWord: that.keyWord,
page:this.page,
pageSize:20,
bBuyer:'10'
}
if ( JSON.parse(localStorage.getItem('userInfo')).datas.data.platformType != 'Z')
jsonR.frspId = JSON.parse(localStorage.getItem('userInfo')).datas.data.tspId ;
this.$http({
method: 'get',
data: jsonR,
url: '/api' + this.$api.partner.userSearch,
success: function (res) {
that.tableData = that.tableData.concat(res.datas);
},
error:(res)=>{
console.log('error:',res);
}
})
}, 500)
console.log('到底了', this.page)
}
},
- 梳理好自己的代码 就可以实现分页加载的功能了,如果后台没有返回总页数或者总条数的话,注意需要后台返回一下!!
- 予人玫瑰,手留余香~