---数据转换格式
This commit is contained in:
@ -24,12 +24,43 @@ const DataFormat = {
|
|||||||
* @param formatString 格式化字符串
|
* @param formatString 格式化字符串
|
||||||
* @returns 格式化后的日期
|
* @returns 格式化后的日期
|
||||||
*/
|
*/
|
||||||
formatDate: (date: Date | string | number, fmt: string = 'yyyy-MM-dd HH:mm:ss') => {
|
format: (data: any, options:FormatOption[]) => {
|
||||||
// return Date.valueOf(date).format(fmt);
|
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;
|
key:string;
|
||||||
type: string;
|
type: string;
|
||||||
formatString: string|number;
|
formatString: string|number;
|
||||||
@ -45,7 +76,7 @@ export class FormatOption {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static createQty(key:string,decimal?:number){
|
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){
|
static createAmt(key:string){
|
||||||
@ -55,7 +86,7 @@ export class FormatOption {
|
|||||||
format(data:any):any{
|
format(data:any):any{
|
||||||
var keys = this.key.split(".");
|
var keys = this.key.split(".");
|
||||||
let valueKey = keys[keys.length-1];
|
let valueKey = keys[keys.length-1];
|
||||||
let valueData;
|
let valueData:any = undefined;
|
||||||
if(keys.length<=1){
|
if(keys.length<=1){
|
||||||
valueData = data;
|
valueData = data;
|
||||||
}else{
|
}else{
|
||||||
@ -65,13 +96,11 @@ export class FormatOption {
|
|||||||
for (let i = 0; i < valueData.length; i++) {
|
for (let i = 0; i < valueData.length; i++) {
|
||||||
let item = valueData[i];
|
let item = valueData[i];
|
||||||
let val = item[valueKey];
|
let val = item[valueKey];
|
||||||
item[valueKey] = this.format(val);
|
item[valueKey] = this.formatValue(val);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if(this.type==FormatType.DATE){
|
|
||||||
|
|
||||||
}else{
|
}else{
|
||||||
|
let val = valueData[valueKey];
|
||||||
|
valueData[valueKey] = this.formatValue(val);
|
||||||
}
|
}
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
@ -80,18 +109,36 @@ export class FormatOption {
|
|||||||
if(value==undefined || value==null) return value;
|
if(value==undefined || value==null) return value;
|
||||||
try {
|
try {
|
||||||
if(this.type==FormatType.DATE || this.type==FormatType.TIME || this.type==FormatType.DATETIME){
|
if(this.type==FormatType.DATE || this.type==FormatType.TIME || this.type==FormatType.DATETIME){
|
||||||
return DataFormat.formatDate(value,this.formatString as string);
|
let fmt = this.formatString as string;
|
||||||
}else if(this.type==FormatType.QTY){
|
return Date.valueOf(value).format(fmt);
|
||||||
return DataFormat.formatQty(value,this.formatString as number);
|
}else if(this.type==FormatType.QTY || this.type==FormatType.AMT){
|
||||||
}else if(this.type==FormatType.AMT){
|
let numFmt = undefined;
|
||||||
return DataFormat.formatAmt(value,this.formatString as number);
|
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) {
|
} catch (error) {
|
||||||
return value;
|
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