angular / angular/components

mat-tree displaying child value integers as nodes

Open
#16,815 5 comments 0 reactions 0 assignees View on GitHub
area: material/tree P4 troubleshooting
Dominant language
TypeScript
Stars
25k
Forks
6.8k
Avg merge
1d 8h
Merged PRs (30d)
91

Description

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((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](https://user-images.githubusercontent.com/17275047/63251797-5df06880-c266-11e9-809a-58b753581fe9.png)

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the JSON in populateTree and trace buildFileTree, especially how object values become children and non-object values become node.type. Done means the intended mat-tree hierarchy renders without the extra child-value node; the issue does not name a repository file or test to run.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, typescript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.