简体   繁体   English

Angular Material Accordion 基于单选按钮值打开/关闭

[英]Angular Material Accordion open/close based on Radio button value

I am trying to use a mat-accordion with a group of radio buttons, one per panel.我正在尝试使用带有一组单选按钮的mat-accordion ,每个面板一个。 If the radio button is selected, expand that panel, close the others and deselect the other radios as expected.如果选择了单选按钮,请展开该面板,关闭其他面板并按预期取消选择其他单选。

I have the radio buttons working as expected, but the panels expand and retract on click as normal;我的单选按钮按预期工作,但面板在点击时正常展开和收缩; I am trying to get them to only expand if their radio button is checked.我试图让他们仅在选中他们的单选按钮时才展开。

I have tried disabling the panel, but that disables its content including the radio button.我尝试禁用面板,但这会禁用其内容,包括单选按钮。

How can i have it only expand/collapse based on its radio button state?我怎样才能让它仅根据其单选按钮 state 展开/折叠?

  <mat-accordion hideToggle="true">
    <mat-expansion-panel>
      <mat-expansion-panel-header>
        <mat-radio-button
          (change)="onChange($event)"
          name="services"
          id="service1"
          value="service1"
        >
          <mat-panel-title> Service 1</mat-panel-title>
        </mat-radio-button>
      </mat-expansion-panel-header>
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Rem, possimus
      aliquam sunt dolorem nam saepe esse laboriosam eius quae quaerat!
    </mat-expansion-panel>
    <mat-expansion-panel>
      <mat-expansion-panel-header>
        <mat-radio-button
          (change)="onChange($event)"
          name="services"
          id="service2"
          value="service2"
        >
          Service 2</mat-radio-button
        >
      </mat-expansion-panel-header>
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Rem, possimus
      aliquam sunt dolorem nam saepe esse laboriosam eius quae quaerat!
    </mat-expansion-panel>
  </mat-accordion>
  {{ selectedService }}

and the component:和组件:

  selectedService = 'service1';

  onChange(event) {
    event.source.checked = true;
    this.selectedService = event.source.value;
  }

Stackblitz demo Stackblitz 演示

You'll have to disable mat-expansion-panel (it still can be opened programmatically).您必须禁用mat-expansion-panel (它仍然可以以编程方式打开)。 Also, you have to fix some CSS style because, for some reason, the radio button is shifted up when you click on it.此外,您必须修复一些 CSS 样式,因为由于某种原因,当您单击单选按钮时,它会向上移动。 So let's start with the styles:那么让我们从styles开始吧:

.fix-radio-position .mat-expansion-panel
.mat-expansion-panel-header mat-panel-description 
.mat-radio-button {
  line-height: 2;
}

Now apply the css class to accordion element:现在将 css class 应用于手风琴元素:

<mat-accordion class="fix-radio-position" hideToggle="true">
...
</mat-accordion>

You'll have to disable your panels by adding the disabled attribute, and put a template reference variable on each one so we can pass them as arguments with the click on the radio buttons:您必须通过添加disabled属性来禁用面板,并在每个面板上放置一个模板引用变量,以便我们可以通过单击单选按钮将它们作为 arguments 传递:

<mat-expansion-panel #panel1 disabled>
...
</mat-expansion-panel>

<mat-expansion-panel #panel2 disabled>
...
</mat-expansion-panel>

Change a little bit the signature of onChange event so you can pass the parent panel as a parameter:稍微更改onChange事件的签名,以便您可以将父面板作为参数传递:

<mat-radio-button (change)="onChange($event, panel1)">Service 1</mat-radio-button>
...
<mat-radio-button (change)="onChange($event, panel2)">Service 2</mat-radio-button>

At last, your onChange method becomes:最后,您的onChange方法变为:

onChange(radio: MatRadioChange, panel: MatExpansionPanel) {
  panel.open();
}

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

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