skip to Main Content

I’m using primeng in my project. I have a mat-tab with the same component inside the both, this component is a form with a button that when I press shows a confirm dialog. The problem is, when I press the button to show de confirm dialog, appears in both tab. How can I resolve this issue?

    confirmPrinting(event: Event) {
      this.confirmationService.confirm({
          target: event.target as EventTarget,
          message: 'Are you sure with' + this.printSelected +'?',
          header: 'Confirmación',
          icon: 'pi pi-exclamation-triangle',
          acceptIcon:"none",
          rejectIcon:"none",
          rejectButtonStyleClass:"p-button-text",
          accept: () => {
               this.messageService.add({ key: 'tc',severity: 'info', summary: 'Confirm', detail: 'Starting print...' });
             
          },
          reject: () => {
              this.messageService.add({ severity: 'error', summary: 'Cancel', detail: 'Print canceled', life: 3000 });
          }
      });
  }

This is the html:

<button type="submit" class="btn mat-btn" [disabled]="printDisabled" (click)="confirmPrinting($event)"><mat-icon class="tab-icon">print</mat-icon></button>

Html main component:

<mat-tab-group (selectedTabChange)="tabChanged($event)">
    <mat-tab label="PrintNew" >
       <app-print></app-print>
<p-confirmDialog></p-confirmDialog>
    </mat-tab>
    <mat-tab label="ExclusivePrint">
       <app-print></app-print> 
<p-confirmDialog></p-confirmDialog>
    </mat-tab>
    <mat-tab label="Options"><app-options></app-options> </mat-tab>
  </mat-tab-group>

HTML:app-print component


<div class="container">
  <div class="row">
      <div class="mat-card">
        <p-toast position="top-center" key="tc"></p-toast>
          <mat-divider></mat-divider>
          <br>
          <div class="example-button-row" *ngIf="nPrint === 1">
            <br>
            <button mat-raised-button (click)="Exlusive()">Select Exclusive</button>Exc Select: 
          </div>
        <form class="mat-form">        
              <div class="form-group">
                  <div class="row">
                    <div class="col-sm-8">
                      <div class="input-field-box">   
                           <mat-form-field>
                        <mat-label>Select</mat-label>
                        <mat-select  name="idP" [(ngModel)]="selP">
                          <mat-option [value]="one">{{One}}</mat-option>
                          <mat-option [value]="two">{{Two}}</mat-option>
                      </mat-select>
                      </mat-form-field>
                      </div>
                      <button type="submit" class="btn mat-btn" [disabled]="printDisabled" (click)="confirmPrinting($event)"><mat-icon class="tab-icon">print</mat-icon></button>

                  </div>
                    <div class="col-sm-4">
                      <div class="card flex justify-content-center card-res">
                        <h5><u>Result:</u></h5>
                          <p>{{ddd}}:</p>
                       </div>
                    </div>
                  </div>
              </div>
          </form>
      </div>
    </div> 
</div>

I would like the confirmation dialog to only appear once, I need them to be independent.

2

Answers


  1. Not sure you doing something like this.

    calling the onSave on (ngSubmit) and from the button.

    <form #form1="ngForm" *ngIf="organizationHierarchyItem" (ngSubmit)="onSave()">
    <button type="submit" (click)="onSave()" class="btn btn-primary btn-sm margin-btn">Save</button>
    </form>
    Login or Signup to reply.
  2. Since

    <p-confirmDialog></p-confirmDialog>
    

    is part of app-print component and it is included twice, then when you call confirmation.confirm it will trigger the confirm dialog to be opened in both of the components, so you got twice.

    Remove <p-confirmDialog> from app-print ad add it to the main component.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search