从数据API动态创建递归树视图的最佳方法

Dan*_*Dan 6 treeview angular2-services angular

我正在学习Angular 2,尝试从(可能非常大的)第三方API构建可扩展的树视图.API有一个这样的底层结构:

- Home (id: 1053)
- - Rugby League (id: 1054)
- - - Super League (id: 1103)
- - - - Castleford Tigers (id: 1111)
- - - - Catalans Dragons (id: 1110)
- - - - Huddersfield Giants (id: 1116)
- - - - Hull FC (id: 1108)
- - - Championship (id: 1104)
- - - - Batley Bulldogs (id: 1120)
- - - - Bradford Bulls (id: 1118)
- - - - Dewsbury Rams (id: 1124)
- - - - Featherstone Rovers (id: 1121)
- - Football (id: 1056)
- - - Premier League (id: 1057)
- - - - AFC Bournemouth (id: 1059)
- - - - etc
- - - - etc
Run Code Online (Sandbox Code Playgroud)

设置API以便传递id并返回该节点的子节点(仅)的简单JSON数组.所以,例如我调用:http://www.example.com/api/contentNodes/?parentId=1053它返回:

[
  {"Id":1054,"Name":"Rugby League","HasChildren":true},
  {"Id":1056,"Name":"Football","HasChildren":true}
]
Run Code Online (Sandbox Code Playgroud)

(HasChildren表示节点是否具有子节点.)

请注意,因为数据集最终会很大,所以我希望在打开树分支时逐步从API中"提取"更多数据,而不是将整个数据集转储到那里并在我的应用程序中呈现出来.

我已经设置了一个Angular 2应用程序,可以在这里看到:http://plnkr.co/edit/QQ1OKCbd4pDptpSVbWch?p = preview

关键组件是`app/content-list.component.ts'文件:

import {Component, OnInit}  from 'angular2/core';
import {ContentNode}        from './content-node';
import {ContentService}     from './content.service';

@Component({
    selector: 'content-list',
    template: `
        <ol class="tree">
            <li *ngFor="#contentNode of contentNodes" class="tree__branch" [ngClass]="{'tree__branch--has-children': contentNode.HasChildren}">
                <a *ngIf="contentNode.HasChildren" (click)="toggleBranch(contentNode.Id)" class="toggle">+</a> {{ contentNode.Name }}
            </li>
        </ol>
        <div class="error" *ngIf="errorMessage">{{errorMessage}}</div>
    `
})
export class ContentListComponent implements OnInit {

    constructor (private _contentService: ContentService) {}

    errorMessage: string;
    private _startNodeId: number = 1053;

    contentNodes: ContentNode[];

    ngOnInit() { 
        this.getContentNodes();
    }

    getContentNodes() {
        this._contentService.getContentNodes(this._startNodeId)
            .subscribe(
                contentNodes => this.contentNodes = contentNodes,
                error =>  this.errorMessage = <any>error
            );
    }

    toggleBranch(branchId:number){
        console.log('branchId: ' + branchId);
    }
}
Run Code Online (Sandbox Code Playgroud)

你会在这里看到我通过传递一个1053的parentId来调用我的服务,该服务返回上面的JSON.

我现在已经碰壁了,当+单击按钮时,能够逐步将树视图的子节点加载到嵌套的HTML列表(<ol>)中.

这里最好的方法是什么才能以一种非常巧妙的方式实现这一目标?

我的下一步将是确保应用程序不会进行过多的API调用,但我的直接关注只是让运行的树视图连接起来并正常工作.

我已经看过这个递归树视图的例子,但似乎(a)有点儿错误(因为<ol></ol>当子节点为空时在HTML中呈现空元素等); (b)它似乎是以一种非常"硬编码"的方式建立的,我没有足够的经验来自信地重构它.

非常感谢.

请注意,出于安全原因,我不能不公开地向公开请求打开API,这使得在Plunkr上进行测试有点困难,我意识到.目前,我的示例仅使用静态的单级JSON数据集.

S.K*_*ski 8

在Angular2中,您可以递归地呈现指令.这使得渲染树非常容易.我已经修改了你的Plunker,只是为了表明这一点.它不是理想的实现,但它按预期工作:).

例:

@Component({
    selector: 'tree-view',
    template: `
        <div *ngFor="#dir of dirs">
            <tree-view [dirs]="dir.dirs"></tree-view>
        <div>
    `,
    directives: [TreeView]
})
export class TreeView {
    @Input() 
    private dirs: Array<Directory>;
}
Run Code Online (Sandbox Code Playgroud)

我希望你会喜欢.

干杯!