gpt4 book ai didi

Angular Material 扁平树父子图形表示

转载 作者:行者123 更新时间:2023-12-02 15:30:06 25 4
gpt4 key购买 nike

我想在 Angular Material Flat Tree 上显示父子图形表示。

这是设计:

enter image description here

Here is the DEMO what i done so far.

最佳答案

第一个解决方案

这是stackBlitz Demo

这是stackBlitz editable link

我使用treeControl.dataNodes.indexOf(node)对每个可见节点进行递归调用,引用节点索引

这是递归函数:

  recNode( arr:any[], data:any[], index:number, maxIndex:number ):any[] {

if ( arr === undefined )
arr = [];

for ( let i = 0; i < data.length; i++ ) {

index++

if ( index === maxIndex ) return ( [ true, index, arr ] );
if ( data[i].children.length ) {

let res = this.recNode( arr, data[i].children, index, maxIndex )
index = res[1];

if ( res[0] === true ) {

arr.splice( 0, 0, ( i !== ( data.length - 1 ) ) )
return ( [ true, index, arr ] );

}

}

}

return ( [ false, index, arr ] );

}

这会返回 [ngClass]true false 值数组

然后在 html 中我这样调用该函数

<li *ngFor="let r of recNode( undefined, dataSource.data, -1, treeControl.dataNodes.indexOf( node ) )[2]; [ngClass]="{'node-arrow': r, 'node-arrow-empty': !r}" ></li>

最后我在 css 中创建了另一个名为 node-arrow-empty 无边框的类

   .node-arrow-nox {
position: relative;
// min-width: 48px;
// min-height: 48px;
.node-arrow {
position: relative;
min-width: 48px;
min-height: 48px;
&:before {
content: "";
position: absolute;
top: -20px;
left: 20px;
border-left: 1px solid $copounsCount;
bottom: 0;
}
}
.node-arrow-empty {
position: relative;
min-width: 48px;
min-height: 48px;
&:before {
content: "";
position: absolute;
top: -20px;
left: 20px;
bottom: 0;
}
}
}

根据数组中的值是true还是false,它会在两个类之间切换html。

第二种解决方案

stackBlitz DEMO

stackBlitz editable link

另一个解决方案是通过添加例如 isLast:boolean 属性来跟踪数组中的最后一个元素。当然,如果您正在处理的数据是动态的,您需要找到一种方法来动态更改该值。

一旦填充了isLast,您将进行递归调用,它将找到当前节点级别 - 1 的前一个同级。话虽如此,您将检查两件事。第一件事是,节点类包含 n-last (这是我给 Html 的类名,表示父数组的最后一个元素),第二件事是检索类 n-{{node -级别}}

通过递归(如果您不想,可以使用非递归方法)检索具有类 n-3n-2 的 previousElementSibling ...到 n-0 您可以检查每个元素是否包含 n-last 类。您将 truefalse 插入数组,就像第一个解决方案在类 node-arrownode-arrow- 之间切换一样空

这是递归函数

  dummy( el:any, arr:any[], level:number ) {

let classes = el.className;

if ( arr === undefined )
arr = [];
else {

arr.splice( 0, 0, classes.includes( 'n-last' ) );

}

let np = /(?!n-)\d+/g;

let m = classes.match( np );
let nLevel = parseInt( m[0] );
nLevel--;

if ( nLevel < 0 ) return ( arr );

while ( parseInt( el.className.match( np )[0] ) !== nLevel ) {

el = el.previousElementSibling;

}

arr = this.dummy(el, arr, nLevel);

return (arr);

}

HTML(这里只是父节点,子节点也是如此)

<mat-tree-node #refP   class="parent-node {{'n-' + node.level}} " *matTreeNodeDef="let node; when: grpNode;" matTreeNodePadding matTreeNodePaddingIndent="0" cdkDrag [cdkDragData]="node" [ngClass]="{'no-child': !node.children.length, 'n-last': node.isLast}">
<span class="node-arrow-nox d-flex">
<li [ngClass]="{'node-arrow': !r , 'node-arrow-empty': r}" *ngFor="let r of dummy(refP, undefined, node.level)"></li>
</span>
<button class="node-toggle" mat-icon-button matTreeNodeToggle [attr.aria-label]="'toggle ' + node.filename" [disabled]="!node.children.length">
<mat-icon class="mat-icon-rtl-mirror toggle-arrow">
{{treeControl.isExpanded(node) ? 'play_arrow' : 'play_arrow'}}
</mat-icon>
</button>
<div class="node-icon icon-h-arw"></div>
<div class="node-name">{{node.isLast}}-{{node.accntCode}} <span>: :</span> {{node.name}} </div>
</mat-tree-node>

如您所见,我使用名为 #refP 的模板变量将元素传递给虚拟函数

这里是 PARENTNODECHILDNODE 声明:

export class PARENTNODE {
id: string;
isLast: boolean;
...
actAsSupplier: boolean;
}

/* Flat node with expandable and level information */
export class CHILDNODE {
constructor(
public id: string,
public isLast: boolean,
...
public actAsSupplier: boolean,
){}
}

第三种解决方案

我不会编写代码,但我会尝试解释它。

这与之前的解决方案类似,只是您可以在树更改(交换、删除、添加)数据时解析并添加一个属性,例如用 true 填充的 divType false 并在 html 中这样调用它

 <li *ngFor="let r of node.divType" [ngClass]="{'node-arrow': r, 'node-arrow-empty': !r}" ></li>

关于 Angular Material 扁平树父子图形表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58184776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com