【问题标题】:Disable only the clicked button of a row in Primeng table仅禁用Primeng表中一行的单击按钮
【发布时间】:2021-09-25 06:09:40
【问题描述】:

我的primeng 表在每一行都有按钮,只要我点击该按钮应该禁用的任何按钮。但是在我的代码中,单击一次按钮就会禁用所有按钮。请指导我如何仅禁用单击的按钮(我不希望按钮切换,禁用的按钮显示仅在页面刷新时启用)。到目前为止,我已经尝试过以下代码。

import { Component } from '@angular/core';
import {MenuItem} from 'primeng/api';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html'
})
export class AppComponent {
  didAction: boolean = false;
  private action() {
    this.didAction = true; //what to do here to disable only the clicked button in the row
  }
}

<p-table [value]="cars">
    <ng-template pTemplate="header">
        <tr>
            <th>Vin</th>
            <th>Year</th>
            <th>Brand</th>
            <th>Action</th>
        </tr>
    </ng-template>
    <ng-template pTemplate="body" let-car>
        <tr>
            <td>{{car.vin}}</td>
            <td>{{car.year}}</td>
            <td>{{car.brand}}</td>
            <td>
              <p-button (click)="action()" class="pointer" label="Click" [disabled]="didAction()"></p-button>
            </td>
        </tr>
    </ng-template>
</p-table>

【问题讨论】:

    标签: html angular events primeng primeng-table


    【解决方案1】:

    您是否在此处发布了所有代码?如果您愿意,我认为我们可以更好地帮助您。

    问题是您在此示例中使用全局变量来禁用一辆或多辆特定汽车。

    我认为你应该:

    • 获取行的索引并将其传递给禁用汽车的函数。
    • 添加禁用属性以禁用特定。

    HTML 文件:

    <p-table [value]="cars">
        <ng-template pTemplate="header">
            <tr>
                <th>Vin</th>
                <th>Year</th>
                <th>Brand</th>
                <th>Action</th>
            </tr>
        </ng-template>
        <ng-template pTemplate="body" let-car let-i="rowIndex">
            <tr>
                <td>{{car.vin}}</td>
                <td>{{car.year}}</td>
                <td>{{car.brand}}</td>
                <td>
                  <p-button (click)="action(i)" class="pointer" label="Click" [disabled]="car.disabled"></p-button>
                </td>
            </tr>
        </ng-template>
    </p-table>
    

    打字稿文件:

    import { Component } from '@angular/core';
    import {MenuItem} from 'primeng/api';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html'
    })
    export class AppComponent {
      cars: car[] = [];
      private action(index: number) {
        cars[index].disabled = true;
      }
    }
    

    【讨论】:

    • 谢谢你直到月亮回来。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    相关资源
    最近更新 更多