gpt4 book ai didi

javascript - 如何将 id 绑定(bind)到 PrimeNg 菜单命令

转载 作者:行者123 更新时间:2023-12-04 10:35:55 25 4
gpt4 key购买 nike

我有一个包含项目列表的表格。我想将 PrimeNg Menu 用于下拉菜单选项,以导航到具有所选项目 ID 的其他页面。我想做的是,当我点击菜单项时,我想绑定(bind)所选项目的 ID。

我的 HTML 看起来像这样

<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>

和我的.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
}
},
];

最佳答案

您可以创建一个属性来保存当前项目并在调用切换方法之前在点击事件中设置所选项目

组件

 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
}
},
];
}

模板

<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>

关于javascript - 如何将 id 绑定(bind)到 PrimeNg 菜单命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60187042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com