简体   繁体   English

角6材料设计手风琴openall

[英]angular 6 material design accordion openall

I tried to have two accordions group with different data in my angular application like the code which i have mentioned below but I don't know how to use two same components in my app. 我试图在我的角度应用程序中使用两个具有不同数据的手风琴组,例如下面提到的代码,但是我不知道如何在我的应用程序中使用两个相同的组件。

 import { OnInit } from '@angular/core'; import { Component, ViewChild, ViewEncapsulation} from '@angular/core'; import {MatAccordion} from '@angular/material'; @Component({ selector: 'app-demo-accordion', templateUrl: './demo-accordion.component.html', styleUrls: ['./demo-accordion.component.css'] }) export class DemoAccordionComponent implements OnInit { constructor() { } ngOnInit() { } @ViewChild(MatAccordion) accordion: MatAccordion; @ViewChild(MatAccordion) testing: MatAccordion; displayMode: string = 'default'; displayMode1: string = 'default'; multi = true; hideToggle = false; hideToggle1 = false; disabled = false; showPanel3 = true; panel11 = false; expandedHeight: string; collapsedHeight: string; } 
 <h1>matAccordion</h1> <div> <p>Accordion Options</p> <p>Accordion Actions <sup>('Multi Expansion' mode only)</sup></p> <div> <button mat-button (click)="accordion.openAll()" >Expand All</button> <button mat-button (click)="accordion.closeAll()" >Collapse All</button> </div> <p>Accordion Panel(s)</p> </div> <br> <mat-accordion [displayMode]="displayMode" [multi]="multi" id="newcha" class="mat-expansion-demo-width"> <mat-expansion-panel #panel1 [hideToggle]="hideToggle"> <mat-expansion-panel-header>Section 1</mat-expansion-panel-header> <p>This is the content text that makes sense here.</p> </mat-expansion-panel> <mat-expansion-panel #panel2 [hideToggle]="hideToggle" [disabled]="disabled"> <mat-expansion-panel-header>Section 2</mat-expansion-panel-header> <p>This is the content text that makes sense here.</p> </mat-expansion-panel> </mat-accordion> <br> <hr> <div> <mat-accordion [displayMode]="displayMode1" [multi]="multi" id="testing" class="mat-expansion-demo-width"> <mat-expansion-panel #panel11 [hideToggle]="hideToggle1"> <mat-expansion-panel-header>Section 12</mat-expansion-panel-header> <p>This is the content text that makes sense here.</p> </mat-expansion-panel> <mat-expansion-panel #panel11 [hideToggle]="hideToggle1" [disabled]="disabled"> <mat-expansion-panel-header>Section 13</mat-expansion-panel-header> <p>This is the content text that makes sense here.</p> </mat-expansion-panel> </mat-accordion> </div> <div> <button mat-button (click)="testing.openAll()" >Expand All New</button> <button mat-button (click)="testing.closeAll()" >Collapse All New</button> </div> 

Now if i try to click the Expand All New Button it have to do the openAll for 2nd Accordion but it is opening the 1st Accordion How to access the specific accordion in Angular Material Design? 现在,如果我尝试单击“展开所有新按钮”,则必须为第二手风琴执行openAll,但它正在打开第一手风琴。如何在Angular Material Design中访问特定的手风琴? Like if i have two accordions with which parameter i can access the specific accordions? 就像我有两个手风琴一样,我可以使用哪个参数访问特定的手风琴?

Try property expanded 尝试expanded属性

[expanded]="true"

For all expansion panel 对于所有扩展面板

Reference ---> https://material.angular.io/components/expansion/examples 参考---> https://material.angular.io/components/expansion/examples

You can make a few minor changes to get this working. 您可以进行一些小的更改以使此功能生效。

Change: 更改:

@ViewChild(MatAccordion) accordion: MatAccordion;
@ViewChild(MatAccordion) testing: MatAccordion;

to: 至:

@ViewChild('firstAccordion') firstAccordion: MatAccordion;
@ViewChild('secondAccordion') secondAccordion: MatAccordion;

Add in some functions: 添加一些功能:

openAllFirst() {
    this.firstAccordion.openAll();
}
closeAllFirst() {
    this.firstAccordion.closeAll();
}
openAllSecond() {
    this.secondAccordion.openAll();
}
closeAllSecond() {
    this.secondAccordion.closeAll();
}

And in the HTML, add the following to the two accordions: 在HTML中,将以下内容添加到两个手风琴中:

<mat-accordion ....  #firstAccordion="matAccordion">
<mat-accordion ....  #secondAccordion="matAccordion">

And change the buttons to the new functions: 并将按钮更改为新功能:

(click)="openAllFirst()"

I've copied your code above into Stackblitz, and made these changes. 我已将您的代码复制到Stackblitz中,并进行了更改。

Stackblitz - Multiple accordion expand all Stackblitz-多个手风琴全部展开

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

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