gpt4 book ai didi

javascript - 导出前的 Ag-grid Angular 格式数据

转载 作者:行者123 更新时间:2023-11-29 23:09:02 25 4
gpt4 key购买 nike

我有要导出的网格:

initializeColumnDefs() {
this.columnDefs = [];
this.columnDefs.push({
headerName: 'time,
field: 'completedTimestamp',
cellRenderer: (params: any) => {
if (params.data.isMomentarily)
return '';
return DatagridComponent.DefaultDatetimeCellRenderer(params);
},
comparator: (valueA: number, valueB: number) => {
return DatagridComponent.DefaultDatetimeCellComparator(valueA, valueB);
}
},
{
headerName: 'people',
field: 'people',
cellRenderer: (params: any) => {
if (!params || !params.value || params.value.length <= 0)
return '';
let titles = '';
params.value.forEach(element => {
if (element.name) {
titles += element.name + ',';
}
});
return titles.substring(0, titles.length - 1);
}
}
);
}

上面有两列的示例:一列带有时间戳,一列带有对象。

我用来导出为 csv 的 export() 方法:

  export() {
let header = this.columnDefs.map(columnDef => {
let id = columnDef.field || columnDef.colId || columnDef.value;
let headerName = columnDef.headerName;
return headerName;
});
let a: any;
let params: any = {
fileName: 'export.csv',
columnSeparator: ';',
skipHeader: true,
columnKeys: this.columnDefs.map(c => c.field || c.colId).filter(c => !!c)
};
params.customHeader = header.join(params.columnSeparator) + '\n';
this.grid.api.exportDataAsCsv(params);
}

但是,我很难找到导出前如何格式化值,因为这里我只得到标题和字段而没有值?当我将我的网格导出到 csv 而不是 datetime 时,我得到例如enter image description here

这是时间戳,对于我的对象,我得到 enter image description here

而不是让 Tom, Bob, Ben

有谁知道如何在导出前格式化这些值?

最佳答案

在您的export() 函数中,您必须添加一个参数processCellCallback

像这样:

export() {
let header = this.columnDefs.map(columnDef => {
let id = columnDef.field || columnDef.colId || columnDef.value;
let headerName = columnDef.headerName;
return headerName;
});
let a: any;
let params: any = {
fileName: 'export.csv',
columnSeparator: ';',
skipHeader: true,
columnKeys: this.columnDefs.map(c => c.field || c.colId).filter(c => !!c)
};
params.customHeader = header.join(params.columnSeparator) + '\n';
params.processCellCallback = function(cellParams) {
if(cellParams && cellParams.column.colId === 'yourTimestampfield') {
return this.formatter; //apply your timestamp formatter
} else if(cellParams && cellParams.column.colId === 'yourObjectfield') {
return this.formatter; //apply your object formatter
} else
return cellParams.value // no formatting
}
this.grid.api.exportDataAsCsv(params);
}

在示例和文档中阅读更多信息 here .

关于javascript - 导出前的 Ag-grid Angular 格式数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54145795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com