我正在使用角度制作距离计算应用程序,
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 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在车辆一中)计算结果为,
For the 前 5 KMS (0 - 5),票价是10/公里所以5*10 = 50, 哪里为接下来的 15 公里(6 到 20)票价是20/公里所以15*20 = 300.
所以总票价是50 + 300 = 350
上面给出的场景只是举例,如果我选择第二辆车,那么票价需要根据公里分割和票价/公里来计算。
如果选择像上面所说的那么总票价输入值,
<input type="number"
placeholder="Fare Per Km"
formControlName="farePerKm">
需要根据上面给出的示例使用上面计算的值 350 进行更新,该值根据选择而变化。
Edit:请不要担心给定的结构,因为在我的实际应用程序中,我正在使用地图来计算行驶的距离,我现在已经将其全部包含在表单中。
唯一要求我是否需要获取乘客乘坐车辆行驶的总距离的总票价值,该车辆的票价计算是根据给定的公里数计算的。
下面给出的是一号车的拆分情况。因此,如果我乘坐这辆车行驶 20 公里,那么总票价需要为 350 美元(例如),对于任何其他不同的车辆来说也是如此。
From Distance (Km) To Distance (Km) Fare Per Kilometer
0 5 10
6 20 20