Angular: tree view from json data

情到浓时终转凉″ 提交于 2021-01-28 09:21:46

问题


I am new to angular. I want to create a expandable tree in angular 5. I have tried to using Angular-tree-component and ngx-treeview. but the problem with them is they need a json in specif format. is there any library which reads a any type of json and creates a expandable tree ? or do I need to convert my json in the format this npm modules will need it ?

I want to do something like tree-grid-directive(http://khan4019.github.io/tree-grid-directive/test/treeGrid.html) is providing in angular js 1.


回答1:


The main point in a tree-view is recursive templates calling. For example:

    <!-- branch -->
    <ng-template let-items="items" #branch>
      <ng-container *ngFor="let item of items">
        <ng-container *ngTemplateOutlet="leaf; context: {item: item}"></ng-container>
      </ng-container>
    </ng-template>
    
    <!-- leaf -->
    <ng-template let-item="item" #leaf>
      <li>
        <span>{{item.content}}</span>
        <ul *ngIf="item.children">
          <ng-container *ngTemplateOutlet="branch; context: {items: item.children}"></ng-container>
        </ul>
      </li>
    </ng-template>

Here "branch" template, with help of *ngTemplateOutlet, refers "leaf" template and vice versa. So it allows to tree to "grow".

Entire, working source code and demo are here




回答2:


I solved my problem by using https://www.npmjs.com/package/angular4-jsoneditor. I am using it in view mode.



来源:https://stackoverflow.com/questions/49194073/angular-tree-view-from-json-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!