简体   繁体   English

如何在单击按钮时动态添加角材扩展面板?

[英]How to add angular material expansion panel dynamically on button click?

I'm setting up a dynamic expansion panel in my Angular 7 application, I am showing a mat-expansion-panel by default in mat-accordion . 我正在Angular 7应用程序中设置一个动态扩展面板,默认情况下,我在mat-accordion显示了mat-expansion-panel Now I have an Add button, and I want if a user clicks on this button, a new mat-expansion-panel should append in mat-accordion with a small form or 4 form inputs . 现在,我有一个“ 添加”按钮,我希望如果用户单击此按钮,新的mat-expansion-panel应该以较小的形式或4个form inputs附加在mat-accordion

I tried it with rendered2 class but it is not working for me. 我尝试了rendered2类,但它对我不起作用。 I might be missing something. 我可能会缺少一些东西。

<mat-accordion id="quoteAccordion">
   <mat-expansion-panel>
       <mat-expansion-panel-header>
          <mat-panel-title>
             <!--  -->
               Panel Title Here
               <!--  -->
               </mat-panel-title>
                  <mat-panel-description>
                    Panel description here
                  </mat-panel-description>
       </mat-expansion-panel-header>
       <!-- Panel Body -->
           <div formArrayName="services" *ngFor="let service of services.controls; let i = index">
                <mat-form-field appearance="outline" fxFlex="30" class="pr-4">
                   <mat-label> Description</mat-label>
                   <input matInput formControlName="title" required>
                   <mat-error>Description is required!</mat-error>
                </mat-form-field>
           </div
       <!-- /Panel Body -->
       </mat-expansion-panel>
       <!-- 
           on click of [Add] button
           Need to append another expansion
           panel with form input here
        -->
</mat-accordion>
       <!--  Ends Here -->
       </div>
<button class="w-100" matTooltip="Add" type="button" mat-raised-button (click)="addAccordion()">
       <mat-icon>add</mat-icon> ADD
</button>

I am expecting the output of adding dynamic mat-expansion-panel with expansion-header and the expansion-body contains a form-input . 我期望添加带有expansion-header动态mat-expansion-panel的输出,并且expansion-body包含一个form-input

Thanks in advance !! 提前致谢 !!

I believe this is a good approach for this type of scenario. 我认为这是针对这种情况的好方法。

Whenever you want to create a new set of input controls on any action (such as a click) of User, then do the followings: - 每当您要对用户的任何操作(例如单击)创建一组新的输入控件时,请执行以下操作:-

  1. Create a variable of type an Array in component.ts and push an empty object into Array because you want to show the default control. component.ts创建一个类型为Array的变量,并将一个空对象推入Array因为您想显示默认控件。
  2. Now loop through this variable in component.html file using *ngFor . 现在,使用*ngForcomponent.html文件中循环遍历此变量。
  3. Whenever user clicks on Add button, just push an empty object into the Array using Array.push(object) then angular will automatically add the new control since you have looped through the Array . 每当用户单击“添加”按钮时,只需使用Array.push(object)将一个空对象推入Array即可,因为您已遍历Array所以angular将自动添加新控件。

Let me know if it works or otherwise. 让我知道它是否有效。

Try this: 尝试这个:

Component.html Component.html

<button  type="button" mat-raised-button color="primary" (click)="addItem()">
       <mat-icon>add</mat-icon> ADD
</button>

<mat-accordion>
  <mat-expansion-panel *ngFor="let item of items">
    <mat-expansion-panel-header>
      <mat-panel-title>
        {{item.title}}
      </mat-panel-title>
      <mat-panel-description>
        {{item.description}}
      </mat-panel-description>
    </mat-expansion-panel-header>

    <div>details or form here</div>

  </mat-expansion-panel>

</mat-accordion>

Component.ts: Component.ts:

export interface myModel{
    title?:string;
    description?:string;
    ...
}


items:Array<myModel>=[{title:"default item title" , description:"default item description"}];

addItem(){
   // push object that you need to be added into array
   this.items.push({title:"new title of item"});
}

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

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