Skip to content

Commit 927d43e

Browse files
alancqueirozMarcelo Portugal
authored and
Marcelo Portugal
committed
fix: pass both tree parent nodes and children nodes when exporting
Fix export all data to excel with treeview bug. Example: http://plnkr.co/edit/hFPAO6W3EW8nlzt2 fix #7127, fix #6819
1 parent 158fd2d commit 927d43e

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

packages/exporter/src/js/exporter.js

+12-9
Original file line numberDiff line numberDiff line change
@@ -952,21 +952,24 @@
952952
* recurse down into the children to get to the raw data element
953953
* which is a row without children (a leaf).
954954
* @param {Node} aNode the tree node on the grid
955-
* @returns {Array} an array of leaf nodes
955+
* @returns {Array} an array with all child nodes from aNode
956956
*/
957957
getRowsFromNode: function(aNode) {
958958
var rows = [];
959-
for (var i = 0; i<aNode.children.length; i++) {
960-
if (aNode.children[i].children && aNode.children[i].children.length === 0) {
961-
rows.push(aNode.children[i]);
962-
} else {
963-
var nodeRows = this.getRowsFromNode(aNode.children[i]);
964-
rows = rows.concat(nodeRows);
965-
}
959+
960+
// Push parent node if it is not undefined and has values other than 'children'
961+
var nodeKeys = aNode ? Object.keys(aNode) : ['children'];
962+
if (nodeKeys.length > 1 || nodeKeys[0] != 'children') {
963+
rows.push(aNode);
964+
}
965+
966+
if (aNode && aNode.children && aNode.children.length > 0) {
967+
for (var i = 0; i < aNode.children.length; i++) {
968+
rows = rows.concat(this.getRowsFromNode(aNode.children[i]));
969+
}
966970
}
967971
return rows;
968972
},
969-
970973
/**
971974
* @ngdoc function
972975
* @name getDataSorted

packages/exporter/test/exporter.spec.js

+8
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,14 @@ describe('ui.grid.exporter', function() {
871871
{col1: 'a_4', col2: 'b_4', col3: 'c_4', children: []}
872872
]);
873873
});
874+
it('should return partent rows when they have their own data', function() {
875+
var bNode = {col1: 'a_1', col2: 'b_1', children: [{col1: 'a_2', col2: 'b_2', col3: 'c_2', children: []}]};
876+
877+
expect(uiGridExporterService.getRowsFromNode(bNode)).toEqual([
878+
{col1: 'a_1', col2: 'b_1', children: [{col1: 'a_2', col2: 'b_2', col3: 'c_2', children: []}]},
879+
{col1: 'a_2', col2: 'b_2', col3: 'c_2', children: []}
880+
]);
881+
});
874882
});
875883

876884
describe('getDataSorted', function() {

0 commit comments

Comments
 (0)