编程语言
首页 > 编程语言> > javascript-角度折叠式手风琴

javascript-角度折叠式手风琴

作者:互联网

我正在制作手风琴以在角度应用程序中使用JavaScript制作可折叠的div.

为此,如果其未打开,请单击“父代之一”或任何其他父代名称上的按钮.

HTML:

<div *ngFor="let item of data">
  <button class="accordion"> {{item.parentName}} </button>
  <div class="panel" *ngFor="let child of item.childProperties">
  <p> {{child.propertyName}} </p>
</div>
</div>

TS:

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  data: any =
    [
      {
        "parentName": "Parent One",
        "childProperties":
          [
            { "propertyName": "Property One" },
            { "propertyName": "Property Two" }
          ]
      },
      {
        "parentName": "Parent Two",
        "childProperties":
          [
            { "propertyName": "Property Three" },
            { "propertyName": "Property Four" },
            { "propertyName": "Property Five" },
          ]
      },
      {
        "parentName": "Parent Three",
        "childProperties":
          [
            { "propertyName": "Property Six" },
            { "propertyName": "Property Seven" },
            { "propertyName": "Property Eight" },
          ]
      }
    ]

  ngOnInit() {
    var acc = document.getElementsByClassName("accordion");
    var i;

    for (i = 0; i < acc.length; i++) {
      acc[i].addEventListener("click", function () {
        this.classList.toggle("active");
        var panel = this.nextElementSibling;
        if (panel.style.maxHeight) {
          panel.style.maxHeight = null;
        } else {
          panel.style.maxHeight = panel.scrollHeight + "px";
        }
      });
    }
  }

}

注意:由于我是angular的新手,所以我用javascript来制作它..请使用纯角度和打字稿帮助我实现结果.

工作堆闪电https://stackblitz.com/edit/angular-lp3riw

您可以在演示中看到父按钮是可见的,但是如果单击该按钮,它将不会展开.

还在下面列出了带有静态值的可工作折叠按钮.

如何使用角度和打字稿方式(没有任何第三方或jquery)使给定的可折叠手风琴像给定的stackblitz静态值.

解决方法:

将函数保留在ngAfterViewInit中,而不是ngOnInit中.查看更新的stackblitz

问题在于,在ngOnInit上,视图未完全绘制,并且没有获得要绑定功能的所有元素.

ngAfterViewInit() {
    var acc = document.getElementsByClassName("accordion");
    var i;

    for (i = 0; i < acc.length; i++) {
      acc[i].addEventListener("click", function () {
        this.classList.toggle("active");
        var panel = this.nextElementSibling;
        if (panel.style.maxHeight) {
          panel.style.maxHeight = null;
        } else {
          panel.style.maxHeight = panel.scrollHeight + "px";
        }
      });
    }
  }

使用angular如下所示.

在按钮上保留单击功能,并将属性isActive绑定到相应的数组元素.然后根据isActive是否具有值true / false来显示/隐藏手风琴.

<div *ngFor="let item of data;let i = index;">
  <button class="accordion" (click)="toggleAccordian($event, i)"> {{item.parentName}} </button>
  <div class="panel" *ngFor="let child of item.childProperties" hide="!item.isActive">
  <p> {{child.propertyName}} </p>
</div>
</div>


toggleAccordian(event, index) {
      var element = event.target;
      element.classList.toggle("active");
      if(this.data[index].isActive) {
        this.data[index].isActive = false;
      } else {
        this.data[index].isActive = true;
      }      
      var panel = element.nextElementSibling;
      if (panel.style.maxHeight) {
        panel.style.maxHeight = null;
      } else {
        panel.style.maxHeight = panel.scrollHeight + "px";
      }
  }

标签:html,accordion,javascript,angular,typescript
来源: https://codeday.me/bug/20191010/1888577.html