Angular:来自 json 数据的 TreeView

标签 angular angular5

我是 Angular 的新手。我想创建一个 Angular 为 5 的可扩展树。我尝试使用 Angular-tree-component 和 ngx-treeview。但他们的问题是他们需要特定格式的 json。是否有任何库可以读取任何类型的 json 并创建可扩展树?还是我需要将我的 json 转换为 npm 模块需要的格式?

我想做一些像 tree-grid-directive( http://khan4019.github.io/tree-grid-directive/test/treeGrid.html ) 在 angular js 1 中提供的东西。

最佳答案

TreeView 中的要点是递归模板调用。例如:

    <!-- 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>

这里的“分支”模板在 *ngTemplateOutlet 的帮助下指的是“叶”模板,反之亦然。因此它允许树“生长”。

完整的工作源代码和演示是 here

关于Angular:来自 json 数据的 TreeView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49194073/

相关文章:

Angular 辅助路由 - 无法在同一个导航调用中导航和清除辅助路由?

javascript - 如何使用json_to_sheet提取xlxs中的json数据

angular - 根路径未正确渲染 Angular Universal

javascript - 如何在 Angular 2 的 DatePipe 中设置语言环境?

angular - 错误:这可能不是npm的问题。上面可能还有其他日志记录输出

javascript - 共享服务示例 Angular 5

java - 为什么我在尝试从 angular5 向 spring-boot 发送 PUT 请求时收到未经授权的消息?

javascript - Angular 5 : setting (click) value from an object not working

angular6 - 如何在 ng-select 中搜索多个字段?

angular - 在公共(public)存储库中公开 Okta clientId 是否安全?