PrimeNG: custom header for DynamicDialog
Asked Answered
A

5

7

Is there a way to define a custom header template for a dynamic dialog?

For the normal dialog you have the possibility to define the p-header tag, with your custom html code.

But I don't find any way to do this for a dynamic dialog.

Afresh answered 3/9, 2020 at 13:17 Comment(0)
S
3

The answer is a little bit late. But maybe someone else is also searching for a workaround. Afaik it is still not possible since this issue on GitHub is still open.

However in documentation to DynamicDialog there is mentioned, that you can not show the header with the parameter showHeader. This means you could easily just hide the default header and in the component for your dialog design your own header.

Selfmortification answered 9/9, 2022 at 8:36 Comment(0)
K
2

There is no official way mentioned in PrimeNG documents to add custom template to DynamicDialog header. When we open a DynamicDialog, we only attach a body of Dialogbox and not header/footer.

You can add dynamic title to DynamicDialog while opening it. Hope this will help.

const ref = this.dialogService.open(DialogComponent, {
  data: this.data,
  header: this.title,
  width: '60%'
});

You can modify css of DynamicDialog header like:

::ng-deep .p-dialog .p-dialog-header {
  border-bottom: 1px solid #000;
}

Important Note: You can use simple Dialog in PrimeNG where you can create custom headers,body & Footers. It will work same like DynamicDialog. Do mention modal=true like below:

<p-dialog [(visible)]="display" [modal]="true">
<p-header>
    Header content here
</p-header>
Content
<p-footer>
    //buttons
</p-footer>
Kerf answered 27/9, 2020 at 14:7 Comment(1)
Using p-dialog is very much not the same. The contents (and any child components) initialize with the parent (and are simply hidden), rather than when the dialog opens. This can have huge performance implications if the child components are doing any heavy lifting, API calls, etc.Pawnbroker
F
2

It is possible to do like that:

     this.resultsDialogRef = this.dialogService.open(ResultsComponent, {
    header: 'Preview Results',
    width: '70%',
    contentStyle: {"overflow": "auto"},
    baseZIndex: 5000,
    maximizable: true,
    templates: {
      header: ResultsDialogHeaderComponent
    }
  }
);
Frug answered 6/1 at 19:28 Comment(0)
I
1

There is one workaround.

In the component where dialog opens

show() {
    this.ref = this.dialogService.open(DynamicDialogContent, {
        header: '',
        width: '70%',
        contentStyle: { overflow: 'auto' },
        baseZIndex: 10000
    });
}

In the DynamicDialogContent component

ngOnInit() {
    const componentRef = this.viewContainerRef.createComponent(CustomHeader);
    let titleSpan = document.getElementsByClassName('p-dialog-title')[0];
    this.renderer.appendChild(titleSpan, componentRef.location.nativeElement)
}

And the custom-header component

@Component({
    template: ` <b>I'm custom dynamic dilog header</b>`
})
export class CustomHeader implements OnInit {
    constructor() {}

    ngOnInit() {
    }
}

Here is the full demo - stackblitz

Instable answered 20/10, 2023 at 10:9 Comment(0)
A
0

This work for me:

ngOnInit() {

    let titleSpan = document.getElementsByClassName('p-dialog-title')[0];            
    titleSpan.innerHTML = 'custom dynamic header'
   }
Attorney answered 29/2 at 20:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.