Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mat-tree displaying child value integers as nodes #16815

Open
JohnTranstel opened this issue Aug 19, 2019 · 5 comments
Open

mat-tree displaying child value integers as nodes #16815

JohnTranstel opened this issue Aug 19, 2019 · 5 comments
Labels
area: material/tree P4 A relatively minor issue that is not relevant to core functions troubleshooting This issue is not reporting an issue, but just asking for help

Comments

@JohnTranstel
Copy link

I am new to this so please say if this is better posted elsewhere.

I am building a mat-tree on the fly by creating a json string.
The string is:
" {"Politics":[],"Sport":[{"footie":[{"uefa":[]}]},{"sportsub":[]}],"Weather":[{"newdir":[]},{"weathersub":[]}"
It works fine but displays the child level value as an extra level, (see image) how do I lose this?

Code is

private populateTree(workdata) {

var dataObject = JSON.parse(workdata);
// Build the tree nodes from Json object. The result is a list of FileNode with nested
// file node as children.
const data = this.buildFileTree(dataObject, 0);
// Notify the change.
this.dataChange.next(data);
}

buildFileTree(obj: { [key: string]: any }, level: number, parentId: string = '0'): FileNode[] {
return Object.keys(obj).reduce<FileNode[]>((accumulator, key, idx) => {
const value = obj[key];
const node = new FileNode();
node.filename = key;
node.id = ${parentId}/${idx};

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

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

}

mat-tree

@JohnTranstel JohnTranstel added the troubleshooting This issue is not reporting an issue, but just asking for help label Aug 19, 2019
@andrewseguin
Copy link
Contributor

Can you create a reproduction of this on StackBlitz to help save us time?

@JohnTranstel
Copy link
Author

JohnTranstel commented Aug 19, 2019 via email

@andrewseguin andrewseguin added the P4 A relatively minor issue that is not relevant to core functions label Aug 19, 2019
@JohnTranstel
Copy link
Author

I copied most of the relevant code from an existing stackblitz.
https://stackblitz.com/edit/mat-tree-with-drag-and-drop?file=app%2Ftree-flat-overview-example.ts

If you comment out line 87 then add the following 2 lines
var test = ' {"Politics":[],"Sport":[{"footie":[{"eufa":[]}]},{"sportsub":[]}],"Weather":[{"newdir":[]},{"weathersub":[]}] }';
var dataObject = JSON.parse(test);

you will see the result.

@JohnTranstel
Copy link
Author

Looking at the stackblitz example and seeing how the tree behaves with the existing json object, I thing the issue is simply " why is the json string not correctly mapping to the equivalent json object? What should the string look like to do this?"

@walvekarnikhil
Copy link
Contributor

There is a problem with the buildFileTree function. The expected Node structure doesn't match with what you have provided.
Here is original structure:

{
  Applications: {
    Calendar: 'app',
    Chrome: 'app',
    Webstorm: 'app'
  },
  Documents: {
    angular: {
      src: {
        compiler: 'ts',
        core: 'ts'
      }
    },
    material2: {
      src: {
        button: 'ts',
        checkbox: 'ts',
        input: 'ts'
      }
    }
  },
  Downloads: {
    October: 'pdf',
    November: 'pdf',
    Tutorial: 'html'
  },
  Pictures: {
    'Photo Booth Library': {
      Contents: 'dir',
      Pictures: 'dir'
    },
    Sun: 'png',
    Woods: 'jpg'
  }
}

Here is what you have provided:

{
  "Politics": [],
  "Sport": [
    {
      "footie": [
        {
          "eufa": []
        }
      ]
    },
    {
      "sportsub": []
    }
  ],
  "Weather": [
    {
      "newdir": []
    },
    {
      "weathersub": []
    }
  ]
}

In the expected structure, there are no arrays it is always {[key: string]: any}. Since the buildFileTree function uses Object.keys for the array it will use the index as the key.
You will have to modify the build function as per the input & output structure.

Here is modified stackblitz: https://stackblitz.com/edit/mat-tree-with-drag-and-drop-vgwpp1?file=app%2Ftree-flat-overview-example.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: material/tree P4 A relatively minor issue that is not relevant to core functions troubleshooting This issue is not reporting an issue, but just asking for help
Projects
None yet
Development

No branches or pull requests

3 participants