Skip to content

Commit

Permalink
feat(tree-view): Supports recursive expanding
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu authored and mportuga committed Nov 11, 2017
1 parent 145e366 commit 4843490
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
46 changes: 34 additions & 12 deletions src/features/tree-base/js/tree-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,10 @@
* @methodOf ui.grid.treeBase.api:PublicApi
* @description expand the immediate children of the specified row
* @param {gridRow} row the row you wish to expand
* @param {boolean} recursive true if you wish to expand the row's ancients
*/
expandRow: function (row) {
service.expandRow(grid, row);
expandRow: function (row, recursive) {
service.expandRow(grid, row, recursive);
},

/**
Expand Down Expand Up @@ -804,7 +805,7 @@
if (row.treeNode.state === uiGridTreeBaseConstants.EXPANDED){
service.collapseRow(grid, row);
} else {
service.expandRow(grid, row);
service.expandRow(grid, row, false);
}

grid.queueGridRefresh();
Expand All @@ -819,17 +820,38 @@
*
* @param {Grid} grid grid object
* @param {GridRow} row the row we want to expand
* @param {boolean} recursive true if you wish to expand the row's ancients
*/
expandRow: function ( grid, row ){
if ( typeof(row.treeLevel) === 'undefined' || row.treeLevel === null || row.treeLevel < 0 ){
return;
}
expandRow: function ( grid, row, recursive ){
if ( recursive ){
var parents = [];
while ( row && typeof(row.treeLevel) !== 'undefined' && row.treeLevel !== null && row.treeLevel >= 0 && row.treeNode.state !== uiGridTreeBaseConstants.EXPANDED ){
parents.push(row);
row = row.treeNode.parentRow;
}

if ( row.treeNode.state !== uiGridTreeBaseConstants.EXPANDED ){
row.treeNode.state = uiGridTreeBaseConstants.EXPANDED;
grid.api.treeBase.raise.rowExpanded(row);
grid.treeBase.expandAll = service.allExpanded(grid.treeBase.tree);
grid.queueGridRefresh();
if ( parents.length > 0 ){
row = parents.pop();
while ( row ){
row.treeNode.state = uiGridTreeBaseConstants.EXPANDED;
grid.api.treeBase.raise.rowExpanded(row);
row = parents.pop();
}

grid.treeBase.expandAll = service.allExpanded(grid.treeBase.tree);
grid.queueGridRefresh();
}
} else {
if ( typeof(row.treeLevel) === 'undefined' || row.treeLevel === null || row.treeLevel < 0 ){
return;
}

if ( row.treeNode.state !== uiGridTreeBaseConstants.EXPANDED ){
row.treeNode.state = uiGridTreeBaseConstants.EXPANDED;
grid.api.treeBase.raise.rowExpanded(row);
grid.treeBase.expandAll = service.allExpanded(grid.treeBase.tree);
grid.queueGridRefresh();
}
}
},

Expand Down
9 changes: 9 additions & 0 deletions src/features/tree-base/test/tree-base.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@ describe('ui.grid.treeBase uiGridTreeBaseService', function () {
expect( expandCount ).toEqual(1);
});

it( 'expandRow recursively', function() {
expect( treeRows.length ).toEqual( 2, 'only the level 0 rows are visible' );

grid.api.treeBase.expandRow(grid.rows[4], true);
treeRows = uiGridTreeBaseService.treeRows.call( grid, grid.rows.slice(0) );
expect( treeRows.length ).toEqual( 7, 'children of row 0, row 3 and row 4 are also visible' );
expect( expandCount ).toEqual(3);
});

it( 'expandRowChildren', function() {
expect( treeRows.length ).toEqual( 2, 'only the level 0 rows are visible' );

Expand Down

0 comments on commit 4843490

Please sign in to comment.