简体   繁体   English

在 Angular 中打开新项目时折叠已经消耗的项目

[英]Collapse already expended item when new item is open in Angular

I have this much code to form a tree structure of sideBar Menu, and this is working fine.我有这么多代码来形成侧栏菜单的树结构,这工作正常。 But what i want is suppose a menu have its submenu and it is expanded.但我想要的是假设一个菜单有它的子菜单并且它被扩展了。 Once the user click on any other parent menu the expanded menu should collapsed.一旦用户单击任何其他父菜单,展开的菜单应折叠。 Can any please help me regarding this?任何人都可以帮我解决这个问题吗?

 <ng-container *ngTemplateOutlet="treeViewList; context: { $implicit: pageMenus }"></ng-container>
  <ng-template #treeViewList let-list>
    <ul>
      <li *ngFor="let item of list">
        <a (click)="item.isopen = !item.isopen">
          <div>
            <mat-icon>{{ item.menuIcon }}</mat-icon>
            <span class="icon-text">{{ item.menuName }}</span>
          </div>
          <i class="fa fa-angle-right" [ngClass]="{ clicked: item.isopen }" aria-hidden="true"></i>
        </a>
        <ul *ngIf="item.children && item.isopen">
          <ng-container *ngTemplateOutlet="treeViewList;context: { $implicit: item.children }">
          </ng-container>
        </ul>
      </li>
    </ul>
  </ng-template

payload of::有效载荷::

 pageMenus = [
    {
      "menuName": "Dashboard",
      "menuIcon": 'dashboard',
      "path": "",
      "children": [
        {
          "menuName": "Status",
          "menuIcon": "",
          "path": "/dashboard",
        }
      ]
    }, {
      "menuName": "Database",
      "menuIcon": 'layers',
      "path": "",
      "children": [
        {
          "menuName": "Users",
          "menuIcon": "",
          "path": "/user",
        },
        {
          "menuName": "Devices",
          "menuIcon": "",
          "path": "/device",
        },
        {
          "menuName": "Sessions",
          "menuIcon": "",
          "path": "/session",
        }
      ]
    }
]

在此处输入图片说明

Instead of using isOpen variable for each item, you can openIndex.您可以 openIndex,而不是为每个项目使用 isOpen 变量。 if itemIndex is equal to open index then only show child menu如果 itemIndex 等于 open index 则只显示子菜单

 <ng-container *ngTemplateOutlet="treeViewList; context: { $implicit: pageMenus }"></ng-container>
  <ng-template #treeViewList let-list>
    <ul>
      <li *ngFor="let item of list; let index = index">
        <a (click)="openIndex = openIndex === index ? -1 : index">
          <div>
            <mat-icon>{{ item.menuIcon }}</mat-icon>
            <span class="icon-text">{{ item.menuName }}</span>
          </div>
          <i class="fa fa-angle-right" [ngClass]="{ clicked: openIndex === index }" aria-hidden="true"></i>
        </a>
        <ul *ngIf="item.children && openIndex === index">
          <ng-container *ngTemplateOutlet="treeViewList;context: { $implicit: item.children }">
          </ng-container>
        </ul>
      </li>
    </ul>
  </ng-template

You will have to create a new variable openIndex in your component.您必须在组件中创建一个新变量 openIndex。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM