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', mand: (event) => {
// this.viewItemDetail() // here I wanted to bind item.id of the selected item and navigate to item detail page
}
},
{
label: 'Edit Item', mand: (event) => {
this.editItem() // here I wanted to bind item.id of the selected item and navigate to item edit page
}
},
];
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', mand: (event) => {
// this.viewItemDetail() // here I wanted to bind item.id of the selected item and navigate to item detail page
}
},
{
label: 'Edit Item', mand: (event) => {
this.editItem() // here I wanted to bind item.id of the selected item and navigate to item edit page
}
},
];
Share
Improve this question
asked Feb 12, 2020 at 11:11
KirubelKirubel
1,6271 gold badge16 silver badges35 bronze badges
2 Answers
Reset to default 8you can create a property to hold the current item and set the selected item at click event before invoke toggle method
ponent
selectedItem:any = null;
...
ngOnInit(){
this.tableMenuItems = [
{
label: 'View Item', mand: (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', mand: (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>
Very good solution. I had apply this same concept to PrimeNg Table and SplitButton using onDropdownClick event, like this:
Template:-
<p-splitButton [label]="getButtonText(element.status)" [model]="menuItens" icon="pi pi-plus-circle"
(onClick)="edit(element.identifier, element.title, element.status, element)" (onDropdownClick)="selectedItem = element"></p-splitButton>
Then in respective ts file ponent:
this.menuItens = [
{
label: 'Print', icon: 'pi pi-fw pi-print', mand: () => {
this.print(this.selectedItem.identifier, this.selectedItem.title, this.selectedItem);
}
},
{ separator: true },
{ label: 'Admin'}
];