【发布时间】:2019-06-22 22:25:53
【问题描述】:
我正在使用具有角度的距离计算应用程序,
HTML:
<form [formGroup]="form" *ngIf="showForm">
<div formArrayName="distance" >
<table>
<thead>
<th> From Distance (Km) </th>
<th> To Distance (Km) </th>
<th> Fare Per Kilometer </th>
</thead>
<tbody>
<tr
*ngFor="let item of form.get('distance').controls; let i = index"
[formGroupName]="i">
<td>
<input type="number"
placeholder="From"
formControlName="from">
</td>
<td>
<input type="number"
placeholder="To"
formControlName="to">
</td>
<td>
<input type="number"
placeholder="Fare Per Km"
formControlName="farePerKm">
</td>
</tr>
</tbody>
</table>
</div>
</form>
TS:
selectedValues(e) {
this.showForm = true;
this.form = this.fb.group({
distance: this.fb.array([]),
});
const control = this.form.controls.distance as FormArray;
if (e.target.value === "V1") {
this.vehicleOne.forEach(data => {
control.push(this.fb.group({
from: [data.from, Validators.required],
to: [data.to, Validators.required],
farePerKm: [data.farePerKm, Validators.required]
}));
});
}
else if (e.target.value === "V2") {
this.vehicleTwo.forEach(data => {
control.push(this.fb.group({
from: [data.from, Validators.required],
to: [data.to, Validators.required],
farePerKm: [data.farePerKm, Validators.required]
}));
});
}
}
以上代码仅供参考,完整代码在工作示例https://stackblitz.com/edit/disable-group-control-value-on-another-control-value-for-itrxah
要求: 在此示例中,您可以看到有一个选择下拉菜单,最初显示选择车辆,在选择两辆车中的任何一辆后,您将根据表格中的起点和终点公里获得车辆每公里的票价。
在此表之后,有一个三个下拉菜单显示从位置、到位置、总行驶距离,一个空输入框显示总票价。
我需要的是,如果用户选择Vehicle One(vehicle), Location A (From Location), Location C (To Location), 20KM (Total Distance travelled),则需要将总票价输入更新为 350。
根据上述选择(其中一辆车的总行驶距离为 20Km),计算将是,
对于前 5 KMS (0 - 5),票价为 10 / km 所以 5*10 = 50,其中对于接下来的 15 KMS ( 6到20)票价是20/公里所以15*20 = 300。
所以总票价是50 + 300 = 350
上面给出的场景只是一个例子,如果我选择第二辆车,那么票价需要根据公里分割和票价/公里来计算。
如果选择像上面所说的那么总票价输入值,
<input type="number"
placeholder="Fare Per Km"
formControlName="farePerKm">
需要根据上面给出的示例使用上述计算值 350 进行更新,具体取决于选择。
编辑: 请不要担心给定的结构,因为在我的实际应用程序中,我使用地图来计算行进的距离,我现在已经把它做成了表格中的所有内容。
唯一的要求是我需要获得乘客在车辆中行驶的总距离的总票价值,该车辆根据给定的公里数计算票价。
下面给出的是一号车的拆分。因此,如果我乘坐这辆车行驶 20 公里,那么总车费需要为 350(例如),对于任何其他不同拆分的车辆也是如此。
From Distance (Km) To Distance (Km) Fare Per Kilometer
0 5 10
6 20 20
【问题讨论】:
-
不清楚您要寻求帮助的是哪一部分 - 实际计算、从表单中获取值、创建表单、使用表单的值在表格中查找内容,或者别的东西。
-
@HammerN'Songs,请不要为给定的结构而烦恼,因为在我的实际应用程序中,我使用地图来计算行进的距离,我现在已经把它做成了表格中的所有内容。事情是基于从下拉列表中选择的,需要计算的总票价..(如果我选择一辆车,从位置 A 到位置 C,行驶距离为 20,那么在总票价输入字段中需要附加该值350..
标签: javascript angular typescript angular6 angular7