简体   繁体   English

如何将数组 OnClick 的值重置为按钮?

[英]How to reset value from an array, OnClick to a button?

I am using Tabulator to generate a table.我正在使用 Tabulator 生成表格。 When a table cell is clicked, I am pushing the cell values to an array, initial value for each item in the array is '0'.单击表格单元格时,我将单元格值推送到数组中,数组中每个项目的初始值为“0”。 I would like to implement a reset button that will reset the assigned value to '0' onClick to the reset button or anywhere in window.我想实现一个重置按钮,它将分配的值重置为“0”onClick 到重置按钮或 window 中的任何位置。

component.ts组件.ts

 names = [{name: first, val: void 0},{name: second, val: void 0}]

 tabulatorColumnName = [{title: firstTitle, field: firstField, cellClick: 
                          this.getValue.bind(this)}
                        {title: secondTitle, field: secondField, 
                          cellClick:this.getValue.bind(this)}]

Assuming there's only one row of data, the following function pushes the firstField, and secondField's data to names[0].val and names[1].val.假设只有一行数据,下面的function将firstField、secondField的数据推送到names[0].val和names[1].val。

     getValue(e, row) {
          this.names[0].val = row.getData().firstField
          this.names[1].val = row.getData().secondField

     }

component.html组件.html

I want to implement a function or button that will clear the [(ngmodel)] value when user click anywhere in window or a button.我想实现一个 function 或按钮,当用户单击 window 或按钮中的任何位置时,它将清除 [(ngmodel)] 值。

 <form (ngSubmit)="saveChild(sideToggleForm)" 
  #sideToggleForm="ngForm">
  <div *ngFor="let name of names">
    <div class="{{ name.type }}">
      <small>{{ name.label }}</small>
      <mat-form-field class="full-width" appearance="outline">
        <input
          name="{{ name.name }}"
          matInput
          [(ngModel)]="name.val"
        />
      </mat-form-field>
    </div>
  </div>
</form>

Add a button in your HTML,在您的 HTML 中添加一个按钮,

<button (click)="resetNameValues()">Reset</button>

Define the function in TS,在TS中定义function,

resetNameValues(){
    // Iterate over name array and set val to 0
    this.names.forEach(
        name => name.val=0
    )
}

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

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