angular - 分层 TreeView 使用 Angular 8 中的 Ul li 展开折叠

标签 angular treeview html-lists

我是 Angular 新手,我有这么多代码来通过递归调用来表示 TreeView 中的分层 json 数据。此代码工作正常,但我想使用 CARET 图标展开和折叠 treeView,但我没有找到任何方法.

<h1>Angular 2 Recursive List</h1>
<ul>
  <ng-template #recursiveList let-list>
    <li *ngFor="let item of list">
      {{item.name}}
      <ul *ngIf="item.children.length > 0">  <!-- item.subnodes.length -->
        <ng-container *ngTemplateOutlet="recursiveList; context:{ $implicit: item.children }"></ng-container>
      </ul>
    </li>
  </ng-template>
  <ng-container *ngTemplateOutlet="recursiveList; context:{ $implicit: menuList}"></ng-container>
</ul>

“menuList”的有效负载

[
  {
    "menuId": 1,
    "menuName": "Inspection",
    "icon": "",
    "type": 1,
    "formId": 0,
    "formUUID": null,
    "formName": null,
    "menu": [
      {
        "menuId": 17,
        "menuName": null,
        "icon": "",
        "type": 2,
        "formId": 0,
        "formUUID": "2f84a801-cc3e-4807-a68c-cdd3cc9df9af",
        "formName": "Production Line",
        "menu": null
      }
    ]
  }
]

最佳答案

<h1>Angular 2 Recursive List</h1>
<ul>
  <ng-template #recursiveList let-list>
    <li *ngFor="let item of list">
      {{item.name}}
      <!--add a button-->
      <button (click)="item.isopen=!item.isopen">click</button>
      <!--change the condition-->
      <ul *ngIf="item.children.length > 0 && item.isopen">  
        <ng-container *ngTemplateOutlet="recursiveList; context:{ $implicit: item.children }"></ng-container>
      </ul>
    </li>
  </ng-template>
  <ng-container *ngTemplateOutlet="recursiveList; context:{ $implicit: menuList}"></ng-container>
</ul>

<!--just for check values-->
<pre>{{menuList|json}}</pre>

关于angular - 分层 TreeView 使用 Angular 8 中的 Ul li 展开折叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59681311/

相关文章:

c# - 为什么 Windows.Forms.TreeView 没有 SelectedNodeChanged 事件?

php - 动态加载 <ul> 并排序为 5 行的列

angular - 访问 Angular 中其他组件的变量

angular - 将 Angular2 中的数据传递给子组件

python - 从项目 Ttk TreeView 中删除标签

javascript - 如何在表单中拖放 HTML 列表项并更改其隐藏输入字段值

html - 溢出: hidden do for ul tag?是什么意思

javascript - Angular 5 - 如何重定向到具有特定 header 的外部 URL?

javascript - IE11 上纯 JavaScript 中的 Angular

javascript - Kendo Treeview 复选框按 ID 检查