primeng icon indicating copy to clipboard operation
primeng copied to clipboard

Dialog prevent from closing

Open jiangyh1024 opened this issue 8 years ago • 5 comments

If you have a PrimeNG PRO Support subscription please post your issue at;

https://pro.primefaces.org

where our team will respond within 4 business hours.

If you do not have a PrimeNG PRO Support subscription, fill-in the report below. Please note that your issue will be added to the waiting list of community issues and will be reviewed on a first-come first-serve basis, as a result, the support team is unable to guarantee a specific schedule on when it will be reviewed. Thank you for your understanding.

Current Waiting Time: ~8 weeks.

I'm submitting a ... (check one with "x")

[ ] bug report => Search github for a similar issue or PR before submitting
[ ] feature request => Please check if request is not on the roadmap already https://github.com/primefaces/primeng/wiki/Roadmap
[ ] support request => Please do not submit support request here, instead see http://forum.primefaces.org/viewforum.php?f=35

Plunkr Case (Bug Reports) Please demonstrate your case at stackblitz by using the issue template below. Issues without a test case have much less possibility to be reviewd in detail and assisted.

https://stackblitz.com/github/primefaces/primeng-issue-template

Current behavior

Expected behavior

How can I prevent the dialog from closing under some conditions. For example, the dialog contains a form, and if user changes some fields and click close button without saving. Under this situation, I want to prevent user from closing the dialog and ask user whether to save data or not.

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

Please tell us about your environment:

  • Angular version: 5.X
  • PrimeNG version: 5.X
  • Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]
  • Language: [all | TypeScript X.X | ES6/7 | ES5]

  • Node (for AoT issues): node --version =

jiangyh1024 avatar Sep 12 '18 08:09 jiangyh1024

I found a way.

ngOnInit() { // this.tabs._handleClick = this.interceptTabChange.bind(this);

this.dialog.close = this.interceptDialogClose.bind(this);

}

interceptDialogClose(event: any) { let a = 0; a = 1; if (a === 1) { event.preventDefault(); } else { Dialog.prototype.close.apply(this.dialog, [event]); } }

jiangyh1024 avatar Sep 12 '18 08:09 jiangyh1024

There is also closable property.

cagataycivici avatar Sep 18 '18 08:09 cagataycivici

There is also closable property.

According to document, closable is to 'Adds a close icon to the header to hide the dialog.' What I want to implement is after clicking close button, system will check if the form is dirty, if no, then close the dialog, if yes, the dialog should be kept open, instead of being closed).

jiangyh1024 avatar Sep 18 '18 08:09 jiangyh1024

@jiangyh1024 thanks for the hack, but can we have a built in event that we can cancel? onHide appears as if it was copied over from the documentation for Dialog, as it isn't in the definition for DynamicDialog, and as was mentioned, the closeable property only adds the icon to the header. I know I can create my own close button, but i would like to use the one built into the dialog so it matches with themes.

dcanning avatar Dec 04 '19 15:12 dcanning

The other solution is to change icon for close button by using template closeicon (https://primeng.org/dialog#api.dialog.templates). And then intercept events on that icon. Like:

 <p-dialog>
  <ng-template pTemplate="closeicon">
    <i class="pi pi-times" (click)="onClose($event)"></i>
  <ng-template>
</p-dialog>
onClose(event: Event) {
   if (form.invalid) {
      // Prevents PrimeNG to close dialog by intercepting event
      event.stopPropagation();
   }
}

agorodnichev avatar Oct 10 '24 13:10 agorodnichev