简体   繁体   English

如何在选中或未选中复选框时从数组中推送和删除项目?

[英]How to push and remove item from an array on checked or unchecked of checkboxes?

when clicking on a checkbox and event.target.checked === true, in the following table, I am pushing the headerName and cell into an array, but I am unable to remove the item when event.target.checked === false, since I don't have the index of the added item.单击复选框和 event.target.checked === true 时,在下表中,我将 headerName 和单元格推入数组,但是当 event.target.checked === false 时我无法删除该项目,因为我没有添加项目的索引。 Any clue to solve this will be appreciated.任何解决此问题的线索将不胜感激。

https://stackblitz.com/edit/angular-ivy-rgchvw?file=src%2Fapp%2Fapp.component.html https://stackblitz.com/edit/angular-ivy-rgchvw?file=src%2Fapp%2Fapp.component.html

component.ts组件.ts

import { Component, VERSION } from "@angular/core";

@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"]
})
export class AppComponent {
  name = "Angular " + VERSION.major;
  public checkedItems = [];
  public tableHeaderNames = [
    "Header1",
    "Header2",
    "Header3",
    "Header4",
    "Header5"
  ];
  public roles = [
    "Role1",
    "Role2",
    "Role3",
    "Role4",
    "Role5",
    "Role6",
    "Role7"
  ];

  select(e, g) {
    if (e.target.checked === true) {
      let checkedObj = {
        headerName: g,
        cell: "test"
      };
      this.checkedItems.push(checkedObj);
    }
    console.log(this.checkedItems);
  }
}

component.html组件.html

<table class="table">
  <thead>
    <tr>
      <th></th>
      <th scope="col" *ngFor="let columnNames of tableHeaderNames"> 
     {{columnNames}}</th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let role of roles">
      <td>{{ role }}</td>
      <td *ngFor="let roleColumn of tableHeaderNames; let i = index">
        <input (click)="select($event, roleColumn)" type="checkbox">
      </td>
    </tr>
  </tbody>
</table>

You will need to add another parameter for the select($event, roleColumn) to check against the role selected.您将需要为select($event, roleColumn)添加另一个参数以检查所选role As you need to check against both headerName and roleNo for cases when it's duplicate in row or column.因为您需要检查headerNameroleNo在行或列中重复的情况。 Then add findIndex and splice for it.然后为其添加findIndexsplice

You can make the changes like so您可以像这样进行更改

html html

<table class="table">
  <thead>
    <tr>
      <th></th>
      <th scope="col" *ngFor="let columnNames of tableHeaderNames">{{columnNames}}</th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let role of roles">
      <td>{{ role }}</td>
      <td *ngFor="let roleColumn of tableHeaderNames">
        <input (click)="select($event, roleColumn, role)" type="checkbox">
      </td>
    </tr>
  </tbody>
</table>

ts ts

select(e, g, r) {
    if (e.target.checked === true) {
      let checkedObj = {
        roleNo: r,
        headerName: g,
        cell: "test"
      };
      this.checkedItems.push(checkedObj);
    }
    if (e.target.checked === false) {
      var index: number = this.checkedItems
                    .findIndex(x => x.roleNo === r && x.headerName === g);
      console.log(index);
      if (index > -1) {
        this.checkedItems.splice(index, 1);
      }
    }
    console.log(this.checkedItems);
}

solved it using findIndex() and Splice()使用 findIndex() 和 Splice() 解决了它

else{
      let index = this.checkedItems.findIndex(
        item => item.headerName === g
      )
      this.checkedItems.splice(index, 1)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM