import { EmopTableProHeaderClass } from "./components/EmopTableProHeaderClass"; import { EmopTableProPageInfoClass } from "./components/EmopTableProPageInfoClass"; export class EmopTableProClass { /** * 横向或纵向支持滚动,也可用于指定滚动区域的宽高度 */ scroll?: { x?: string, y?: string; } = null; /** * 是否是表格树形展开 */ /** * 是否是表格树形展开 */ // tableTree?: { // enable?: boolean, // frontExpand?: boolean, // childrenTag?: string, // } = { // enable: false, // frontExpand: true, // childrenTag: "children" // }; private _mode?: 'checkbox' | 'radio' | 'normal' = 'normal'; /** * 是否渲染序号 */ order?: boolean = false; /** * 表格数据 */ private _data?: Array = []; get data() { // 前端筛选启用时要对数据进行筛选,后端筛选不需要任何操作 return this.frontFilter ? this.dataFilter(this._data) : this._data; } set data(value) { this._data = value; // this.treeKeyGenerate(value, ""); // value.forEach(item => { // this.mapOfExpandedData[item.key] = this.convertTreeToList(item); // }); } /** * 树组件 */ mapOfExpandedData?: any = {}; /** * 表格头 */ header?: EmopTableProHeaderClass[] = []; /** * 表格加载状态 */ loading?: boolean = false; /** * 表格延迟加载时长 */ loadingDelay?: number = 100; /** * 当前选中的数据 */ selected?: any = null; /** * [选择标识符] 在单选或多选时,获取当前行数据的某个属性。 * 当值为以下字段时,将会特殊处理 * @param '@index' 索引值 */ selectedTag?: string = '@index'; /** * 表格模式 * @param checkbox 多选模式 * @param radio 单选模式 * @param normal 普通模式 */ get mode() { return this._mode; } set mode(value: 'checkbox' | 'radio' | 'normal') { if (value === 'checkbox') { this.selected = []; } else { this.selected = null; } this._mode = value; } /** * 表格大小 */ size?: 'middle' | 'small' | 'default' = 'small'; /** * 是否前端分页 */ frontPagination?: boolean = true; /** * 是否前端筛选 */ frontFilter?: boolean = true; /** * 设置分页器 */ pageInfo?: EmopTableProPageInfoClass = new EmopTableProPageInfoClass(true); /** * 暴露此方法 * 添加表格头部 */ public addHeaders?(headers: EmopTableProHeaderClass[]) { headers?.forEach(item => { this.header.push(new EmopTableProHeaderClass(item)); }); } constructor(constructorData: EmopTableProClass) { constructorData.header?.forEach(item => { this.header.push(new EmopTableProHeaderClass(item)); }); delete constructorData.header; if (constructorData.pageInfo) { // for (const key in constructorData.pageInfo) { // this.pageInfo[key] = constructorData.pageInfo[key]; // } // 构造pageInfo this.pageInfo = new EmopTableProPageInfoClass(constructorData.pageInfo); } delete constructorData.pageInfo; // console.log("constructorData.tableTree", JSON.parse(JSON.stringify(constructorData.tableTree))); // if (constructorData.tableTree?.enable) { // for (const key in constructorData.tableTree) { // this.tableTree[key] = constructorData.tableTree[key]; // } // } // delete constructorData.tableTree; // 将构造函数中的参数全部进行构造 for (const key in constructorData) { if (constructorData[key] !== undefined) this[key] = constructorData[key]; } } /** * 树形结构key 生成 */ // treeKeyGenerate?(data: any, lastKey: string) { // if (!data?.length) return lastKey?.substring(1); // data?.forEach((item, index) => { // item['key'] = this.treeKeyGenerate(item?.children, lastKey + "-" + (++index)); // }); // return lastKey?.substring(1); // } // convertTreeToList?(root: any): any[] { // const stack: any[] = []; // const array: any[] = []; // const hashMap = {}; // stack.push({ ...root, level: 0, expand: false }); // while (stack.length !== 0) { // const node = stack.pop()!; // this.visitNode(node, hashMap, array); // if (node.children) { // for (let i = node.children.length - 1; i >= 0; i--) { // stack.push({ ...node.children[i], level: node.level! + 1, expand: false, parent: node }); // } // } // } // return array; // } // visitNode?(node: any, hashMap: { [key: string]: boolean; }, array: any[]): void { // if (!hashMap[node.key]) { // hashMap[node.key] = true; // array.push(node); // } // } /** * 获取搜索参数 */ getParams?(): object { let result = { pageIndex: this.pageInfo.pageIndex, pageSize: this.pageInfo.pageSize, header: this.header.map(item => { let result = { key: item.key }; if (item.sort) result['order'] = item.sort.order; if (item.filter) result['filter'] = item.filter.searchValue; return result; }) }; return result; } clearParams?(): void { this.pageInfo.pageIndex = 1; this.header.forEach(item => { if (item.sort) { item.sort.order = null; } if (item.filter) { item.filter.value = null; item.filter.searchValue = null; item.filter.visible = false; } }); } /** * 数据筛选函数 */ private dataFilter?(data) { const filterList = this.header.filter(item => item.filter); filterList.forEach(item => { data = data.filter(p => { if (item.filter.mode === "checkbox") return item.filter.searchValue?.length ? item.filter.searchValue.includes(p[item.key] + '') : true; else return (p[item.key] + '').includes(item.filter.searchValue || ''); }); }); return data; } }