gpt4 book ai didi

javascript - 如何重置数组中的值,OnClick 到按钮?

转载 作者:行者123 更新时间:2023-11-30 11:00:12 25 4
gpt4 key购买 nike

我正在使用 Tabulator 生成表格。单击表格单元格时,我将单元格值推送到数组,数组中每个项目的初始值为“0”。我想实现一个重置按钮,它将在点击重置按钮或窗口中的任何位置时将分配的值重置为“0”。

component.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)}]

假设只有一行数据,以下函数将 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

我想实现一个函数或按钮,当用户单击窗口或按钮中的任意位置时将清除 [(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>

最佳答案

在您的 HTML 中添加一个按钮,

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

在TS中定义函数,

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

关于javascript - 如何重置数组中的值,OnClick 到按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58119264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com