简体   繁体   English

如何以编程方式切换 Angular 材质扩展面板

[英]How to toggle Angular material expansion panel programmatically

I just started working on an Angular 4 project with material design.我刚刚开始使用材料设计进行 Angular 4 项目。

I am currently working with the expansion component, the API states that a disabled expansion panel can't be toggled by the user, but can still be manipulated programmatically .我目前正在使用扩展组件, API 指出用户无法切换禁用的扩展面板,但仍可以通过编程方式进行操作 I don't know however, how you can toggle your panel programmatically.但是,我不知道如何以编程方式切换面板。

What is the preferred way in Angular to simulate this?在 Angular 中模拟这个的首选方法是什么?

expanded is set to true to expand the expansion panel and set to false to close the expansion panel. Expanded 设置为 true 以展开扩展面板,设置为 false 以关闭扩展面板。 In the following example, expansion panel is programetically opened and closed.在以下示例中,扩展面板以编程方式打开和关闭。 Please refer this link请参考这个链接

TS file TS文件

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

/**
 * @title Expansion panel as accordion
 */
@Component({
  selector: 'expansion-steps-example',
  templateUrl: 'expansion-steps-example.html',
  styleUrls: ['expansion-steps-example.css']
})
export class ExpansionStepsExample {
  step = 0;

  setStep(index: number) {
    this.step = index;
  }

  nextStep() {
    this.step++;
  }

  prevStep() {
    this.step--;
  }
}

HTML file HTML文件

<mat-accordion class="example-headers-align">
  <mat-expansion-panel [expanded]="step === 0" (opened)="setStep(0)" hideToggle="true">
    <mat-expansion-panel-header>
      <mat-panel-title>
        Personal data
      </mat-panel-title>
      <mat-panel-description>
        Type your name and age
        <mat-icon>account_circle</mat-icon>
      </mat-panel-description>
    </mat-expansion-panel-header>

    <mat-form-field>
      <input matInput placeholder="First name">
    </mat-form-field>

    <mat-form-field>
      <input matInput type="number" min="1" placeholder="Age">
    </mat-form-field>

    <mat-action-row>
      <button mat-button color="primary" (click)="nextStep()">Next</button>
    </mat-action-row>
  </mat-expansion-panel>

  <mat-expansion-panel [expanded]="step === 1" (opened)="setStep(1)" hideToggle="true">
    <mat-expansion-panel-header>
      <mat-panel-title>
        Destination
      </mat-panel-title>
      <mat-panel-description>
        Type the country name
        <mat-icon>map</mat-icon>
      </mat-panel-description>
    </mat-expansion-panel-header>

    <mat-form-field>
      <input matInput placeholder="Country">
    </mat-form-field>

    <mat-action-row>
      <button mat-button color="warn" (click)="prevStep()">Previous</button>
      <button mat-button color="primary" (click)="nextStep()">Next</button>
    </mat-action-row>
  </mat-expansion-panel>

  <mat-expansion-panel [expanded]="step === 2" (opened)="setStep(2)" hideToggle="true">
    <mat-expansion-panel-header>
      <mat-panel-title>
        Day of the trip
      </mat-panel-title>
      <mat-panel-description>
        Inform the date you wish to travel
        <mat-icon>date_range</mat-icon>
      </mat-panel-description>
    </mat-expansion-panel-header>

    <mat-form-field>
      <input matInput placeholder="Date" [matDatepicker]="picker" (focus)="picker.open()" readonly>
    </mat-form-field>
    <mat-datepicker #picker></mat-datepicker>

    <mat-action-row>
      <button mat-button color="warn" (click)="prevStep()">Previous</button>
      <button mat-button color="primary" (click)="nextStep()">End</button>
    </mat-action-row>
  </mat-expansion-panel>

</mat-accordion>

Another very simple way to handle Mat Expansion panel programmatically is by using local references in Angular以编程方式处理 Mat Expansion 面板的另一种非常简单的方法是在 Angular 中使用本地引用

<mat-accordion>
  <mat-expansion-panel #mep="matExpansionPanel">
  </mat-expansion-panel>
</mat-accordion>

Now in HTML(not accessible directly in TS file until we use @ViewChild) file use mep to expand or collapse Expansion panel using something like this:现在在 HTML(在我们使用 @ViewChild 之前无法直接在 TS 文件中访问)文件中,使用 mep 使用以下内容展开或折叠扩展面板:

<button (click)="mep.expanded = true;">Expand</button>
<button (click)="mep.expanded = false;">Collapse</button> 

Simple implementation for array of panels面板阵列的简单实现

<mat-expansion-panel [expanded]="indexExpanded == i" disabled="true" *ngFor="...; let i = index">
    ...
    <button (click)="togglePanels(i)">Toggle</button>
    ...
</mat-expansion-panel>

And in code在代码中

indexExpanded: number = -1;

togglePanels(index: number) {
    this.indexExpanded = index == this.indexExpanded ? -1 : index;
}

app.component.html: app.component.html:

    <button (click)='xpandStatus=xpandStatus?false:true'>Toggle it</button>
    <p>
    <mat-expansion-panel [(expanded)]="xpandStatus">
      <mat-expansion-panel-header>
        <mat-panel-title>This an expansion panel</mat-panel-title>
        <mat-panel-description>xpandStatus is {{xpandStatus}}</mat-panel-description>
      </mat-expansion-panel-header>
      Two-way binding on the expanded attribute gives us a way to store and manipulate the expansion status.
    </mat-expansion-panel>
    </p>

app.component.ts app.component.ts

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

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

Here it is live in StackBlitz: https://stackblitz.com/edit/angular-gtsqg8这里是 StackBlitz 中的直播: https ://stackblitz.com/edit/angular-gtsqg8

You can set a var, in this case I used "isExpanded" then use it to open and close set the boolean value.您可以设置一个变量,在这种情况下我使用“isExpanded”然后使用它来打开和关闭设置布尔值。

<mat-expansion-panel
  *ngIf="advertisements.objs"
  [expanded]="isExpanded"
>
</mat-expansion-panel>

If you want to use it with the integrated toogle button instead you must update the "isExpanded" var with the "(opened)" and "(closed)" hooks from mat-expansion-panel.如果您想将它与集成的工具按钮一起使用,您必须使用来自 mat-expansion-panel 的“(打开)”和“(关闭)”挂钩更新“isExpanded”变量。

<mat-expansion-panel
      *ngIf="advertisements.objs"
      [expanded]="isExpanded"
      (opened)="isExpanded = true"
      (closed)="isExpanded = false"
>
</mat-expansion-panel>

Suppose you are looking for the way to trigger open() method from the component on button click, this is what I've been able to get from the material API documentation.假设您正在寻找在按钮单击时从组件触发 open() 方法的方法,这就是我能够从材料 API 文档中获得的内容。

import { MatAccordion, MatExpansionPanel } from '@angular/material/expansion';

export class MyComponent implements OnInit {
  @ViewChild(MatExpansionPanel) pannel?: MatExpansionPanel; 
  @ViewChild(MatAccordion) accordion?: MatAccordion;

constructor(){}

  closePannel() { // for expansion panel only
    if(!this.pannel) { return }
    this.pannel.close();
  }

  closeAll() { // for all panels in accordion
    if(!this.accordion) { return }
    this.accordion.closeAll();
  }
}

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

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