【问题标题】:How can a condition in ngIf be true only for the selected rowngIf 中的条件如何仅适用于所选行
【发布时间】:2019-10-29 00:14:07
【问题描述】:

我正在使用 Angular 版本 7 开发一个 Angular 项目。我有一个显示一些 json 对象的表。在每个 json 对象中,我添加了 2 个按钮,当每个按钮被选中时,我都会有条件地显示它们。

我的意思是,当我单击“编辑”按钮时,我希望显示“不编辑”按钮,反之亦然,仅在我按下按钮的那一行。但是发生的情况是,当我单击特定行的“编辑”按钮时,其他行的所有其他按钮都会更改。

我该怎么做?

myapp.component.html 文件:

<table>
    <thead>
      <tr>
        <th scope="col" translate>Name</th>
        <th scope="col" translate>Description</th>
      </tr>
    </thead>
    <tbody>
      <tr *ngFor=" let product of products">
        <td>{{product.name}}</td>
        <td>{{datasource.description}}</td>
        <td>
          <button *ngIf="allowEdit" (click)="edit(product)">
            Edit
          </button>&nbsp;
          <button *ngIf="allowNoEdit" (click)="noEdit(product)">
            No Edit
          </button>&nbsp;
        </td>
      </tr>
    </tbody>
  </table>

myapp.component.ts 文件:

allowEdit: boolean = true;
allowNoEdit: boolean = false;

edit(product) {
   this.allowEdit= !this.allowEdit;
   this.allowNoEdit= !this.allowNoEdit;
}

noEdit(product) {
   this.allowEdit= !this.allowEdit;
   this.allowNoEdit= !this.allowNoEdit;
}

【问题讨论】:

  • 您必须为每一行添加 *ngIf,例如。 *ngIf="product.allowEdit",然后您可以在单击方法中切换它,allowEdit 和 noEdit 以适用于选定的产品 product.edit = !product.edit,它应该适用于每一行。
  • @mocni_T 你能给出示例代码吗?谢谢
  • 检查我的代码
  • 我会使用 CSS。单击编辑按钮时,我会在该行中添加一个类。此类导致编辑按钮消失并出现 noedit 按钮。单击 noedit 按钮时,您然后从行中删除 css 类
  • @schlonzo 我不会。建议尽可能使用*ngIf,以避免angular执行不必要的检查。

标签: javascript angular typescript angular7


【解决方案1】:

myapp.component.html 文件:

<table>
    <thead>
      <tr>
        <th scope="col" translate>Name</th>
        <th scope="col" translate>Description</th>
      </tr>
    </thead>
<tbody>
  <tr *ngFor=" let product of products">
    <td>{{product.name}}</td>
    <td>{{datasource.description}}</td>
    <td>
      <button *ngIf="!product.showEdit" (click)="allowEdit(product)">
        Edit
      </button>&nbsp;
      <button  *ngIf="product.showEdit"(click)="noEdit(product)">
        No Edit
      </button>&nbsp;
    </td>
  </tr>
</tbody>

在你的打字稿中

allowEdit(product) { 
     // in case you want to close all others editing products
     this.products.forEach(product => product.showEdit = false);
     product.showEdit = true;
}

noEdit(product) { 
     product.showEdit = false;
}

您可以在此链接上查看:https://codepen.io/Mocni/pen/PrProv

【讨论】:

  • 我按下按钮。它显示另一个,但仅持续 3 秒。然后无需再次按下相同的按钮即可显示
  • @KathrineHanson 你想一次只显示一个“不编辑”按钮吗??
  • 好吧,如果您一直更改数据(产品)以显示可见的属性将会更改...您可以添加用于添加产品的代码吗?当您处理加载的数据时,您可以看到此演示正在运行。
  • 其实我发现了问题所在。我也很抱歉。我正在使用输出并使用 EventEmitter 发出输出以更新另一个组件。当我放置 EventMitter 时,它不起作用。没有它,它就可以工作。再次感谢。
  • @mocni_T 嗨。有没有其他方法可以做到这一点?我认为这种方法不好,因为它正在改变产品对象的结构。
【解决方案2】:

如果在 json 对象中包含显示此类按钮的条件呢?单击编辑时,您可以修改特定 json 对象(行)的标志并显示您需要的任何内容

为每个产品初始化一个product.allowEdit = false。然后您的Edit 按钮将调用allowEdit(product)() 函数,您可以在其中更改product.allowEdit = !product.allowEdit; 中的标志

【讨论】:

  • 没有。不喜欢这种做法。不过谢谢你的建议。
  • 不知道为什么要建议一个函数调用,当它可以在 DOM 端本身处理时。
  • @shikhar 我猜是因为这是 OP 所遵循的方法。就我个人而言,我总是尽量减少对 OP 代码的更改。也许他/她正在实际代码中的函数中做更多的事情(很可能这只是一个演示)。然而,很多时候确实不可能遵循他/她的方法,因为需要从另一个角度看待解决方案。
【解决方案3】:

您可以将条件作为“可编辑”键动态添加到您的 product 对象并检查条件以使其可编辑,或者您可以检查产品上已经存在的条件 你的 html 将是

如果您将 editable 键作为布尔值添加到您的对象,那么您的 html 可以是

<tbody>
  <tr *ngFor=" let product of products">
    <td>{{product.name}}</td>
    <td>{{datasource.description}}</td>
    <td>
      <button *ngIf="product.editable" (click)="allowEdit(product)">
        Edit
      </button>&nbsp;
      <button *ngIf="product.editable" (click)="noEdit(product)">
        No Edit
      </button>&nbsp;
    </td>
  </tr>
</tbody>

或者,如果您的对象包含按钮应该处于活动状态的所有列表的任何通用值:

<tbody>
<tr *ngFor=" let product of products">
<td>{{product.name}}</td>
<td>{{datasource.description}}</td>
<td>
  <button *ngIf="product.somecondition" (click)="allowEdit(product)">
    Edit
  </button>&nbsp;
  <button *ngIf="product.somecondition" (click)="noEdit(product)">
    No Edit
  </button>&nbsp;
</td>

【讨论】:

  • 这正是其他两个答案所说的
  • 我必须说他们很快并不意味着方法是错误的
猜你喜欢
  • 2016-02-19
  • 1970-01-01
  • 1970-01-01
  • 2020-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-07
  • 1970-01-01
相关资源
最近更新 更多