If you want to programmatically open a contextMenu in PrimeNG, there is a slightly tricky way to do it.
First, you need to define your ContextMenu in your template:
<p-contextMenu #cm [global]="true" [model]="items"></p-contextMenu>
Next, add the following code to your button: (click)="toggle($event)"
Then, in your typescript file, you can use the following example for the toggle method:
toggle(event){
if(!this.cm.containerViewChild.nativeElement.style.display ||
this.cm.containerViewChild.nativeElement.style.display == 'none') {
//Open contextual menu
setTimeout(() => {
this.cm.position(event);
this.cm.containerViewChild.nativeElement.style.display = 'block';
this.cm.containerViewChild.nativeElement.style.visiblility = 'visible';
}, 0);
}else{
//close contextual menu
setTimeout(() => {
this.cm.containerViewChild.nativeElement.style.display = 'none';
}, 0);
}
}
Remember to define cm
as your ContextMenu
in your typescript file:
@ViewChild("cm") cm:ContextMenu;
Alternatively, you can also consider using the PrimeNG's tiered menu for this purpose.