Files
mayfly-go/mayfly_go_web/src/components/pagetable/index.ts

244 lines
5.6 KiB
TypeScript
Raw Normal View History

import { dateFormat } from '@/common/utils/date';
import { getTextWidth } from '@/common/utils/string';
export class TableColumn {
/**
*
*/
prop: string;
/**
*
*/
label: string;
/**
*
*/
autoWidth: boolean = true;
/**
*
*/
addWidth: number = 0;
/**
*
*/
minWidth: number | string;
/**
2023-07-01 21:24:07 +08:00
* prop属性名
*/
2023-07-01 21:24:07 +08:00
slot: boolean = false;
showOverflowTooltip: boolean = true;
sortable: boolean = false;
type: string;
width: number | string;
fixed: any;
align: string = "center"
/**
*
* param1: data, param2: prop
*/
formatFunc: Function
/**
*
*/
show: boolean = true
constructor(prop: string, label: string) {
this.prop = prop;
this.label = label;
}
/**
*
* @param rowData
* @returns
*/
getValueByData(rowData: any) {
if (this.formatFunc) {
return this.formatFunc(rowData, this.prop);
}
return rowData[this.prop];
}
static new(prop: string, label: string): TableColumn {
return new TableColumn(prop, label)
}
setMinWidth(minWidth: number | string): TableColumn {
this.minWidth = minWidth
this.autoWidth = false;
return this;
}
setAddWidth(addWidth: number): TableColumn {
this.addWidth = addWidth
return this;
}
2023-07-01 21:24:07 +08:00
/**
*
* @returns this
*/
isSlot(): TableColumn {
this.slot = true
return this;
}
/**
*
* @param func ( -> data: 该行对应的数据prop: 该列对应的prop属性值)
* @returns
*/
setFormatFunc(func: Function): TableColumn {
this.formatFunc = func;
return this;
}
/**
* 使
* @returns this
*/
isTime(): TableColumn {
this.setFormatFunc((data: any, prop: string) => {
return dateFormat(data[prop])
})
return this;
}
fixedRight(): TableColumn {
this.fixed = "right";
return this;
}
fixedLeft(): TableColumn {
this.fixed = "left";
return this;
}
/**
*
* @param str
* @param tableData
* @param label label也参与宽度计算
* @returns
*/
autoCalculateMinWidth = (tableData: any) => {
const prop = this.prop
const label = this.label
if (!tableData || !tableData.length || tableData.length === 0 || tableData === undefined) {
return 0;
}
let maxWidthText = ""
let maxWidthValue
// 为了兼容formatFunc格式化回调函数
let maxData
// 获取该列中最长的数据(内容)
for (let i = 0; i < tableData.length; i++) {
let nowData = tableData[i]
let nowValue = nowData[prop]
if (!nowValue) {
continue;
}
// 转为字符串比较长度
let nowText = nowValue + "";
if (nowText.length > maxWidthText.length) {
maxWidthText = nowText;
maxWidthValue = nowValue;
maxData = nowData;
}
}
if (this.formatFunc && maxWidthValue) {
maxWidthText = this.formatFunc(maxData, prop) + ""
}
// 需要加上表格的内间距等,视情况加
const contentWidth: number = getTextWidth(maxWidthText) + 30;
// 获取label的宽度取较大的宽度
const columnWidth: number = getTextWidth(label) + 60;
const flexWidth: number = contentWidth > columnWidth ? contentWidth : columnWidth;
// 设置上限与累加需要额外增加的宽度
this.minWidth = (flexWidth > 400 ? 400 : flexWidth) + this.addWidth;
};
}
export class TableQuery {
/**
*
*/
prop: string;
/**
*
*/
label: string;
/**
* textselectdate
*/
type: string;
/**
* select可选值
*/
options: any;
/**
*
*/
slot: string;
constructor(prop: string, label: string) {
this.prop = prop;
this.label = label;
}
static new(prop: string, label: string): TableQuery {
return new TableQuery(prop, label)
}
static text(prop: string, label: string): TableQuery {
const tq = new TableQuery(prop, label)
tq.type = 'text';
return tq;
}
static select(prop: string, label: string): TableQuery {
const tq = new TableQuery(prop, label)
tq.type = 'select';
return tq;
}
static date(prop: string, label: string): TableQuery {
const tq = new TableQuery(prop, label)
tq.type = 'date';
return tq;
}
static slot(prop: string, label: string, slotName: string): TableQuery {
const tq = new TableQuery(prop, label)
tq.slot = slotName;
return tq;
}
setOptions(options: any): TableQuery {
this.options = options;
return this
}
}