Skip to content

Commit 4c63239

Browse files
Portugal, Marcelomportuga
Portugal, Marcelo
authored andcommitted
fix(exporter.js): Exporter will respect headerCellFilter and cellFilter.
Ensured that when exporterHeaderFilter and exporterFieldCallback are not defined, that the exporter will respect the headerCellFilter and cellFilter attributes. fix #6628, fix #5377, fix #5159
1 parent 3134f77 commit 4c63239

File tree

2 files changed

+40
-70
lines changed

2 files changed

+40
-70
lines changed

Diff for: src/features/exporter/js/exporter.js

+23-10
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,8 @@
7575
*
7676
* @description Services for exporter feature
7777
*/
78-
module.service('uiGridExporterService', ['$q', 'uiGridExporterConstants', 'gridUtil', '$compile', '$interval', 'i18nService',
79-
function ($q, uiGridExporterConstants, gridUtil, $compile, $interval, i18nService) {
80-
78+
module.service('uiGridExporterService', ['$filter', '$q', 'uiGridExporterConstants', 'gridUtil', '$compile', '$interval', 'i18nService',
79+
function ($filter, $q, uiGridExporterConstants, gridUtil, $compile, $interval, i18nService) {
8180
var service = {
8281

8382
delay: 100,
@@ -528,7 +527,7 @@
528527
* }
529528
* </pre>
530529
*/
531-
gridOptions.exporterFieldCallback = gridOptions.exporterFieldCallback ? gridOptions.exporterFieldCallback : function( grid, row, col, value ) { return value; };
530+
gridOptions.exporterFieldCallback = gridOptions.exporterFieldCallback ? gridOptions.exporterFieldCallback : defaultExporterFieldCallback;
532531

533532
/**
534533
* @ngdoc function
@@ -543,7 +542,7 @@
543542
*
544543
* @param {Grid} grid provides the grid in case you have need of it
545544
* @param {GridRow} row the row from which the data comes
546-
* @param {GridCol} col the column from which the data comes
545+
* @param {GridColumn} col the column from which the data comes
547546
* @param {object} value the value for your massaging
548547
* @returns {object} you must return the massaged value ready for exporting
549548
*
@@ -811,13 +810,13 @@
811810
columns = leftColumns.concat(bodyColumns,rightColumns);
812811
}
813812

814-
columns.forEach( function( gridCol, index ) {
815-
// $$hashKey check since when grouping and sorting programmatically this ends up in export. Filtering it out
813+
columns.forEach( function( gridCol ) {
814+
// $$hashKey check since when grouping and sorting pragmatically this ends up in export. Filtering it out
816815
if ( gridCol.colDef.exporterSuppressExport !== true && gridCol.field !== '$$hashKey' &&
817816
grid.options.exporterSuppressColumns.indexOf( gridCol.name ) === -1 ){
818817
var headerEntry = {
819818
name: gridCol.field,
820-
displayName: grid.options.exporterHeaderFilter ? ( grid.options.exporterHeaderFilterUseName ? grid.options.exporterHeaderFilter(gridCol.name) : grid.options.exporterHeaderFilter(gridCol.displayName) ) : gridCol.displayName,
819+
displayName: getDisplayName(grid, gridCol),
821820
width: gridCol.drawnWidth ? gridCol.drawnWidth : gridCol.width,
822821
align: gridCol.colDef.align ? gridCol.colDef.align : (gridCol.colDef.type === 'number' ? 'right' : 'left')
823822
};
@@ -1508,7 +1507,6 @@
15081507
docDefinition = grid.options.exporterExcelCustomFormatters( grid, workbook, docDefinition );
15091508
}
15101509
if ( grid.options.exporterExcelHeader ) {
1511-
var data = [];
15121510
if (angular.isFunction( grid.options.exporterExcelHeader )) {
15131511
grid.options.exporterExcelHeader(grid, workbook, sheet, docDefinition);
15141512
} else {
@@ -1561,8 +1559,23 @@
15611559

15621560
};
15631561

1564-
return service;
1562+
function getDisplayName(grid, gridCol) {
1563+
if (grid.options.exporterHeaderFilter) {
1564+
return grid.options.exporterHeaderFilterUseName ?
1565+
grid.options.exporterHeaderFilter(gridCol.name) :
1566+
grid.options.exporterHeaderFilter(gridCol.displayName);
1567+
}
15651568

1569+
return gridCol.headerCellFilter ?
1570+
$filter(gridCol.headerCellFilter)(gridCol.displayName) :
1571+
gridCol.displayName;
1572+
}
1573+
1574+
function defaultExporterFieldCallback(grid, row, col, value) {
1575+
return col.cellFilter ? $filter(col.cellFilter)(value) : value;
1576+
}
1577+
1578+
return service;
15661579
}
15671580
]);
15681581

Diff for: src/features/exporter/test/exporter.spec.js

+17-60
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe('ui.grid.exporter', function() {
3434

3535
grid = gridClassFactory.createGrid({});
3636
grid.options.columnDefs = [
37-
{field: 'col1', name: 'col1', displayName: 'Col1', width: 50, pinnedLeft: true},
37+
{field: 'col1', name: 'col1', displayName: 'Col1', width: 50, headerCellFilter: 'uppercase', pinnedLeft: true},
3838
{field: 'col2', name: 'col2', displayName: 'Col2', width: '*', type: 'number', cellFilter: 'uppercase'},
3939
{field: 'col3', name: 'col3', displayName: 'Col3', width: 100},
4040
{field: 'col4', name: 'col4', displayName: 'Col4', width: 200}
@@ -115,49 +115,6 @@ describe('ui.grid.exporter', function() {
115115
});
116116
});
117117

118-
describe('defaultGridOptions', function() {
119-
var options;
120-
121-
beforeEach(function () {
122-
options = {};
123-
});
124-
125-
it('set all options to default', function () {
126-
uiGridExporterService.defaultGridOptions(options);
127-
expect(options).toEqual({
128-
exporterSuppressMenu: false,
129-
exporterMenuLabel: 'Export',
130-
exporterCsvColumnSeparator: ',',
131-
exporterCsvFilename: 'download.csv',
132-
exporterPdfFilename: 'download.pdf',
133-
exporterOlderExcelCompatibility: false,
134-
exporterIsExcelCompatible: false,
135-
exporterPdfDefaultStyle: {fontSize: 11},
136-
exporterPdfTableStyle: {margin: [0, 5, 0, 15]},
137-
exporterPdfTableHeaderStyle: {bold: true, fontSize: 12, color: 'black'},
138-
exporterPdfHeader: null,
139-
exporterPdfFooter: null,
140-
exporterPdfOrientation: 'landscape',
141-
exporterPdfPageSize: 'A4',
142-
exporterPdfMaxGridWidth: 720,
143-
exporterPdfCustomFormatter: jasmine.any(Function),
144-
exporterHeaderFilterUseName: false,
145-
exporterMenuAllData: true,
146-
exporterMenuVisibleData: true,
147-
exporterMenuSelectedData: true,
148-
exporterMenuCsv: true,
149-
exporterMenuPdf: true,
150-
exporterMenuExcel: true,
151-
exporterFieldCallback: jasmine.any(Function),
152-
exporterFieldFormatCallback: jasmine.any(Function),
153-
exporterFieldApplyFilters: false,
154-
exporterAllDataFn: null,
155-
exporterSuppressColumns: [],
156-
exporterMenuItemOrder: 200
157-
});
158-
});
159-
});
160-
161118
describe('defaultGridOptions', function() {
162119
var options;
163120

@@ -781,15 +738,15 @@ describe('ui.grid.exporter', function() {
781738
describe('getColumnHeaders', function() {
782739
it('gets visible headers', function() {
783740
expect(uiGridExporterService.getColumnHeaders(grid, uiGridExporterConstants.VISIBLE)).toEqual([
784-
{name: 'col1', displayName: 'Col1', width: 50, align: 'left'},
741+
{name: 'col1', displayName: 'COL1', width: 50, align: 'left'},
785742
{name: 'col2', displayName: 'Col2', width: '*', align: 'right'},
786743
{name: 'col4', displayName: 'Col4', width: 200, align: 'left'}
787744
]);
788745
});
789746

790747
it('gets all headers', function() {
791748
expect(uiGridExporterService.getColumnHeaders(grid, uiGridExporterConstants.ALL)).toEqual([
792-
{name: 'col1', displayName: 'Col1', width: 50, align: 'left'},
749+
{name: 'col1', displayName: 'COL1', width: 50, align: 'left'},
793750
{name: 'col2', displayName: 'Col2', width: '*', align: 'right'},
794751
{name: 'col3', displayName: 'Col3', width: 100, align: 'left'},
795752
{name: 'col4', displayName: 'Col4', width: 200, align: 'left'}
@@ -889,48 +846,48 @@ describe('ui.grid.exporter', function() {
889846
describe('getData', function() {
890847
it('gets all rows and columns', function() {
891848
expect(uiGridExporterService.getData(grid, uiGridExporterConstants.ALL, uiGridExporterConstants.ALL)).toEqual([
892-
[ {value: 'a_0'}, {value: 'b_0'}, {value: 'c_0'}, {value: 'd_0'} ],
893-
[ {value: 'a_1'}, {value: 'b_1'}, {value: 'c_1'}, {value: 'd_1'} ],
894-
[ {value: 'a_2'}, {value: 'b_2'}, {value: 'c_2'}, {value: 'd_2'} ]
849+
[ {value: 'a_0'}, {value: 'B_0'}, {value: 'c_0'}, {value: 'd_0'} ],
850+
[ {value: 'a_1'}, {value: 'B_1'}, {value: 'c_1'}, {value: 'd_1'} ],
851+
[ {value: 'a_2'}, {value: 'B_2'}, {value: 'c_2'}, {value: 'd_2'} ]
895852
]);
896853
});
897854

898855
it('ignores selection row header column', function() {
899856
grid.columns[0].colDef.exporterSuppressExport = true;
900857
expect(uiGridExporterService.getData(grid, uiGridExporterConstants.ALL, uiGridExporterConstants.ALL)).toEqual([
901-
[ {value: 'b_0'}, {value: 'c_0'}, {value: 'd_0'} ],
902-
[ {value: 'b_1'}, {value: 'c_1'}, {value: 'd_1'} ],
903-
[ {value: 'b_2'}, {value: 'c_2'}, {value: 'd_2'} ]
858+
[ {value: 'B_0'}, {value: 'c_0'}, {value: 'd_0'} ],
859+
[ {value: 'B_1'}, {value: 'c_1'}, {value: 'd_1'} ],
860+
[ {value: 'B_2'}, {value: 'c_2'}, {value: 'd_2'} ]
904861
]);
905862
});
906863

907864
it('ignores suppressed column', function() {
908865
grid.options.exporterSuppressColumns = [ 'col1' ];
909866
expect(uiGridExporterService.getData(grid, uiGridExporterConstants.ALL, uiGridExporterConstants.ALL)).toEqual([
910-
[ {value: 'b_0'}, {value: 'c_0'}, {value: 'd_0'} ],
911-
[ {value: 'b_1'}, {value: 'c_1'}, {value: 'd_1'} ],
912-
[ {value: 'b_2'}, {value: 'c_2'}, {value: 'd_2'} ]
867+
[ {value: 'B_0'}, {value: 'c_0'}, {value: 'd_0'} ],
868+
[ {value: 'B_1'}, {value: 'c_1'}, {value: 'd_1'} ],
869+
[ {value: 'B_2'}, {value: 'c_2'}, {value: 'd_2'} ]
913870
]);
914871
});
915872

916873
it('ignores disabled row', function() {
917874
grid.rows[1].exporterEnableExporting = false;
918875
expect(uiGridExporterService.getData(grid, uiGridExporterConstants.ALL, uiGridExporterConstants.ALL)).toEqual([
919-
[ {value: 'a_0'}, {value: 'b_0'}, {value: 'c_0'}, {value: 'd_0'} ],
920-
[ {value: 'a_2'}, {value: 'b_2'}, {value: 'c_2'}, {value: 'd_2'} ]
876+
[ {value: 'a_0'}, {value: 'B_0'}, {value: 'c_0'}, {value: 'd_0'} ],
877+
[ {value: 'a_2'}, {value: 'B_2'}, {value: 'c_2'}, {value: 'd_2'} ]
921878
]);
922879
});
923880

924881
it('gets visible rows and columns', function() {
925882
expect(uiGridExporterService.getData(grid, uiGridExporterConstants.VISIBLE, uiGridExporterConstants.VISIBLE)).toEqual([
926-
[ {value: 'a_0'}, {value: 'b_0'}, {value: 'd_0'} ],
927-
[ {value: 'a_2'}, {value: 'b_2'}, {value: 'd_2'} ]
883+
[ {value: 'a_0'}, {value: 'B_0'}, {value: 'd_0'} ],
884+
[ {value: 'a_2'}, {value: 'B_2'}, {value: 'd_2'} ]
928885
]);
929886
});
930887

931888
it('gets selected rows and visible columns', function() {
932889
expect(uiGridExporterService.getData(grid, uiGridExporterConstants.SELECTED, uiGridExporterConstants.VISIBLE)).toEqual([
933-
[ {value: 'a_0'}, {value: 'b_0'}, {value: 'd_0'} ]
890+
[ {value: 'a_0'}, {value: 'B_0'}, {value: 'd_0'} ]
934891
]);
935892
});
936893

0 commit comments

Comments
 (0)