Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't collapse/uncollapse Material Tree (Angular 6)

I'm trying to set up Material Tree in my app. This was rather complicated compared to mat-table f.ex., but I've managed to to fetch data and show this. However, I'm just able to show all names, rather than expanding/collapsing the children of them. I get the error

cannot read property treeControl of undefined

when clicking on the items. I tried a suggestion from another SO-post, then I get an additional error of

cannot read property length of undefined

How can I resolve this? Here is the code I have now:

overview.component.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 matTreeNodeToggle [attr.aria-label]="'toggle ' + node.name">
          <mat-icon class="mat-icon-rtl-mirror">
            {{nestedTreeControl.isExpanded(node) ? 'expand_more' : 'chevron_right'}}
          </mat-icon>
        </button>
        {{node.name}}
      </div>
      <ul [class.example-tree-invisible]="!nestedTreeControl.isExpanded(node)">
        <ng-container matTreeNodeOutlet></ng-container>
      </ul>
    </li>
  </mat-nested-tree-node>
</mat-tree>

overview.component.scss

example-tree-invisible {display: none;}

.example-tree ul,
.example-tree li {
    margin-top: 0;
    margin-bottom: 0;
    list-style-type: none;
}

overview.module.ts

    import { NgModule } from '@angular/core';
    import { CommonModule } from '@angular/common';
    import { OverviewComponent } from './overview.component';
    import { MatTreeModule,
      MatIconModule,
      MatButtonModule } from '@angular/material';


    @NgModule({
      imports: [
        CommonModule,
        MatTreeModule,
        MatIconModule,
        MatButtonModule
      ],
      declarations: [OverviewComponent]
    })
    export class OverviewModule { }

overview.component.ts

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { MatTreeNestedDataSource } from '@angular/material';
import { NestedTreeControl } from '@angular/cdk/tree';
import { of as observableOf } from 'rxjs';

export class Group {
  name: string;
}
export class MasterGroup extends Group {
  childGroups: Array<Group>;
}

@Component({
  selector: 'app-overview',
  templateUrl: './overview.component.html',
  styleUrls: ['./overview.component.scss']
})
export class OverviewComponent implements OnInit {
  nestedTreeControl: NestedTreeControl<Group>;
  nestedDataSource: MatTreeNestedDataSource<MasterGroup>;



  constructor(private http: HttpClient) {
    this.nestedTreeControl = new NestedTreeControl<Group>(this._getChildren);
    this.nestedDataSource = new MatTreeNestedDataSource();

    this.getGroups();
   }

  ngOnInit() {
  }

  private _getChildren = (node: MasterGroup) => {
     return observableOf(node.childGroups);
  }

   getGroups() {
    const data = [
    {
      name: 'test',
      childGroups: [
        {
          name: 'inner test'
        },
        {
          name: 'inner test 2'
        }
      ]
    },
    {
      name: 'test 2',
      childGroups: [
        {
          name: 'another test'
        }
      ]
    },
    {
      name: 'test 3',
      childGroups: []
    }
  ];

    this.nestedDataSource.data = data;
   }

  hasNestedChild = (_: number, nodeData: MasterGroup) => {
    /*if (nodeData.childGroups) {
      // I get an error using .length here - without it shows all groups
      // When clicking item it gives error 'cannt read property treeControl of undefined'
      return true;
    } else {
      return false;
    } */

    // tried this line from another SO-post, I then get both errors at same time
    return nodeData.childGroups.length;
  }
}
like image 338
Lars Rødal Avatar asked Oct 15 '18 12:10

Lars Rødal


3 Answers

Adding another answer in case it helps someone. I also ran into this issue. I was using the tree in a library module and the module was then imported into my application. When I ran my application the tree worked fine but when I served up the module itself it didn't. I had to change the target in tsconfig.lib.json from es2015 to es5 for it to work.

like image 57
Pace Avatar answered Nov 17 '22 10:11

Pace


Okay, while I already had Angular 7 (manual upgrade), I had the same problem.

To solve it, I created a new Angular project (using ng new) and made sure that all package versions in package.json matched with those of my project. Then I replaced my tsconfig.json file with the one from the new project.

Then it worked. I suppose there were some dependencies, like @angular/cdk, had incompatibilities with my previous tsconfig.json.

I hope for anyone coming here, it helps, too.

like image 35
JFFIGK Avatar answered Nov 17 '22 09:11

JFFIGK


Seems the matTreeNodeToggle works stable only on mat-tree-node or mat-nested-tree-node tag level. For toggling from level below of these tags use (click)="nestedTreeControl.toggle(node)"

like image 37
Adrian Avatar answered Nov 17 '22 10:11

Adrian