gpt4 book ai didi

javascript - 如何制作mat-tree组件Angular Material 6.0.1

转载 作者:太空狗 更新时间:2023-10-29 19:32:22 26 4
gpt4 key购买 nike

我正在尝试做一个基于 material angular mat-tree 的应用程序,但是当我运行这段代码时,它没有显示值,而且我没有收到任何错误,我该如何解决这个问题,帮助我继续前进

当我们打开应用程序时,我们需要显示类名

下面我添加了我的html和组件代码

<mat-nested-tree-node *matTreeNodeDef="let node; when: hasNestedChild">
<li>
<div class="mat-tree-node">
<button mat-icon-button matTreeNodeToggle
[attr.aria-label]="'toggle ' + node.filename">
<mat-icon class="mat-icon-rtl-mirror">
{{nestedTreeControl.isExpanded(node) ? 'expand_more' : 'chevron_right'}}
</mat-icon>
</button>

</div>
<ul [class.example-tree-invisible]="!nestedTreeControl.isExpanded(node)">
<ng-container matTreeNodeOutlet></ng-container>
</ul>
</li>
</mat-nested-tree-node>

组件:

const TREE_DATA = [
{
"class":"ece",
"count":60,
"students": [

{"name":"a","id":"11"},
{"name":"b","id":"12"},
{"name":"c","id":"13"},
{"name":"d","id":"14"}

]
},
{
"class":"mech",
"count":60,
"students": [

{"name":"r","id":"21"},
{"name":"e","id":"22"},
{"name":"w","id":"23"},
{"name":"q","id":"24"}

]
}
];


@Injectable()
export class FileDatabase {
dataChange = new BehaviorSubject<FileNode[]>([]);

get data(): FileNode[] { return this.dataChange.value; }

constructor() {
this.initialize();
}

initialize() {

const dataObject = TREE_DATA;


const data = this.buildFileTree(dataObject, 0);


this.dataChange.next(data);
}


buildFileTree(obj: object, level: number): FileNode[] {
return Object.keys(obj).reduce<FileNode[]>((accumulator, key) => {
const value = obj[key];
const node = new FileNode();
node.filename = key;

if (value != null) {
if (typeof value === 'object') {
node.children = this.buildFileTree(value, level + 1);
} else {
node.type = value;
}
}

return accumulator.concat(node);
}, []);
}
}

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

constructor(database: FileDatabase) {
this.nestedTreeControl = new NestedTreeControl<FileNode>(this._getChildren);
this.nestedDataSource = new MatTreeNestedDataSource();

database.dataChange.subscribe(data => this.nestedDataSource.data = data);
}

hasNestedChild = (_: number, nodeData: FileNode) => !nodeData.type;

private _getChildren = (node: FileNode) => node.children;
}

这是我的 demo

打开页面应该是这样的

enter image description here

当我点击 ece 时,它​​应该是这样的

enter image description here

最佳答案

添加了{{node :json}}下面的内容,你只需要从节点对象中获取正确的数据即可。

    <button mat-icon-button matTreeNodeToggle
[attr.aria-label]="'toggle ' + node.filename">
<mat-icon class="mat-icon-rtl-mirror">
{{nestedTreeControl.isExpanded(node) ? 'expand_more' : 'chevron_right'}}
</mat-icon>
{{node | json}}
</button>

关于javascript - 如何制作mat-tree组件Angular Material 6.0.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52578743/

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