javascript - Ag-grid angular format data before exporting - Stack Overflow

I have grid which I want to export: initializeColumnDefs() {this.columnDefs = [];this.columnDefs.push({

I have grid which I want to export:

initializeColumnDefs() {
    this.columnDefs = [];
    this.columnDefs.push({
        headerName: 'time,
        field: 'pletedTimestamp',
        cellRenderer: (params: any) => {
          if (params.data.isMomentarily)
            return '';
          return DatagridComponent.DefaultDatetimeCellRenderer(params);
        },
        parator: (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);
        }
      }
    );
  }

I have grid which I want to export:

initializeColumnDefs() {
    this.columnDefs = [];
    this.columnDefs.push({
        headerName: 'time,
        field: 'pletedTimestamp',
        cellRenderer: (params: any) => {
          if (params.data.isMomentarily)
            return '';
          return DatagridComponent.DefaultDatetimeCellRenderer(params);
        },
        parator: (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);
        }
      }
    );
  }

Above there's example of two columns: one with timestamp, one with object.

My export() method which I use to export as 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);
}

However, I have trouble finding how format values before exporting, because here I only get header and field and no value? And when I export my grid to csv instead of datetime I get e.g.

which is timestamp and for my object I get

Instead of having Tom, Bob, Ben

Does anyone know how to format these values before exporting?

Share edited Jan 31, 2019 at 21:20 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Jan 11, 2019 at 11:38 user122222user122222 2,4496 gold badges46 silver badges89 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

In your export() function, you will have to add a parameter processCellCallback.

Something like this:

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

Read more in the example and docs here.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744882168a4598901.html

相关推荐

  • javascript - Ag-grid angular format data before exporting - Stack Overflow

    I have grid which I want to export: initializeColumnDefs() {this.columnDefs = [];this.columnDefs.push({

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信