У меня работает функционал, но когда я переключаю значение промежуточного значения, я получаю эту ошибку.Не уверен, что я изменяю 2 наблюдаемых, так что мне нравится, какое значение я должен принять.ИДК лол!Я добавил свой код внизу.Если вам нужно что-то еще, просто дайте мне знать.Любые идеи о том, как решить эту проблему?Человек мат компонент дерева так раздражает!
import { NestedTreeControl } from '@angular/cdk/tree';
import { Component, ChangeDetectorRef } from '@angular/core';
import { MatTreeNestedDataSource } from '@angular/material/tree';
import { SelectionModel } from '@angular/cdk/collections';
interface ITreeNode {
children?: ITreeNode[];
name: string;
expanded: boolean;
}
const TREE_DATA = [
{
name: 'Land Plane',
expanded: true,
children: [
{ name: 'Piston', expanded: true, children: [] },
{ name: 'Jet', expanded: true, children: [] },
{ name: 'Turboprop', expanded: true, children: [] }
]
},
{
name: 'Helicopter',
expanded: true,
children: [
{ name: 'Piston', expanded: true, children: [] },
{ name: 'Turboprop', expanded: true, children: [] }
]
},
{
name: 'Amphibian',
expanded: true,
children: [{ name: 'Turboprop', expanded: true, children: [] }]
},
{
name: 'Tiltwing',
expanded: true,
children: [{ name: 'Turboprop', expanded: true, children: [] }]
},
{
name: 'Gyrocopter',
expanded: true,
children: [{ name: 'Piston', expanded: true, children: [] }]
},
{
name: 'Tower',
expanded: true,
children: []
},
{
name: 'Gyrocopter',
expanded: true,
children: []
}
];
@Component({
selector: 'globe-source-facets',
templateUrl: './globe-source-facets.component.html',
styleUrls: ['./globe-source-facets.component.scss']
})
export class GlobeSourceFacetsComponent {
public nestedTreeControl: NestedTreeControl<ITreeNode>;
public nestedDataSource: MatTreeNestedDataSource<ITreeNode>;
public checklistSelection = new SelectionModel<ITreeNode>(true);
constructor(private changeDetectorRef: ChangeDetectorRef) {
this.nestedTreeControl = new NestedTreeControl<ITreeNode>(
this.getChildren
);
this.nestedDataSource = new MatTreeNestedDataSource();
this.nestedDataSource.data = TREE_DATA;
}
public hasNestedChild = (_: number, nodeData: ITreeNode) =>
nodeData.children.length > 0;
public getChildren = (node: ITreeNode) => node.children;
public changeState(node) {
node.expanded = !node.expanded;
}
descendantsAllSelected(node: ITreeNode): boolean {
const descendants = this.nestedTreeControl.getDescendants(node);
if (!descendants.length) {
return this.checklistSelection.isSelected(node);
}
const selected = this.checklistSelection.isSelected(node);
const allSelected = descendants.every(child => this.checklistSelection.isSelected(child));
if (!selected && allSelected) {
this.checklistSelection.select(node);
this.changeDetectorRef.markForCheck();
}
return allSelected;
}
public descendantsPartiallySelected(node: ITreeNode): boolean {
const descendants = this.nestedTreeControl.getDescendants(node);
if (!descendants.length) {
return false;
}
const result = descendants.some(child => this.checklistSelection.isSelected(child));
return result && !this.descendantsAllSelected(node);
}
public todoItemSelectionToggle(node: ITreeNode): void {
this.checklistSelection.toggle(node);
const descendants = this.nestedTreeControl.getDescendants(node);
this.checklistSelection.isSelected(node)
? this.checklistSelection.select(...descendants)
: this.checklistSelection.deselect(...descendants);
}
}
<div class="facets-container">
<div class="tree-container">
<mat-tree
[dataSource]="nestedDataSource"
[treeControl]="nestedTreeControl"
class="example-tree"
>
<mat-tree-node *matTreeNodeDef="let node" disabled="true">
<li class="mat-tree-node">
<button mat-icon-button disabled></button>
<mat-checkbox
class="checklist-leaf-node"
[checked]="checklistSelection.isSelected(node)"
(change)="todoItemSelectionToggle(node)"
>{{ node.name }}</mat-checkbox
>
</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
? 'chevron_right'
: 'expand_more'
}}
</mat-icon>
</button>
<mat-checkbox
*ngIf="node.name !== ''"
class="checklist-leaf-node"
[checked]="checklistSelection.isSelected(node)"
[indeterminate]="descendantsPartiallySelected(node)"
(change)="todoItemSelectionToggle(node)"
>{{ node.name }}</mat-checkbox
>
</div>
<ul [class.example-tree-invisible]="node.expanded">
<ng-container matTreeNodeOutlet></ng-container>
</ul>
</li>
</mat-nested-tree-node>
</mat-tree>
</div>
<div class="facet-actions">
<button mat-button>CLEAR</button>
<button mat-button color="primary">APPLY</button>
</div>
</div>