Angular 5 Material多层轮播样式SideNav菜单(无下拉菜单)

我设法通过Angular Material做到了这一点,它与通过下拉菜单部署的子菜单一起使用,但是我正在寻找一种使它们不“下拉菜单”的方法,我希望它们像一个朝着正确的方向过渡,是这样的:

https://jmouriz.github.io/angular-material-multilevel-menu/demo/demo.html#!/demo/views/item-2

但这是在AngularJS 1.X中完成的。我正在使用Angular 5,是否有人提出任何方法或从哪里开始?谢谢!

元帅

在进一步研究之后,我感到原始的解决方案过于复杂,因此必须有更好的方法。

  • 最初的方法主要是从视图的角度来控制层次结构。
  • 这是错误的做法

修改后的方法基于基于数组层次结构驱动视图的概念……这使解决方案更干净,可伸缩性更高。


定义数组层次结构...这里的关键是将带有子菜单的选项分配给rootTab索引,这些索引将在单击时保留子菜单选项。

rootTabs = [
    {
      id: 0,
      options: [
        { option: 'Home' },
        { option: 'Parámetros' },
        { option: 'Operativa' },
        { option: 'Productos' },
        { option: 'Invocación Servicios', rootTab: 1 }
      ]
    },
    {
      id: 1,
      options: [
        { option: 'Portal 1', rootTab: 2 },
        { option: 'Portal 2', rootTab: 4 },
        { option: 'Portal 3', rootTab: 5 }
      ]
    },
    {
      id: 2,
      options: [
        { option: 'Service 1 Item1', route: '/Submenu1' },
        { option: 'Service 1 Item2', route: '/Submenu2' },
        { option: 'Service 1 Item3', route: '/Submenu3' },
        { option: 'Service 1 Item4', route: '/Submenu4' },
        { option: 'Service 1 Item5', route: '/Submenu5' },
        { option: 'Invocación Servicios', rootTab: 3 }
      ]
    },
    {
      id: 3, options: [
        { option: 'put additional options here' }
      ]
    },
    {
      id: 4,
      options: [
        { option: 'Service 2 Item1', route: '/Submenu1' },
        { option: 'Service 2 Item2', route: '/Submenu2' },
        { option: 'Service 2 Item3', route: '/Submenu3' },
        { option: 'Service 2 Item4', route: '/Submenu4' },
        { option: 'Service 2 Item5', route: '/Submenu5' },
      ]
    },
    {
      id: 5,
      options: [
        { option: 'Service 3 Item1', route: '/Submenu1' },
        { option: 'Service 3 Item2', route: '/Submenu2' },
        { option: 'Service 3 Item3', route: '/Submenu3' },
        { option: 'Service 3 Item4', route: '/Submenu4' },
        { option: 'Service 3 Item5', route: '/Submenu5' },
      ]
    }
  ]

创建组件方法以通过UI交互处理菜单状态。

rootSelected(optionIndex, optionRootIndex, rootOption) {
    this.numItemsSelected++;
    this.previousRootTab = this.currentSelectedRootTab;
    this.currentSelectedRootTab = optionRootIndex;
    this.indexClicked = optionIndex;
    if (!this.breadcrumb1) {
      this.breadcrumb1 = rootOption
    } else if (!this.breadcrumb2) {
      this.breadcrumb2 = rootOption
    } else {
      this.breadcrumb3 = rootOption
    }
  }

  back2Main() {
    this.currentSelectedRootTab = 0;
    this.previousRootTab = 0;
    this.indexClicked = 0;

    this.numItemsSelected = 0;
    this.breadcrumb1 = null;
    this.breadcrumb2 = null;
    this.breadcrumb3 = null;
  }

  toBreadcrum1() {
    if (this.numItemsSelected > 1) {
      this.currentSelectedRootTab = this.breadcrumb1.rootTab;
      this.breadcrumb2 = null;
      this.numItemsSelected--
    }
  }

  toBreadcrum2() {
    if (this.numItemsSelected > 2) {
      this.currentSelectedRootTab = this.breadcrumb2.rootTab;
      this.breadcrumb3 = null;
      this.numItemsSelected--
    }
  }

将其全部连接到模板中

<div style="display: flex;flex-direction: row;margin:1% ;height:30px;">
    <div routerLink="/" routerLinkActive="active" (click)="back2Main()" style="cursor:pointer" class="vertical-align-text">Main</div>
    <div *ngIf="numItemsSelected >= 1">&nbsp;<mat-icon>chevron_right</mat-icon></div>
    <span *ngIf="numItemsSelected >= 1" routerLink="/" routerLinkActive="active" class="vertical-align-text" style="cursor:pointer;vertical-align: middle;" (click)="toBreadcrum1();">&nbsp;{{breadcrumb1.option}}</span>
    <div *ngIf="numItemsSelected >= 2">&nbsp;<mat-icon>chevron_right</mat-icon></div>
   <span *ngIf="numItemsSelected >= 2" routerLink="/" routerLinkActive="active" class="vertical-align-text" style="cursor:pointer" (click)="toBreadcrum2();">&nbsp;{{breadcrumb2.option}}</span>
    <div *ngIf="numItemsSelected >= 3">&nbsp;<mat-icon>chevron_right</mat-icon></div>
   <span *ngIf="numItemsSelected >= 3" routerLink="/" routerLinkActive="active" class="vertical-align-text" style="cursor:pointer" (click)="toBreadcrum3();">&nbsp;{{breadcrumb3.option}}</span>
   </div>


<div style="display: flex;flex-direction: row;background-color:white; height:100vh">
    <mat-tab-group class="navigation-tabs" [selectedIndex]="currentSelectedRootTab" dynamicHeight style="width:25vw; background-color:lightgray">
        <mat-tab *ngFor="let rootTab of rootTabs; let rootIndex = index" [label]="rootIndex">
            <mat-nav-list>
        <div *ngFor="let rootOption of rootTab.options; let optionIndex = index;">       
          <a mat-list-item *ngIf="!rootOption.rootTab" [routerLink]="rootOption.route" routerLinkActive="active">{{rootOption.option}}</a>
          <a mat-list-item style="width:100%" *ngIf="rootOption.rootTab" (click)="rootSelected(optionIndex, rootOption.rootTab, rootOption)">{{rootOption.option}}<div><mat-icon style="padding-left:20%;vertical-align: middle;">chevron_right</mat-icon></div></a>
        </div> 
            </mat-nav-list>
        </mat-tab>
    </mat-tab-group>
  <div style="margin:auto">
    <router-outlet></router-outlet> 
  </div>
</div>

Stackblitz

https://stackblitz.com/edit/angular-x5xefi-gmzy46?embed=1&file=app/tab-group-basic-example.html

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Angular 5 Material如何将下拉菜单添加到多层Carousel样式SideNav?

Angular 5下拉菜单

Angular 5 Material多重菜单

在angular5中带有文本框的下拉菜单

Material Angular 4.制作自动下拉菜单

在Angular Material的下拉菜单中删除部分文本

将Angular Material下拉菜单移到左侧

下拉菜单过渡 Bootstrap 5

下拉菜单过渡 Bootstrap 5

来自Angular 5中json对象的动态嵌套Material菜单

Angular 4 嵌套下拉菜单

Angular下拉菜单-反应格式页面

Angular 4的语义UI下拉菜单

Angular 6的Bootstrap下拉菜单

Angular 7响应式下拉菜单

Angular 自动分配下拉菜单

Angular 4-带有子菜单下拉菜单的下拉菜单(多级)

Angular Material-md-select-下拉菜单中的选定项目

无法为 Angular Material 中的下拉菜单设置默认值

带有分层缩进的 Angular 2 Material Design 多选下拉菜单

如何在Angular Material中创建手风琴下拉菜单?

如何在 CSS flexbox 中使用 Angular 的 material2 下拉菜单?

从Angular Material的下拉菜单中为每个选项显示不同的彩色内容详细信息

Bootstrap 5 with Angular 12:通过 JavaScript 获取/创建引导程序实例后,引导程序(导航栏、下拉菜单)交互被阻止

下拉菜单中的angular6下拉菜单不起作用

如何使Angular Reactive Formarray中的级联下拉菜单工作而不会弄乱下拉菜单值

Angular-material如何建立控制md内容的sidenav菜单?

Angular Material:sidenav折叠时显示菜单图标

动态选择下拉菜单Rails 5 / AJAX