最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to bind id to PrimeNg menu command - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 8

you 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'}

  ];
发布评论

评论列表(0)

  1. 暂无评论