添加 usePager 分页操作
This commit is contained in:
@@ -1,39 +1,46 @@
|
|||||||
import { ref } from "vue";
|
import { computed, ref } from "vue";
|
||||||
import { assign, parse } from "../utils";
|
import { assign, parse } from "../utils";
|
||||||
import type { Response } from "../service";
|
import { useListView, type ClListViewItem } from "@/uni_modules/cool-ui";
|
||||||
|
|
||||||
|
// 分页参数类型
|
||||||
type Pagination = {
|
type Pagination = {
|
||||||
page: number;
|
page: number; // 当前页码
|
||||||
size: number;
|
size: number; // 每页数量
|
||||||
total: number;
|
total: number; // 总数量
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 分页响应数据类型
|
||||||
type PagerResponse = {
|
type PagerResponse = {
|
||||||
list: UTSJSONObject[];
|
list: UTSJSONObject[]; // 列表数据
|
||||||
pagination: Pagination;
|
pagination: Pagination; // 分页信息
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 分页回调函数类型
|
||||||
type PagerCallback = (params: UTSJSONObject) => Promise<UTSJSONObject>;
|
type PagerCallback = (params: UTSJSONObject) => Promise<UTSJSONObject>;
|
||||||
|
|
||||||
|
// 分页器类
|
||||||
export class Pager {
|
export class Pager {
|
||||||
public page = 1;
|
public page = 1; // 当前页码
|
||||||
public size = 20;
|
public size = 20; // 每页数量
|
||||||
public total = 0;
|
public total = 0; // 总数量
|
||||||
public list = ref<UTSJSONObject[]>([]);
|
public list = ref<UTSJSONObject[]>([]); // 列表数据
|
||||||
public loading = ref(false);
|
public loading = ref(false); // 加载状态
|
||||||
public refreshing = ref(false);
|
public refreshing = ref(false); // 刷新状态
|
||||||
public finished = ref(false);
|
public finished = ref(false); // 是否加载完成
|
||||||
public params = {} as UTSJSONObject;
|
public params = {} as UTSJSONObject; // 请求参数
|
||||||
public cb: PagerCallback | null = null;
|
public cb: PagerCallback | null = null; // 回调函数
|
||||||
|
|
||||||
|
// 构造函数
|
||||||
constructor(cb: PagerCallback) {
|
constructor(cb: PagerCallback) {
|
||||||
this.cb = cb;
|
this.cb = cb;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 完成加载
|
||||||
done() {
|
done() {
|
||||||
this.loading.value = false;
|
this.loading.value = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 清空数据
|
||||||
clear() {
|
clear() {
|
||||||
this.list.value = [];
|
this.list.value = [];
|
||||||
this.finished.value = false;
|
this.finished.value = false;
|
||||||
@@ -41,29 +48,39 @@ export class Pager {
|
|||||||
this.loading.value = false;
|
this.loading.value = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 刷新数据
|
||||||
public refresh = async (params: UTSJSONObject): Promise<UTSJSONObject> => {
|
public refresh = async (params: UTSJSONObject): Promise<UTSJSONObject> => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
assign(this.params, params);
|
// 合并参数
|
||||||
|
this.params = assign(this.params, params);
|
||||||
|
|
||||||
|
// 构建请求参数
|
||||||
const data = {
|
const data = {
|
||||||
page: this.page,
|
page: this.page,
|
||||||
size: this.size,
|
size: this.size,
|
||||||
...this.params
|
...this.params
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 设置加载状态
|
||||||
this.loading.value = true;
|
this.loading.value = true;
|
||||||
|
|
||||||
|
// 发起请求
|
||||||
this.cb!(data)
|
this.cb!(data)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
|
// 解析响应数据
|
||||||
const { list, pagination } = parse<PagerResponse>(res)!;
|
const { list, pagination } = parse<PagerResponse>(res)!;
|
||||||
|
|
||||||
|
// 更新分页信息
|
||||||
this.page = pagination.page;
|
this.page = pagination.page;
|
||||||
this.size = pagination.size;
|
this.size = pagination.size;
|
||||||
this.total = pagination.total;
|
this.total = pagination.total;
|
||||||
|
|
||||||
|
// 更新加载完成状态
|
||||||
this.finished.value = this.list.value.length >= this.total;
|
this.finished.value = this.list.value.length >= this.total;
|
||||||
|
|
||||||
|
// 更新列表数据
|
||||||
if (data.page == 1) {
|
if (data.page == 1) {
|
||||||
this.list.value = list;
|
this.list.value = [...list];
|
||||||
} else {
|
} else {
|
||||||
this.list.value.push(...list);
|
this.list.value.push(...list);
|
||||||
}
|
}
|
||||||
@@ -79,16 +96,24 @@ export class Pager {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 加载更多数据
|
||||||
public loadMore = () => {
|
public loadMore = () => {
|
||||||
if (this.loading.value || this.finished.value) {
|
if (this.loading.value || this.finished.value) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.page += 1;
|
this.refresh({
|
||||||
this.refresh({});
|
page: this.page + 1
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 列表视图数据
|
||||||
|
public listView = computed<ClListViewItem[]>(() => {
|
||||||
|
return useListView(this.list.value);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 创建分页器实例
|
||||||
export function usePager(cb: PagerCallback) {
|
export function usePager(cb: PagerCallback) {
|
||||||
return new Pager(cb);
|
return new Pager(cb);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user