便利贴--46{基于移动端长页中分页加载逻辑封装}
使用
this.pages = new Pagination({
method: inspectionList,
value: this.ourData.tabel.value,
search: { propertyType: 2 },
mustSearch: ["checkState"],
whereList: ["rows"],
pageName: "pageNum",
outTotal: (num) => {
console.log(num);
},
fn: (res, reset) => {
doData(res, reset);
},
});
this.pages.onSearch();
scrollBottom(e) {
let bottomPosition = e.target.scrollHeight - e.target.clientHeight,
nowPosition = e.target.scrollTop,
gap = bottomPosition - e.target.scrollTop;
if (gap <= 100) {
if (!this.pages.onMove) {
this.pages.next();
}
}
},
源代码
export default class pagination {
constructor(arg) {
this.method = arg?.method || '';
this.search = arg?.search || {};
this.otherSearch = arg?.otherSearch || {};
this.pageName = arg?.pageName || 'page'
this.pageSizeName = arg?.pageSizeName || 'pageSize'
this.page = arg?.page || {
current: 1,
pageSize: 10,
total: 0,
state: false,
stop: false
};
this.value = arg.value || [];
this.mustSearch = arg.mustSearch || [];
this.listName = arg.listName || '';
this.fn = arg.fn || '';
this.outTotal = arg.outTotal || function (r) {
console.log(r, 'total输出')
}
this.onMove = false;
this.whereList = arg.whereList || [];
}
async getData(reset) {
if (this.onMove) {
console.log('操作频繁,请稍后重试!')
return
}
if (this.page.stop) return;
let data = {};
data[this.pageName] = this.page.current;
data[this.pageSizeName] = this.page.pageSize;
if (Object.keys(this.search).length != 0) {
for (let k in this.search) {
data[k] = this.search[k];
}
}
if (Object.keys(this.otherSearch).length != 0) {
for (let k in this.otherSearch) {
data[k] = this.otherSearch[k];
}
}
if (!this.method) {
console.log('未设置api')
return
}
console.log(data, '传参see')
this.onMove = true
let res = await this.method(data);
this.onMove = false
console.log(res, '返回see');
if (this.fn) {
this.fn(res, reset)
}
let useList = res;
if (this.whereList.length > 0) {
for (let k in this.whereList) {
useList = useList[this.whereList[k]]
}
}
if (!useList || !res?.total) {
this.outTotal(0)
return
};
this.outTotal(res?.total)
this.value.push(...useList);
this.page.total = res?.total;
if (this.value.length < this.page.total) {
this.page.state = true;
this.page.stop = false;
this.page.current++;
} else {
this.page.state = true;
this.page.stop = true;
}
}
next() {
this.getData();
};
onSearch(data, other = {
reset: true
}) {
if (other.reset) {
this.onReset();
}
let must = this.mustSearch;
for (let k in data) {
if (must.includes(k)) {
this.search[k] = data[k]
} else {
this.otherSearch[k] = data[k];
}
}
if (!this.passSearch()) {
return
};
this.getData(other.reset);
};
onReset() {
if (this.value.length > 0) {
this.value.splice(0, this.value.length);
}
this.page = {
current: 1,
pageSize: 10,
total: 0,
state: false,
stop: false
};
this.onMove = false;
};
passSearch() {
let must = this.mustSearch,
i = 0;
for (let k in this.search) {
if (must.includes(k)) {
i++;
}
}
if (i == must.length) {
return true
} else {
return false
}
};
setMethod(method, search) {
this.method = method || "";
search &&
this.onSearch()
}
setMustSearch(mustSearch) {
this.mustSearch = mustSearch || "";
}
}