gpt4 book ai didi

angular - 使用数据 bool 值展开/折叠 Material 树(Angular 7)

转载 作者:行者123 更新时间:2023-12-04 01:51:07 24 4
gpt4 key购买 nike

我试图从它的数据源中获取一个使用 bool 值的 Material 树。我在输入时过滤 datasource.data,这会导致每次用户输入内容时树都会折叠。为了避免这种情况,我想在特定行中使用 bool 值来展开/折叠 Material 树。我已经用 Material 表成功地做到了这一点,但我无法让它与树一起工作。这是我到目前为止:

HTML 文件:

<mat-tree [dataSource]="nestedDataSource" [treeControl]="nestedTreeControl" class="example-tree">
<mat-tree-node *matTreeNodeDef="let node">
<li class="mat-tree-node">
<button mat-icon-button disabled></button>
{{node.name}}
</li>
</mat-tree-node>

<mat-nested-tree-node *matTreeNodeDef="let node; when: hasNestedChild">
<li>
<div class="mat-tree-node">
<button mat-icon-button
[attr.aria-label]="'toggle ' + node.name"
click="changeState(node)">
<mat-icon class="mat-icon-rtl-mirror">
{{node.expanded ? 'expand_more' : 'chevron_right'}}
</mat-icon>
</button>
{{node.name}} ({{node.expanded}})
</div>
<ul [class.example-tree-invisible]="node.expanded">
<ng-container matTreeNodeOutlet></ng-container>
</ul>
</li>
</mat-nested-tree-node>
</mat-tree>

TS 文件:
import {NestedTreeControl} from '@angular/cdk/tree';
import {Component, Injectable} from '@angular/core';
import {MatTreeNestedDataSource} from '@angular/material/tree';
import {BehaviorSubject} from 'rxjs';

/**
* Json node data with nested structure. Each node has a filename and a value or a list of children
*/
export class FileNode {
childGroups: FileNode[];
name: string;
expanded: boolean;
}

/**
* The Json tree data in string. The data could be parsed into Json object
*/
const TREE_DATA = [
{
'name': 'Group 1',
'expanded': false,
'childGroups': [
{
'name': 'Childgroup 1',
'expanded': false,
'childGroups': []
},
{
'name': 'Childgroup 2',
'expanded': false,
'childGroups': [
{
'name': 'Child of child',
'expanded': false,
'childGroups': []
}
]
}
]
},
{
'name': 'Group 2',
'expanded': false,
'childGroups': [
{
'name': 'Childgroup 1',
'expanded': false,
'childGroups': []
},
{
'name': 'Childgroup 2',
'expanded': false,
'childGroups': [
{
'name': 'Child of child',
'expanded': false,
'childGroups': []
}
]
}
]
},
{
'name': 'Group 3',
'expanded': false,
'childGroups': [
{
'name': 'Childgroup 1',
'expanded': false,
'childGroups': []
},
{
'name': 'Childgroup 2',
'expanded': false,
'childGroups': [
{
'name': 'Child of child',
'expanded': false,
'childGroups': []
}
]
}
]
},
]

@Component({
selector: 'tree-nested-overview-example',
templateUrl: 'tree-nested-overview-example.html',
styleUrls: ['tree-nested-overview-example.css']
})
export class TreeNestedOverviewExample {
nestedTreeControl: NestedTreeControl<FileNode>;
nestedDataSource: MatTreeNestedDataSource<FileNode>;

constructor() {
this.nestedTreeControl = new NestedTreeControl<FileNode>(this._getChildren);
this.nestedDataSource = new MatTreeNestedDataSource();
this.nestedDataSource.data = TREE_DATA;
}

hasNestedChild = (_: number, nodeData: FileNode) =>
nodeData.childGroups.length > 0;

private _getChildren = (node: FileNode) => node.childGroups;

changeState(node) {
console.log(node);
node.expanded = !node.expanded;
}
}

Stackblitz

最佳答案

来自 Angular documentation on User Input :

To bind to a DOM event, surround the DOM event name in parentheses and assign a quoted template statement to it.



您需要做的就是改变 点击 (点击) .

DEMO

关于angular - 使用数据 bool 值展开/折叠 Material 树(Angular 7),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53082684/

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