3

我有要导出的网格:

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);
        }
      }
    );
  }

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

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

  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 时,我得到例如在此处输入图像描述

这是时间戳,对于我的对象,我得到在此处输入图像描述

而不是拥有Tom, Bob, Ben

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

4

1 回答 1

3

在您的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);
    }

在此处阅读示例和文档中的更多信息。

于 2019-01-27T03:04:25.740 回答