---数据转换格式
This commit is contained in:
@ -24,12 +24,43 @@ const DataFormat = {
|
||||
* @param formatString 格式化字符串
|
||||
* @returns 格式化后的日期
|
||||
*/
|
||||
formatDate: (date: Date | string | number, fmt: string = 'yyyy-MM-dd HH:mm:ss') => {
|
||||
// return Date.valueOf(date).format(fmt);
|
||||
format: (data: any, options:FormatOption[]) => {
|
||||
for (let i = 0; i < options.length; i++) {
|
||||
const option = options[i];
|
||||
data = option.format(data);
|
||||
}
|
||||
return data;
|
||||
},
|
||||
test: () => {
|
||||
let data: any[] = [
|
||||
{
|
||||
id: 1,
|
||||
name: 'John',
|
||||
age: 30,
|
||||
total: 100548931,
|
||||
amt: 12121.256,
|
||||
createDate: new Date()
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
name: 'John-1',
|
||||
age: 30,
|
||||
total: 8564355.78832,
|
||||
amt: 12121.256,
|
||||
createDate: "2025-06-26 10:25:06.125"
|
||||
}
|
||||
];
|
||||
|
||||
DataFormat.format(data, [
|
||||
new FormatOption('total', FormatType.QTY,"###,###,###,###,###,###,###.00"),
|
||||
new FormatOption('amt', FormatType.AMT, 2),
|
||||
new FormatOption('createDate', FormatType.DATE, DATE_FORMAT.DATE)
|
||||
]);
|
||||
console.log(data);
|
||||
},
|
||||
};
|
||||
|
||||
export class FormatOption {
|
||||
class FormatOption {
|
||||
key:string;
|
||||
type: string;
|
||||
formatString: string|number;
|
||||
@ -45,7 +76,7 @@ export class FormatOption {
|
||||
}
|
||||
|
||||
static createQty(key:string,decimal?:number){
|
||||
return new FormatOption(key,FormatType.QTY,decimal!=undefined?decimal:3);
|
||||
return new FormatOption(key,FormatType.QTY,decimal!=undefined?decimal:3);
|
||||
}
|
||||
|
||||
static createAmt(key:string){
|
||||
@ -55,7 +86,7 @@ export class FormatOption {
|
||||
format(data:any):any{
|
||||
var keys = this.key.split(".");
|
||||
let valueKey = keys[keys.length-1];
|
||||
let valueData;
|
||||
let valueData:any = undefined;
|
||||
if(keys.length<=1){
|
||||
valueData = data;
|
||||
}else{
|
||||
@ -65,13 +96,11 @@ export class FormatOption {
|
||||
for (let i = 0; i < valueData.length; i++) {
|
||||
let item = valueData[i];
|
||||
let val = item[valueKey];
|
||||
item[valueKey] = this.format(val);
|
||||
item[valueKey] = this.formatValue(val);
|
||||
}
|
||||
}
|
||||
if(this.type==FormatType.DATE){
|
||||
|
||||
}else{
|
||||
|
||||
let val = valueData[valueKey];
|
||||
valueData[valueKey] = this.formatValue(val);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
@ -80,18 +109,36 @@ export class FormatOption {
|
||||
if(value==undefined || value==null) return value;
|
||||
try {
|
||||
if(this.type==FormatType.DATE || this.type==FormatType.TIME || this.type==FormatType.DATETIME){
|
||||
return DataFormat.formatDate(value,this.formatString as string);
|
||||
}else if(this.type==FormatType.QTY){
|
||||
return DataFormat.formatQty(value,this.formatString as number);
|
||||
}else if(this.type==FormatType.AMT){
|
||||
return DataFormat.formatAmt(value,this.formatString as number);
|
||||
let fmt = this.formatString as string;
|
||||
return Date.valueOf(value).format(fmt);
|
||||
}else if(this.type==FormatType.QTY || this.type==FormatType.AMT){
|
||||
let numFmt = undefined;
|
||||
if(Number.is(this.formatString)){
|
||||
numFmt = this.createNumberFmt(this.formatString as number);
|
||||
}else{
|
||||
numFmt = this.formatString as string;
|
||||
}
|
||||
return Number.format(Number.parse(value),numFmt) ;
|
||||
}
|
||||
} catch (error) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
createNumberFmt(num:number){
|
||||
let numFmt = "###,###,###,###,###,###";
|
||||
if(num>0){
|
||||
numFmt += ".";
|
||||
for (let i = 0; i < num; i++) {
|
||||
numFmt += "0";
|
||||
}
|
||||
}
|
||||
return numFmt;
|
||||
}
|
||||
}
|
||||
|
||||
export {DataFormat,FormatOption,DATE_FORMAT,FormatType};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user