Dynamic DialogDialogs can be created dynamically with any component as the content using a DialogService.

DynamicDialog - 图1

Documentation

Import

  1. import {DynamicDialogModule} from 'primeng/dynamicdialog';
  2.  

Getting Started

Dynamic dialogs require an instance of a DialogService that is responsible for displaying a dialog with a component as its content. Since the dynamically loaded content is not defined at build time, a configuration is necessary using the entryComponents of your parent module. Example below, displays a list of cars using the CarsListDemo component so it needs to be configured at entryComponents along with the import of DynamicDialogModule. The configuration of the demo page is as follows;

  1. @NgModule({
  2. imports: [
  3. CommonModule,
  4. DynamicDialogModule,
  5. ToastModule,
  6. TableModule,
  7. ButtonModule
  8. ],
  9. declarations: [
  10. DynamicDialogDemo,
  11. CarsListDemo
  12. ],
  13. entryComponents: [
  14. CarsListDemo
  15. ]
  16. })
  17. export class DynamicDialogDemoModule {
  18.  

Next step, is injecting an instance of a DialogService to the component that will open the dialog.

  1. import {Component} from '@angular/core';
  2. import {DialogService} from 'primeng/api';
  3. import {CarsListDemo} from './carslistdemo';
  4. import {Car} from '../../components/domain/car';
  5. @Component({
  6. templateUrl: './dynamicdialogdemo.html',
  7. providers: [DialogService]
  8. })
  9. export class DynamicDialogDemo {
  10. constructor(public dialogService: DialogService) {}
  11. }
  12.  

Displaying a dialog is done with the open method where the first parameter is the type of component to load and the second parameter is the configuration of the Dialog such as header, width and more.

  1. <button type="button" (click)="show()" pButton icon="pi pi-info-circle" label="Show"></button>
  2.  
  1. show() {
  2. const ref = this.dialogService.open(CarsListDemo, {
  3. header: 'Choose a Car',
  4. width: '70%'
  5. });
  6. }
  7.  

In case you need to pass data to the component that is dynamically loaded, use the data property that can be access using the DynamicDialogConfig class. In additon, the loaded component can also control the Dialog using the DynamicDialogRef API. Both the DynamicDialogConfig and DynamicDialogRef are injectable using the constructor.

  1. show() {
  2. const ref = this.dialogService.open(CarsListDemo, {
  3. data: {
  4. id: '51gF3'
  5. },
  6. header: 'Choose a Car',
  7. width: '70%'
  8. });
  9. }
  10.  
  1. import {Component} from '@angular/core';
  2. import {Car} from '../../components/domain/car';
  3. import {CarService} from '../../service/carservice';
  4. import {DynamicDialogRef} from 'primeng/api';
  5. import {DynamicDialogConfig} from 'primeng/api';
  6. @Component({
  7. templateUrl: './carslistdemo.html',
  8. })
  9. export class CarsListDemo {
  10. cars: Car[];
  11. constructor(private carService: CarService, public ref: DynamicDialogRef, public config: DynamicDialogConfig) { }
  12. ngOnInit() {
  13. //id: this.config.id
  14. this.carService.getCarsSmall(id).then(cars => this.cars = cars);
  15. }
  16. }
  17.  

Most of the time, requirement is returning a value from the dialog. DialogRef's close method is used for this purpose where the parameter passed will be available at the onCloseevent at the caller. Here is an example on how to close the dialog from the CarsListDemo by passing a selected car.

  1. import {Component} from '@angular/core';
  2. import {Car} from '../../components/domain/car';
  3. import {CarService} from '../../service/carservice';
  4. import {DynamicDialogRef} from 'primeng/api';
  5. import {DynamicDialogConfig} from 'primeng/api';
  6. @Component({
  7. templateUrl: './carslistdemo.html',
  8. })
  9. export class CarsListDemo {
  10. cars: Car[];
  11. constructor(private carService: CarService, public ref: DynamicDialogRef, public config: DynamicDialogConfig) { }
  12. ngOnInit() {
  13. //id: this.config.id
  14. this.carService.getCarsSmall(id).then(cars => this.cars = cars);
  15. }
  16. selectCar(car: Car) {
  17. this.ref.close(car);
  18. }
  19. }
  20.  

Now at the class that opens the dialog, the selected car gets passed to the Observable.

  1. show() {
  2. const ref = this.dialogService.open(CarsListDemo, {
  3. header: 'Choose a Car',
  4. width: '70%'
  5. });
  6. ref.onClose.subscribe((car: Car) => {
  7. if (car) {
  8. this.messageService.add({severity:'info', summary: 'Car Selected', detail:'Vin:' + car.vin});
  9. }
  10. });
  11. }
  12.  

Properties for DynamicDialog

Dynamic Dialogs provide the following customization options.

NameTypeDefaultDescription
dataanynullAn object to pass to the component loaded inside the Dialog.
headerstringnullHeader text of the dialog.
footerstringnullFooter text of the dialog.
widthstringnullWidth of the dialog.
heightstringnullHeight of the dialog.
closeOnEscapebooleantrueSpecifices if pressing escape key should hide the dialog.
baseZIndexnumber0Base zIndex value to use in layering.
autoZIndexbooleantrueWhether to automatically manage layering.
dismissableMaskbooleanfalseSpecifices if clicking the modal background should hide the dialog.
rtlbooleanfalseWhen enabled dialog is displayed in RTL direction.
stylestringnullInline style of the component.
contentStylestringnullInline style of the content section.
styleClassstringnullStyle class of the component.
transitionOptionsstring400ms cubic-bezier(0.25, 0.8, 0.25, 1)Transition options of the animation.
closablebooleantrueAdds a close icon to the header to hide the dialog.
showHeaderbooleantrueWhether to show the header or not.

Events

NameParametersDescription
onShowevent: Event objectCallback to invoke when dialog is shown.
onHideevent: Event objectCallback to invoke when dialog is hidden.

Styling

Following is the list of structural style classes, for theming classes visit theming page.

NameElement
ui-dialogContainer element
ui-dynamicdialogContainer element
ui-dialog-titlebarContainer of header.
ui-dialog-titleHeader element.
ui-dialog-titlebar-iconIcon container inside header.
ui-dialog-titlebar-closeClose icon element.
ui-dialog-contentContent element.
ui-dialog-footerFooter element.

Dependencies

None.

Source

View on GitHub

DynamicDialogDemo

  1. <p-toast [style]="{marginTop: '80px'}"></p-toast>
  2. <button type="button" (click)="show()" pButton icon="pi pi-info-circle" label="Show"></button>
  3.  
  1. export class DynamicDialogDemo {
  2. constructor(public dialogService: DialogService, public messageService: MessageService) {}
  3. show() {
  4. const ref = this.dialogService.open(CarsListDemo, {
  5. header: 'Choose a Car',
  6. width: '70%',
  7. contentStyle: {"max-height": "350px", "overflow": "auto"}
  8. });
  9. ref.onClose.subscribe((car: Car) =>{
  10. if (car) {
  11. this.messageService.add({severity:'info', summary: 'Car Selected', detail:'Vin:' + car.vin});
  12. }
  13. });
  14. }
  15. }
  16.  

CarsListDemo

  1. <p-table [value]="cars" [paginator]="true" [rows]="5" [responsive]="true">
  2. <ng-template pTemplate="header">
  3. <tr>
  4. <th pSortableColumn="vin">Vin <p-sortIcon field="vin"></p-sortIcon></th>
  5. <th pSortableColumn="year">Year <p-sortIcon field="year"></p-sortIcon></th>
  6. <th pSortableColumn="brand">Brand <p-sortIcon field="brand"></p-sortIcon></th>
  7. <th pSortableColumn="color">Color <p-sortIcon field="color"></p-sortIcon></th>
  8. <th style="width:4em"></th>
  9. </tr>
  10. </ng-template>
  11. <ng-template pTemplate="body" let-car>
  12. <tr>
  13. <td><span class="ui-column-title">Vin</span>{{car.vin}}</td>
  14. <td><span class="ui-column-title">Year</span>{{car.year}}</td>
  15. <td><span class="ui-column-title">Brand</span>{{car.brand}}</td>
  16. <td><span class="ui-column-title">Color</span>{{car.color}}</td>
  17. <td>
  18. <button pButton icon="pi pi-search" (click)="selectCar(car)"></button>
  19. </td>
  20. </tr>
  21. </ng-template>
  22. </p-table>
  23.  
  1. export class CarsListDemo {
  2. cars: Car[];
  3. constructor(private carService: CarService, public ref: DynamicDialogRef, public config: DynamicDialogConfig) { }
  4. ngOnInit() {
  5. this.carService.getCarsSmall().then(cars => this.cars = cars);
  6. }
  7. selectCar(car: Car) {
  8. this.ref.close(car);
  9. }
  10. }
  11.