I have a table with a list of items. I wanted to use PrimeNg Menu for dropdown menu options to navigate to other pages with the selected item id. What I wanted to do is, when I click on the menu items I wanted to bind id of the selected item.
my HTML looks like this
<tr *ngFor="let item of items" class="animated fadeIn">
<td>{{item.category}}</td>
<td>{{item.name}}</td>
<td>{{item.date}}</td>
<td>
<div>
<p-menu #tableMenu [popup]="true" [model]="tableMenuItems" appendTo="body"></p-menu>
<button type="button" pButton icon="pi pi-ellipsis-v"
class="ui-button-secondary ui-button-rounded ui-button-transparent"
(click)="tableMenu.toggle($event)">
</button>
</div>
</td>
</tr>
and my .ts
this.tableMenuItems = [
{
label: 'View Item', command: (event) => {
// this.viewItemDetail() // here I wanted to bind item.id of the selected item and navigate to item detail page
}
},
{
label: 'Edit Item', command: (event) => {
this.editItem() // here I wanted to bind item.id of the selected item and navigate to item edit page
}
},
];
you can create a property to hold the current item and set the selected item at click event before invoke toggle method
component
selectedItem:any = null;
...
ngOnInit(){
this.tableMenuItems = [
{
label: 'View Item', command: (event) => {
console.log(this.selectedItem);
// this.viewItemDetail() // here I wanted to bind item.id of the selected item and navigate to item detail page
}
},
{
label: 'Edit Item', command: (event) => {
this.editItem() // here I wanted to bind item.id of the selected item and navigate to item edit page
}
},
];
}
template
<tr *ngFor="let item of items" class="animated fadeIn">
<td>{{item.category}}</td>
<td>{{item.name}}</td>
<td>{{item.date}}</td>
<td>
<div>
<p-menu #tableMenu [popup]="true" [model]="tableMenuItems" appendTo="body"></p-menu>
<button type="button" pButton icon="pi pi-ellipsis-v"
class="ui-button-secondary ui-button-rounded ui-button-transparent"
(click)="selectedItem = item;tableMenu.toggle($event)">
</button>
</div>
</td>
</tr>