简体   繁体   English

使用 Angular 中的复选框从数组中删除数据?

[英]Removing data from array using checkbox in Angular?

I have a mat-table with some data generated through an API, here's a snippet for example:我有一个 mat-table,其中包含一些通过 API 生成的数据,例如,这里有一个片段:

<mat-table [dataSource]="dataSource$">
     <ng-container matColumnDef="process">
        <mat-header-cell #checkBox *matHeaderCellDef> Process </mat-header-cell>
        <mat-cell *matCellDef="let execution"> {{execution.process}} </mat-cell>
     </ng-container>
     <ng-container matColumnDef="reprocess">
      <mat-header-cell *matHeaderCellDef>  
         <button tslbutton class="tslbutton" id="reprocessButton">
            Reprocess
         </button> 
      </mat-header-cell>
      <mat-cell id="reprocessCell" *matCellDef="let execution">
         <mat-checkbox (change)="reprocessHomeProcesses(execution)" class="checker"></mat-checkbox>
      </mat-cell>
   </ng-container>
</mat-table>

Basically, each row has a process and a checkbox.基本上,每一行都有一个进程和一个复选框。 Above the checkbox is a button that will call an API with an array of the checked data.复选框上方是一个按钮,该按钮将调用 API,其中包含一组选中的数据。 I need to add the "execution" data to an array by clicking the checkbox.我需要通过单击复选框将“执行”数据添加到数组中。 Currently, I'm able to send the data to the TS using the (change) function. This calls the following function:目前,我可以使用(更改)function 将数据发送到 TS。这将调用以下 function:

executions = []

reprocessHomeProcesses(exe) {
    this.executions.push(exe);
    console.log(this.executions)
  }

This successfully adds the execution object to the array.这成功地将执行 object 添加到数组中。 Looks like this:看起来像这样:

[{process: 1234, ....}]

However, when I deselect, the checkbox, it adds the same object again但是,当我取消选择复选框时,它会再次添加相同的 object

[{process: 1234, ....}, {process: 1234, ....}]

(as expected, I don't have any logic for this.executions.pop() yet) How can I rewrite the function so that when I deselect the checkbox, it removes the object from the array? (正如预期的那样,我还没有this.executions.pop()的任何逻辑)我如何重写 function 以便当我取消选中复选框时,它从数组中删除 object?

I consulted this question: Remove deselected checkbox value from Array using Angular but I could not seem to get it to work for my code, maybe there's some slight differences in layout.我咨询了这个问题: Remove deselected checkbox value from Array using Angular但我似乎无法让它为我的代码工作,可能布局略有不同。 But any help to remove item from array when checkbox is deselected would be great help!但是在取消选中复选框时从数组中删除项目的任何帮助都会有很大帮助!

I think I have an understanding of the adding and removal I just dont really understand how to differentiate check vs uncheck.我想我对添加和删除有所了解我只是不太了解如何区分检查与取消检查。

My solution was pretty simple.我的解决方案非常简单。 I added an event to the function call - like this.我向 function 调用添加了一个事件 - 就像这样。

<tsl-checkbox #myCheckbox [checked] (change)="setHomeProcesses(execution, $event)"></tsl-checkbox>
reprocessHomeProcesses(exe, event: any) {
    if (event.checked) {
      this.reprocesses.push(exe)
    } else {
      var index = this.reprocesses.indexOf(exe)
      if (index > -1) {
        this.reprocesses.splice(index, 1);
      }
    }
  }

This works as needed here.这在这里根据需要工作。

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

相关问题 取消选中复选框时从数组中删除数据 - Removing data from an array when unchecking the checkbox 使用Angular从数组中删除未选中的复选框值 - Remove deselected checkbox value from Array using Angular 取消选择复选框将从前端角度4中删除整行 - deselecting checkbox is removing whole row from the front-end angular 4 如何在不使用指定角度删除现有数据的情况下在特定于数组的索引处插入数组 - How do I insert an array at an array specific index without removing existing data at specified index using angular 从节点查询器中使用复选框创建的数组中删除逗号 - Removing commas from Array created with checkbox from Node Inquirer 从复选框组中获取值并从数组中添加/删除它们 - Getting values from checkbox group and adding/removing them from array 使用 angular 4 删除对象数组中的重复项 - Removing duplicates with in an object array using angular 4 从角度4的阵列列表中删除错误的项目 - removing wrong items from the array list in angular 4 未选中复选框时从数组中删除项目在反应中未按预期工作 - Removing Item from Array when checkbox is unchecked not working as Expected in react 尝试使用复选框取消选择处理从数组中删除项目 - Trying to Handle Removing Item From Array with Checkbox De-Select
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM