我正在使用Angular 1.5查看以下树视图示例:link
在css部分中,它为.grid指定固定高度,如下所示:
.grid {
width: 500px;
height: 500px;
}
是否有办法根据单击树节点的展开方式动态调整此高度?
treeview的相关文档是here。但是,那里显示的示例也有一个固定的高度,我想将其更改为动态高度。
答案 0 :(得分:0)
我设法通过实现动态更改容器高度的方法找到解决方案。在控制器中,我首先初始化将在JS和模板中使用ng-style的变量:
//===================================================
// Initalize height that will be altered later based
// on tree expand/collapse events
//===================================================
var row_height = 30; // use this to set height everywhere
$scope.height = row_height;
$scope.height_px = row_height + 'px'; // used in template with ng-style
然后在初始化树视图网格选项时,可以引用row_height。我们还根据选项中的rowExpanded
和rowCollapsed
事件添加了添加或减少高度的逻辑:
$scope.gridOptions = {
rowHeight: row_height,
enableSorting: true,
enableFiltering: false,
//.....continue other options.....
onRegisterApi: function(gridApi) {
$scope.gridApi = gridApi;
// Add container height for row expanded
$scope.gridApi.treeBase.on.rowExpanded(null, function(row) {
addHeight(row.treeNode.children.length);
});
// Deduct container height for row collapsed after considering
// all child node expanded state
$scope.gridApi.treeBase.on.rowCollapsed(null, function(row) {
// get number of children that are already expanded
var count = row.treeNode.children.length;
for(var i=0; i < row.treeNode.children.length; i++) {
count += getChildCount( row.treeNode.children[i]);
}
deductHeight(count);
});
}
}
然后,我们将方法添加到控制器,以便addHeight()
,deductHeight()
和getChildCount()
工作:
// Method to add height of container dynamically based on number of rows
var addHeight = function (num_rows) {
$scope.height += num_rows * row_height;
$scope.height_px = $scope.height + 'px';
};
// Method to deduct height of container dynamically based on number of rows
var deductHeight = function (num_rows) {
$scope.height -= num_rows * row_height;
if($scope.height <= 0) {
$scope.height = row_height;
}
$scope.height_px = $scope.height + 'px';
};
// Method to get count of expanded child rows for a given node (used recursively)
var getChildCount = function (node) {
var count = 0;
if(node.state == 'expanded') {
// change the state to collapsed for all child nodes
node.state = 'collapsed';
count += node.children.length;
for(var i=0; i < node.children.length; i++) {
count += getChildCount(node.children[i]);
}
return count;
} else {
return 0;
}
};
请注意,当我们折叠节点时,我们递归地获取节点数。我们还将子节点的状态更改为折叠,以便下次正确计算高度。
最后在模板中,我们需要引用$ scope.height_px,它给出了容器的动态高度。我们通过如下设置ng-style="{ 'height': height_px }"
来完成此操作:
<div id="gridTree-1" ui-grid="gridOptions" class="grid" ui-grid-tree-view ui-grid-auto-resize ng-style="{ 'height': height_px }"></div>
在此之后我遇到的一个问题是当鼠标滚动放置在网格顶部时停止工作。这个问题显然有一个建议 here 的解决方案。但是,实际问题与ui-grid.js
中禁用滚动功能有关。在版本3.1.1中,第2721行读取event.preventDefault();
。我不得不对这一行进行评论,以便滚动再次正常工作。
答案 1 :(得分:0)
当用户展开或折叠父行时设置UI网格树视图的手动高度
结帐onRegisterApi:
var rowtpl = '<div ng-repeat="(colRenderIndex, col) in colContainer.renderedColumns track by col.colDef.name" class="ui-grid-cell" ng-class="{ \'disabled\': true, \'ui-grid-row-header-cell\': col.isRowHeader }" ui-grid-cell></div>';
$scope.gridOptions = {
enableColumnResizing: true,
enableSorting: false,
enableColumnMenus: false,
showTreeExpandNoChildren: false,
enableExpandAll:false,
enableTreeView: true,
rowTemplate: rowtpl,
enableFiltering: true,
enableRowSelection: true,
enableRowHeaderSelection: false,
enableHorizontalScrollbar: 0,
multiSelect: false,
modifierKeysToMultiSelect: false,
noUnselect: true,
onRegisterApi: function (gridApi) {
$scope.gridApi = gridApi;
//Row Collapse Set Height
$scope.gridApi.treeBase.on.rowCollapsed($scope, function (row) {
gridHeight();
});
//Row Expand Set Height
$scope.gridApi.treeBase.on.rowExpanded($scope, function (row) {
gridHeight();
});}}
现在查看函数gridHeight(),您可以在其中轻松设置高度值
function gridHeight() {
//timeout function is used due to late updation of $scope.gridApi.core.getVisibleRows()
$timeout(function () {
// 40: header height
// 30: height for each row
const visibleRowHeight = 40 + (30 * $scope.gridApi.core.getVisibleRows().length);
if (visibleRowHeight > 280) {
vm.totalHeight = 280;
}
else {
vm.totalHeight = visibleRowHeight;
}
//Adjust your height max and min accordingly like i did
}, 200);
}
查看:
<div id="grid" ui-grid="gridOptions" class="grid" ui-grid-tree-view ui-grid-tree-row-header-buttons ui-grid-cellNav ui-grid-auto-resize ui-grid-resize-columns ui-grid-selection ng-style="{height: vm.totalHeight+'px'}"></div>