javascript - 如何将数组、OnClick 中的值重置为按钮?
问题描述
我正在使用 Tabulator 生成表格。单击表格单元格时,我将单元格值推送到数组中,数组中每个项目的初始值为“0”。我想实现一个重置按钮,它将在单击重置按钮或窗口中的任何位置时将分配的值重置为“0”。
组件.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
}
组件.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
)
}
推荐阅读
- reactjs - 做出反应。子组件正在改变父组件
- python - 如何使用 numpy 数组值在 Keras 模型中输入张量?
- javascript - 编辑器扩展 onDidCloseTerminal() 导致编辑器冻结
- rundeck - Rundeck 多个作业执行,排队或丢弃?
- delphi - 仅右键单击以选择 VirtualTreeView 中的节点?
- javascript - 如何使用按钮同时添加和删除图像?
- jquery - 未经身份验证的 api laravel 子域
- xodus - 关于xodus的一些问题
- python - 查找并计算两个数组之间的共同 1(TRUE) 和 0(FALSE)
- c# - 您如何解决错误“无法加载文件或程序集'microsoft aspnetcore razor runtime 3.1 1”,