11import { EventEmitter , Injectable } from '@angular/core' ;
2- import { DEFAULT_OWNER , ExportHeaderType , IColumnInfo , IExportRecord , IgxBaseExporter } from '../exporter-common/base-export-service' ;
2+ import { DEFAULT_OWNER , ExportHeaderType , ExportRecordType , IColumnInfo , IExportRecord , IgxBaseExporter } from '../exporter-common/base-export-service' ;
33import { ExportUtilities } from '../exporter-common/export-utilities' ;
44import { CharSeparatedValueData } from './char-separated-value-data' ;
55import { CsvFileTypes , IgxCsvExporterOptions } from './csv-exporter-options' ;
@@ -50,14 +50,40 @@ export class IgxCsvExporterService extends IgxBaseExporter {
5050 private _stringData : string ;
5151
5252 protected exportDataImplementation ( data : IExportRecord [ ] , options : IgxCsvExporterOptions , done : ( ) => void ) {
53- const dimensionKeys = data [ 0 ] ?. dimensionKeys ;
54- data = dimensionKeys ?. length ?
55- data . map ( ( item ) => item . rawData ) :
56- data . map ( ( item ) => item . data ) ;
53+ const firstDataElement = data [ 0 ] ;
54+ const dimensionKeys = firstDataElement ?. dimensionKeys ;
55+
56+ const dataRecords = dimensionKeys ?. length ?
57+ data . filter ( item => item . type !== ExportRecordType . SummaryRecord ) . map ( ( item ) => item . rawData ) :
58+ data . filter ( item => item . type !== ExportRecordType . SummaryRecord ) . map ( ( item ) => item . data ) ;
59+
60+ // Get summary records if exportSummaries is enabled
61+ const summaryRecords : any [ ] = [ ] ;
62+ if ( options . exportSummaries ) {
63+ const summaries = data . filter ( item => item . type === ExportRecordType . SummaryRecord ) ;
64+ for ( const summary of summaries ) {
65+ // Convert summary record data to a flat object format for CSV
66+ const summaryData : any = { } ;
67+ if ( summary . data ) {
68+ for ( const [ key , value ] of Object . entries ( summary . data ) ) {
69+ if ( value && typeof value === 'object' && 'label' in value && 'value' in value ) {
70+ summaryData [ key ] = `${ value . label } : ${ value . value } ` ;
71+ } else {
72+ summaryData [ key ] = value ;
73+ }
74+ }
75+ }
76+ summaryRecords . push ( summaryData ) ;
77+ }
78+ }
79+
80+ // Combine data records and summary records
81+ const allRecords = [ ...dataRecords , ...summaryRecords ] ;
82+
5783 const columnList = this . _ownersMap . get ( DEFAULT_OWNER ) ;
5884 const columns = columnList ?. columns . filter ( c => c . headerType === ExportHeaderType . ColumnHeader ) ;
5985 if ( dimensionKeys ) {
60- const dimensionCols = dimensionKeys . map ( ( key ) => {
86+ const dimensionCols = dimensionKeys . map ( ( key ) => {
6187 const columnInfo : IColumnInfo = {
6288 header : key ,
6389 field : key ,
@@ -72,13 +98,13 @@ export class IgxCsvExporterService extends IgxBaseExporter {
7298 columns . unshift ( ...dimensionCols ) ;
7399 }
74100
75- const csvData = new CharSeparatedValueData ( data , options . valueDelimiter , columns ) ;
101+ const csvData = new CharSeparatedValueData ( allRecords , options . valueDelimiter , columns ) ;
76102 csvData . prepareDataAsync ( ( r ) => {
77103 this . _stringData = r ;
78104 this . saveFile ( options ) ;
79105 this . exportEnded . emit ( { csvData : this . _stringData } ) ;
80106 done ( ) ;
81- } ) ;
107+ } , options . alwaysExportHeaders ) ;
82108 }
83109
84110 private saveFile ( options : IgxCsvExporterOptions ) {
0 commit comments