如何使用angular 6&bootstrap 3.3.7创建具有复选框列表的可折叠/可扩展/树结构

Mad*_*pop 7 javascript twitter-bootstrap typescript twitter-bootstrap-3 angular

在这里,我正在尝试可折叠/树结构与父和子的复选框,但我不能完全创建它我能够创建直到从json无序列表

{
  "properties": {
    "host": {
      "fields": {
        "keyword": {
          "ignore_above": 256,
          "type": "keyword"
        }
      },
      "type": "text",
      "fielddata": true
    },
    "information": {
      "properties": {
        "filetype": {
          "fields": {
            "keyword": {
              "ignore_above": 256,
              "type": "keyword"
            }
          },
          "type": "text",
          "fielddata": true
        },
        "author": {
          "fields": {
            "keyword": {
              "ignore_above": 256,
              "type": "keyword"
            }
          },
          "type": "text",
          "fielddata": true
        },
        "authorcountry": {
          "fields": {
            "keyword": {
              "ignore_above": 256,
              "type": "keyword"
            }
          },
          "type": "text",
          "fielddata": true
        }
      }
    },
    "url": {
      "fields": {
        "keyword": {
          "ignore_above": 256,
          "type": "keyword"
        }
      },
      "type": "text",
      "fielddata": true
    },
    "name": {
      "fields": {
        "keyword": {
          "ignore_above": 256,
          "type": "keyword"
        }
      },
      "type": "text",
      "fielddata": true
    },
    "instrument": {
      "properties": {
        "Style": {
          "fields": {
            "keyword": {
              "ignore_above": 256,
              "type": "keyword"
            }
          },
          "type": "text",
          "fielddata": true
        },
        "instrumentCode": {
          "type": "integer"
        },
        "instrumentName": {
          "type": "text"
        },
        "instrumentNumber": {
          "fields": {
            "keyword": {
              "ignore_above": 256,
              "type": "keyword"
            }
          },
          "type": "text",
          "fielddata": true
        }

      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

.html代码

<button class="btn btn-primary" (click)="getData()">getData</button>


<h1>ul element</h1>

<hr>

 <ul class="list-group"   *ngFor="let x of inf | keyvalue">
    <li class="list-group-item">{{x.key}}</li>
    <ng-container *ngIf="x.value.hasOwnProperty('properties')">
      <ul *ngFor="let y of x.value.properties | keyvalue">
      <li>
        {{y.key}}
      </li>
      </ul>  
    </ng-container> 
  </ul>
Run Code Online (Sandbox Code Playgroud)

可折叠/树结构

下面是我的stackblitz链接

https://stackblitz.com/edit/angular-k5tdpe

我也可以尝试插件,但插件的输入数据格式是不同的angular2-tree插件&ng2 -tree/ngx-tree所以任何建议

树的结构

Eli*_*seo 7

只需添加输入类型检查并使用[(ngModel)]

<ul class="list-group"   *ngFor="let x of inf | keyvalue">
    <li class="list-group-item">
     <!--add a input type checkbox and relation with x.check-->
     <input type="checkbox" [(ngModel)]="x.check">
     {{x.key}}</li>
    <!---change the *ngIf to add the x.check condition-->
    <ng-container *ngIf="x.check && x.value.hasOwnProperty('properties')">
      <ul *ngFor="let y of x.value.properties | keyvalue">
      <li>
        {{y.key}}
      </li>
      </ul>  
    </ng-container> 
  </ul>
Run Code Online (Sandbox Code Playgroud)

如果你想要一个"递归组件",它就更新了.我举一个例子,你可以在 stackblitz中看到结果

基本上"递归组件是模板中具有相同组件的组件.通常我们使用具有子属性的json模型(是的,你可以在一些具有子属性的属性中转换你的"复杂"json)如果有一次你创建了一个有孩子的jsondata,你的json就像,比如

data = [{
    title: "uno", children: [
      { title: "uno-uno" }]
  },
  {
    title: "dos", children: [
      { title: "dos-uno",children: [
           { title: "dos-uno" }
           ]},
      { title: "dos-dos" }]
  }
  ]
Run Code Online (Sandbox Code Playgroud)

我们可以像app.component一样

  <li *ngFor="let item of data">
     <app-li [title]="item.title" [children]="item.children"></app-li>
  </li>
Run Code Online (Sandbox Code Playgroud)

我们的应用程序就像

<li (click)="check=!check">
      <div [className]="children?check?'arrow-down':'arrow-up':'arrow-right'"></div>
      {{title}}
</li>
<ul *ngIf="children && check">
  <ng-container *ngFor="let item of children">
       <app-li [title]="item.title" [children]="item.children"></app-li>
  </ng-container>
</ul>
Run Code Online (Sandbox Code Playgroud)

看到我们用"孩子"喂app-li

注意:我添加一个带有className的<div>来"模拟"三角形

更新2 我们可以通过自己的项目itselft

组件变得像

@Component({
  selector: 'app-li',
  template: `<li >
              <div (click)="check=!check" [className]="item.children?
                   check?'arrow-down':'arrow-up':'arrow-right'">
              </div>
              <input type="checkbox" [(ngModel)]="item.select" >
              <span (click)="check=!check">{{item.title}}</span>
              </li>
             <ul *ngIf="item.children && check">
             <ng-container *ngFor="let item of item.children">
               <app-li [item]="item" ></app-li>
               </ng-container>
             </ul>
  `,
    styleUrls: [ './hello.component.css' ]



})
export class HelloComponent  {
  @Input() item: any;
}
Run Code Online (Sandbox Code Playgroud)

和应用程序

<li *ngFor="let item of data">
     <app-li [item]="item" ></app-li>
</li>
Run Code Online (Sandbox Code Playgroud)

请参阅stackblitz分叉