Search code examples
angularangular-materialtreeviewflatten

How do I access flattenedData when using MatTreeFlatDataSource


After upgrading to Angular Material - version 12 ("@angular/material": "^12.1.2"). It seems that I cannot access the _flattenData property anymore from MatTreeFlatDataSource. This part of code use to work before the upgrade:

    this.dataSource._flattenedData.value.forEach(item => {
  const issueHasThisItem =
    this.data.exitingItems.findIndex(
      (loopItem: Entity) =>
        (item.level === 1 ||
          this.data.treeModalType === TreeModalType.SOLUTION) &&
        loopItem.id === item.item.id
    ) !== -1;
  if (issueHasThisItem) {
    this.checklistSelection.select(item);
  }
});

Now, it seems that _flattenData is read only private and it does not work anymore (according to the latest commit: Angular Material link):

    Error: src/app/modules/issues/components/issue-impact/tree-modal/tree-modal.component.ts:151:21 - error TS2341: Property '_flattenedData' is private and only accessible within class 'MatTreeFlatDataSource<T, F, K>'.

151     this.dataSource._flattenedData.value.forEach((item) => {
                    ~~~~~~~~~~~~~~

I know that this is not the proper way on how to access the flatten data. Are there any other alternatives ?


Solution

  • You can access the nodes via treeControl:

      this.treeControl.dataNodes.forEach(item => {
    

    Source: https://stackoverflow.com/a/53545700/6025248