Skip to content

Commit aadc952

Browse files
committed
Enh(treeView): fix #3475 add option to hide + on nodes with no children
1 parent 21c895d commit aadc952

File tree

4 files changed

+41
-10
lines changed

4 files changed

+41
-10
lines changed

misc/tutorial/215_treeView.ngdoc

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ the tree.
1717

1818
TreeView is still alpha, and under development, however it is included in the distribution files
1919
to allow people to start using it. Notable outstandings are:
20-
- doesn't calculate or display counts of child nodes anywhere, it would be nice if it did
20+
- calculates but doesn't display counts of child nodes anywhere, it would be nice if it did
2121
- it would be nice to display an hourglass or icon whilst additional data was loading, the current
2222
arrangement means the grid doesn't know whether or not you're adding additional data
2323
- perhaps we could permit sorting of the nodes, and the children within each node, rather than sorting the
2424
whole data set. This would be a whole new sort algorithm though
25-
- it might be nice if nodes with no children could have their + removed, we'd need some way to be sure
26-
that these weren't nodes for which children are going to be dynamically loaded
2725

2826
Options to watch out for include:
2927

3028
- `treeViewIndent`: the expand buttons are indented by a number of pixels (default 10) as the tree
3129
level gets deeper. Larger values look nicer
3230
- `treeViewRowHeaderWidth`: the width of the tree row header
31+
- `showTreeExpandNoChildren`: defaults to true. Shows the + even if there are no children, allows
32+
you to dynamically load children. If set to false there'll be no + if there are no children
3333

3434
@example
3535
In this example most of the data is loaded on initial page load. The nodes under Guerrero Lopez, however,

src/features/tree-view/js/tree-view.js

+36-5
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
* will run a rowsProcessor to set expand buttons alongside these nodes, and will maintain the
2222
* expand/collapse state of each node.
2323
*
24-
* In future a count of the direct children of each node could optionally be calculated and displayed
25-
* alongside the node - the current issue is deciding where to display that. For now we calculate it
26-
* but don't display it.
24+
* A count of the direct children of each node is calculated in the expanded states, the current
25+
* issue is deciding where to display that. For now we calculate it but don't display it.
2726
*
28-
* In future the count could be used to remove the + from a row that doesn't actually have any children.
27+
* The count can be used to remove the + from a row that doesn't actually have any children, if you
28+
* set the option treeHideNoChildren.
2929
*
3030
* Optionally the treeView can be populated only when nodes are clicked on. This will provide callbacks when
3131
* nodes are expanded, requesting the additional data. The node will be set to expanded, and when the data
@@ -358,6 +358,17 @@
358358
* <br/>Defaults to true
359359
*/
360360
gridOptions.showTreeViewRowHeader = gridOptions.showTreeViewRowHeader !== false;
361+
362+
/**
363+
* @ngdoc object
364+
* @name showTreeExpandNoChildren
365+
* @propertyOf ui.grid.treeView.api:GridOptions
366+
* @description If set to true, show the expand/collapse button even if there are no
367+
* children of a node. You'd use this if you're planning to dynamically load the children
368+
*
369+
* <br/>Defaults to true
370+
*/
371+
gridOptions.showTreeExpandNoChildren = gridOptions.showTreeExpandNoChildren !== false;
361372
},
362373

363374

@@ -586,6 +597,9 @@
586597
row.visible = true;
587598
}
588599

600+
// increment the child count on the parent
601+
service.incrementChildCount(parents);
602+
589603
// if this row is a node, then add it to the parents array
590604
if ( typeof(row.treeLevel) !== 'undefined' && row.treeLevel > -1 ){
591605
service.addOrUseState(grid, row, parents);
@@ -630,12 +644,14 @@
630644
grid.treeView.rowExpandedStates[row.uid] = { state: uiGridTreeViewConstants.COLLAPSED, row: row };
631645
}
632646
row.treeExpandedState = grid.treeView.rowExpandedStates[row.uid];
647+
row.treeExpandedState.childCount = 0;
633648
} else {
634649
var parentState = parents[parents.length - 1].treeExpandedState;
635650
if ( typeof(parentState[row.uid]) === 'undefined') {
636651
parentState[row.uid] = { state: uiGridTreeViewConstants.COLLAPSED, row: row };
637652
}
638653
row.treeExpandedState = parentState[row.uid];
654+
row.treeExpandedState.childCount = 0;
639655
}
640656
parents.push(row);
641657
},
@@ -661,8 +677,23 @@
661677
});
662678

663679
return currentState;
664-
}
680+
},
665681

682+
683+
/**
684+
* @ngdoc function
685+
* @name incrementChildCount
686+
* @methodOf ui.grid.treeView.service:uiGridTreeViewService
687+
* @description Increments the child count of the lowest node in the parents array.
688+
*
689+
* @param {array} parents an array of the parents this row should have
690+
* @returns {string} the state we should be setting to any nodes we see
691+
*/
692+
incrementChildCount: function( parents ){
693+
if ( parents.length > 0 ){
694+
parents[parents.length - 1].treeExpandedState.childCount++;
695+
}
696+
}
666697
};
667698

668699
return service;
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<div class="ui-grid-tree-view-row-header-buttons" ng-class="{'ui-grid-tree-view-header': row.treeLevel > - 1}" ng-click="treeViewButtonClick(row, $event)">
2-
<i ng-class="{'ui-grid-icon-minus-squared': row.treeExpandedState.state === 'expanded', 'ui-grid-icon-plus-squared': row.treeExpandedState.state === 'collapsed'}" ng-style="{'padding-left': grid.options.treeViewIndent * row.entity.$$treeLevel + 'px'}"></i>
2+
<i ng-class="{'ui-grid-icon-minus-squared': ( grid.options.showTreeExpandNoChildren || row.treeExpandedState.childCount > 0 ) && row.treeExpandedState.state === 'expanded', 'ui-grid-icon-plus-squared': ( grid.options.showTreeExpandNoChildren || row.treeExpandedState.childCount > 0 ) && row.treeExpandedState.state === 'collapsed'}" ng-style="{'padding-left': grid.options.treeViewIndent * row.entity.$$treeLevel + 'px'}"></i>
33
&nbsp;
44
</div>

src/features/tree-view/test/tree-view.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
describe('ui.grid.treeView uiGridTreeViewService', function () {
1+
ddescribe('ui.grid.treeView uiGridTreeViewService', function () {
22
var uiGridTreeViewService;
33
var uiGridTreeViewConstants;
44
var gridClassFactory;

0 commit comments

Comments
 (0)