Skip to content

Commit

Permalink
fix: pass both tree parent nodes and children nodes when exporting
Browse files Browse the repository at this point in the history
Fix export all data to excel with treeview bug. Example: http://plnkr.co/edit/hFPAO6W3EW8nlzt2

fix #7127, fix #6819
  • Loading branch information
alancqueiroz authored and mportuga committed Oct 24, 2020
1 parent 158fd2d commit 3a5ac8a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
21 changes: 12 additions & 9 deletions packages/exporter/src/js/exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -952,21 +952,24 @@
* recurse down into the children to get to the raw data element
* which is a row without children (a leaf).
* @param {Node} aNode the tree node on the grid
* @returns {Array} an array of leaf nodes
* @returns {Array} an array with all child nodes from aNode
*/
getRowsFromNode: function(aNode) {
var rows = [];
for (var i = 0; i<aNode.children.length; i++) {
if (aNode.children[i].children && aNode.children[i].children.length === 0) {
rows.push(aNode.children[i]);
} else {
var nodeRows = this.getRowsFromNode(aNode.children[i]);
rows = rows.concat(nodeRows);
}

// Push parent node if it is not undefined and has values other than 'children'
var nodeKeys = aNode ? Object.keys(aNode) : ['children'];
if (nodeKeys.length > 1 || nodeKeys[0] != 'children') {
rows.push(aNode);
}

if (aNode && aNode.children && aNode.children.length > 0) {
for (var i = 0; i < aNode.children.length; i++) {
rows = rows.concat(this.getRowsFromNode(aNode.children[i]));
}
}
return rows;
},

/**
* @ngdoc function
* @name getDataSorted
Expand Down
8 changes: 8 additions & 0 deletions packages/exporter/test/exporter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,14 @@ describe('ui.grid.exporter', function() {
{col1: 'a_4', col2: 'b_4', col3: 'c_4', children: []}
]);
});
it('should return partent rows when they have their own data', function() {
var bNode = {col1: 'a_1', col2: 'b_1', children: [{col1: 'a_2', col2: 'b_2', col3: 'c_2', children: []}]};

expect(uiGridExporterService.getRowsFromNode(bNode)).toEqual([
{col1: 'a_1', col2: 'b_1', children: [{col1: 'a_2', col2: 'b_2', col3: 'c_2', children: []}]},
{col1: 'a_2', col2: 'b_2', col3: 'c_2', children: []}
]);
});
});

describe('getDataSorted', function() {
Expand Down

0 comments on commit 3a5ac8a

Please sign in to comment.