UNPKG

@ticatec/app-data-manager

Version:

A comprehensive TypeScript library providing hierarchical data manager classes for CRUD operations, pagination, and data management in frontend applications. Features include full list, paged, and stackable data managers with built-in caching and transfor

172 lines (171 loc) 4.49 kB
import BaseDataManager from "./BaseDataManager"; import { utils } from "@ticatec/enhanced-utils"; /** * 通用分页数据管理器抽象类,提供分页查询和数据管理功能 * @template T 继承自PagingDataService的服务类型 */ export default class CommonPagedDataManager extends BaseDataManager { static rowsCount = 25; static rowsKey = 'rows'; static pageNoKey = 'page'; #criteria; #pageCount = 1; #pageNo = 1; #count = 0; #rows; // /** * 构造函数 * @param service 分页数据服务实例 * @param keyField 主键字段名或相等性检查函数 * @param options 配置选项 * @protected */ constructor(service, keyField, options = null) { super(service, keyField, options); this.list = []; this.#rows = CommonPagedDataManager.rowsCount; this.#criteria = this.tagData == null ? {} : utils.clone(this.tagData); } /** * 设置分页查询时每页行数的属性名称 * @param value 每页行数 */ static setRowsPerPage(value) { CommonPagedDataManager.rowsCount = value; } /** * 设置默认的每页行数 * @param value 行数对应的属性名 */ static setRowsKey(value) { CommonPagedDataManager.rowsKey = value; } /** * 设置分页查询时候页吗对应的属性名称 * @param value 页码对应的属性名 */ static setPageNoKey(value) { CommonPagedDataManager.pageNoKey = value; } /** * 根据条件通过接口代理查询 * @param criteria 查询条件 * @returns 查询结果 * @protected */ async searchViaProxy(criteria) { return this.service.search(criteria); } /** * 根据条件查询数据 * @param criteria 查询条件 * @param pageNo 页码,默认为1 */ async searchData(criteria, pageNo = 1) { if (pageNo > this.#pageCount) { pageNo = this.#pageCount; } if (pageNo <= 0) { pageNo = 1; } if (pageNo != this.#pageNo) { this.#pageNo = pageNo; } criteria[CommonPagedDataManager.pageNoKey] = pageNo; criteria[CommonPagedDataManager.rowsKey] = this.#rows; let result = await this.searchViaProxy(criteria); this.processDataResult(result); this.#pageCount = Math.floor((result.count - 1) / criteria.rows) + 1; this.#pageNo = pageNo || 1; this.#count = result.count; this.#criteria = utils.clone(criteria); } /** * 重置查询条件 */ resetCriteria() { this.#criteria = this.tagData == null ? {} : utils.clone(this.tagData); return this.#criteria; } /** * 设置新的显示页 * @param value 页码 */ async setPageNo(value) { await this.searchData(this.#criteria, value); } /** * 重新设置每页的行数,并从首页查询 * @param value 每页行数 */ async setRowsPage(value) { this.#rows = value; this.#pageNo = 1; await this.searchData(this.#criteria, this.#pageNo); } /** * 重置条件查询 */ async resetSearch() { await this.searchData(this.buildCriteria()); } /** * 按照条件重新查询 * @param criteria 查询条件 */ async search(criteria) { await this.searchData(criteria); } /** * 设置查询条件 * @param criteria 查询条件 * @deprecated 请使用search方法 */ async setCriteria(criteria) { await this.searchData(criteria); } /** * 刷新查询数据 */ async refresh() { await this.searchData(this.#criteria); } /** * 当前的查询条件 * @returns 查询条件对象 */ get criteria() { return this.#criteria; } /** * 获取当前页码 * @returns 当前页码 * @protected */ getPageNo() { return this.#pageNo; } /** * 总页数 * @returns 总页数 * @protected */ getPageCount() { return this.#pageCount; } /** * 返回纪录总数 * @returns 纪录总数 */ get count() { return this.#count; } /** * 构建查询条件 * @returns 查询条件对象 * @protected */ buildCriteria() { return { ...this.tagData }; } }