我已经创建了一个表组件,我为每一行添加了一个拆分按钮,并试图调用该函数,但没有工作

下面是我用来创建菜单项的函数

getItems(rowData: any): MenuItem[] {
  const items: MenuItem[] = [
  {
      label: 'Edit', icon: 'pi pi-fw pi-check', command: () => this.update(rowData)
  },
  { label: 'Delete', icon: 'pi pi-fw pi-times' }];
    return items;
  }

  update(employee: any): void {
    this.isEditMode = true;
    this.addEmployee = true;
    this.employee = { ...employee };
}

下面是我在我的组件中调用它的方式

<app-prime-table [data]="data" [columns]="columns" [customBodyTemplate]="customBodyTemplate"
                [globalFilters]="columns" [displayFilter]="true">
            </app-prime-table>
            <ng-template #customBodyTemplate let-rowData>

                <tr>
                    <td *ngFor="let col of columns">
                        <div *ngIf="col.type == 'button' && rowData.isActive">
                            <!-- <button pButton pRipple icon="pi pi-pencil" (click)="update(rowData)"
                                class="p-button-rounded p-button-primary mr-1"></button>
                            <button pButton pRipple icon="pi pi-trash" (click)="delete(rowData)"
                                class="p-button-rounded p-button-danger"></button> -->
                        <p-splitButton label="Actions" [model]="getItems(rowData)" appendTo="body">
                        </p-splitButton>
                        </div>
                        <div *ngIf="!col.type">{{rowData[col.field]}}</div>
                        <div *ngIf="col.type == 'date'">{{formatDate(rowData[col.field])}}</div>
                    </td>
                </tr>
            </ng-template>

我可以看到这些项目,但无法对其执行任何操作

enter image description here

推荐答案

由于使用函数获取下拉列表的值列表而出现问题,由于使用了函数,由于运行更改检测,总是会创建对<编码>items的新引用.要解决这个问题,只需初始化下拉项一次,您就会注意到我们在命令函数上有一个<编码>event属性,我们可以用它来访问数据和执行操作!

我们可以使用<编码>styleClass来引入一个类来设置菜单的样式

我们可以使用<编码>onDropdownClick来存储活动行,并使用它来执行菜单操作!

编码

<编码>import { Component, OnInit } from '@angular/core';
import { Product } from '../../domain/product';
import { ProductService } from '../../service/productservice';
import { MenuItem } from 'primeng/api';

@Component({
  selector: 'table-basic-demo',
  templateUrl: 'table-basic-demo.HTML',
})
export class TableBasicDemo implements OnInit {
  products!: Product[];
  items: MenuItem[];
  activeRow!: Product | null;

  constructor(private productService: ProductService) {}

  ngOnInit() {
    this.items = this.getItems();
    this.productService.getProductsMini().then((data) => {
      this.products = data;
    });
  }

  getItems(): MenuItem[] {
    const items: MenuItem[] = [
      {
        label: 'Edit',
        icon: 'pi pi-fw pi-check',
        command: (event: any) => {
          console.log(event);
          this.update(this.activeRow);
        },
        styleClass: 'red',
      },
      { label: 'Delete', icon: 'pi pi-fw pi-times', styleClass: 'blue' },
    ];
    return items;
  }

  update(employee: any): void {
    alert(JSON.stringify(employee));
  }
}

HTML

<编码> <div class="card">
  <p-table [value]="products" [tableStyle]="{ 'min-width': '50rem' }">
    <ng-template pTemplate="header">
      <tr>
        <th>Actions</th>
        <th>Code</th>
        <th>Name</th>
        <th>Category</th>
        <th>Quantity</th>
      </tr>
    </ng-template>
    <ng-template pTemplate="body" let-product>
      <tr>
        <td>
          <p-splitButton
            (onDropdownClick)="activeRow = product"
            label="Actions"
            [model]="items"
            appendTo="body"
            data-id="product.编码"
          >
          </p-splitButton>
        </td>
        <td>{{ product.编码 }}</td>
        <td>{{ product.name }}</td>
        <td>{{ product.category }}</td>
        <td>{{ product.quantity }}</td>
      </tr>
    </ng-template>
  </p-table>
</div>

Stackblitz Demo

Angular相关问答推荐

文件阅读器未正确给出某些CSV文件的结果

Angular 动画—淡出适用于DIV,不完全适用于子组件

为什么这个拦截器只在发送事件上触发,而不是在实际响应上触发?

如何用Effect()识别正确的Angular 信号?

如何在init为FALSE时以Angular 初始化主拇指擦除器-Swiper 11.0.6Angular 17 SSR

Swiper 11角17 SSR Swiper断点不工作

首期日历发行

Angular 16:CSRF配置:我仍然可以使用HttpXsrfInterceptor和HttpXsrfCookieExtractor类吗?Inteli-J说他们不存在

使用jsPDF生成带有图像的PDF时出现灰色条纹

HTTP拦截器在Angular 17项目中不起作用

当信号的值发生变化时,模板不会更新

在Angular中扩展多个类

没有调用订阅的原因是什么?

Angular 项目构建仅在 GitHub Actions 上失败

缺少 .angular-cli.json 文件:Angular

使用 rxjs 处理刷新令牌

使用 Angular2 将文件上传到 REST API

NG 测试中的调试测试

Angular将 Select 值转换为 int

如何在 Angular cli 项目中包含来自 node_modules 的assets