小程序 - 分页

const qp = getApp(),
	qpApi = qp.api;

Page({
	data: {
		paging: {
			list: [], // 分页数据集合
			size: 10, // 分页单页长度
			page: 0, // 分页第几页
			more: 0, // 分页是否显示更多
			over: 0 // 分页是否全部加载
		}
	},

	onLoad: function () {
		this.onRender();
	},

	onPullDownRefresh: function () {
		this.data.paging.page = 0;
		this.onRender();
	},

	onRender: function () {
		this.setData({
			loading: 0
		});

		this.getPagingList();
	},

	onReachBottom: function () {
		const ts = this,
			td = ts.data;

		if (td.paging.more) {
			td.paging.more = 0;
			ts.getPagingList();
		} else if (td.paging.over) {
			td.paging.over = 0;
			qp.tip('已经全部加载完毕');
		}
	},

	getPagingList: function () {
		const ts = this,
			td = ts.data;

		qp.post(qpApi.requestUrl, {
			data: {
				currentPage: td.paging.page,
				pageSize: td.paging.size
			},
			success: function (res, size) {
				const _list = res.activityList;

				if (td.paging.page) {
					ts.setData({
						['paging.list[' + td.paging.page + ']']: _list
					});
				} else {
					size && (td.paging.size = size);

					ts.setData({
						['paging.list']: null,
						['paging.list[' + td.paging.page + ']']: _list
					});
				}

				if (_list.length === td.paging.size) {
					td.paging.page++;
					td.paging.more = 1;
				} else if (td.paging.page) {
					_list.length ? (td.paging.over = 1) : qp.tip('已经全部加载完毕');
				}
			},
			error: function () {
				td.paging.more = 1;
			},
			complete: function () {
				!td.loading && ts.setData({
					loading: 1
				});
			}
		});
	}
});

猜你喜欢

转载自blog.csdn.net/NaShiShiWo/article/details/84429790