【问题标题】:Distance Calculation in Angular角度距离计算
【发布时间】: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


【解决方案1】:

只需要一个函数来计算价格。

好吧,在此之前,您必须更好地定义“票价”,其中的票价必须等于 to 和 next from,即

vehicleOne: any = [{ from: "0", to: "5", farePerKm: "10" }, 
                   { from: "5", to: "20", farePerKm: "20" }, //<--"from" is equal to "to" above
                   { from: "20", to: "50", farePerKm: "5" }] //<--"from" is equal to "to" above

另外,用“V1”车做“6Km”的价格是多少?

功能很简单:

  getPrice(vehicleID: string, distance: number) {
    //get the fare
    let fare: any[];
    switch (vehicleID) {
      case "V1":
        fare = this.vehicleOne;
        break;
      case "V2":
        fare = this.vehicleTwo;
        break;
    }
    let price: number = 0;
    let distanceCal = distance;
    fare.forEach(x => {
      let incprice=0;
      if (distanceCal > 0) {
        if (distance > +x.to)
          incprice += ((+x.to) - (+x.from)) * (+x.farePerKm);
        else
          incprice += (distance-(+x.from)) * (+x.farePerKm);

        price+=incprice
        distanceCal -= (+x.to) - (+x.from)
      }
    })
    if (distanceCal>0) //If the distance if greater than the last fare
      price+=distanceCal * (+fare[fare.length-1].farePerKm) //use the last farePerKm

    return price;
  }

嗯,使用开关选择票价有些奇怪。如果你的票价是这样,你可以改进代码

vehicles: any = [
   { id: "V1", vehicleName: "Vehicle One", fares: [{ from: "0", to: "5", farePerKm: "10" }, { from: "5", to: "20", farePerKm: "20" }] },
   { id: "V2", vehicleName: "Vehicle Two", fares: [{ from: "0", to: "10", farePerKm: "15" }, { from: "10", to: "20", farePerKm: "12" }] }

然后你可以将函数更改为

getPrice(vehicleID: string, distance: number) {
    //get the fare
    let fare= this.vehicles.find(x=>x.id==vehicleID).fares;
    ....rest of the code...
}

注意:在您的票价中,from、to 和 farePerKm 是字符串,您必须使用“+”转换为数字 注意2:您必须检查该功能。例如你可以在 ngOnInit -only for check-写一些类似的东西

for (let distance=0;distance<30;distance++)
      console.log(distance,this.getPrice("V1",distance))

【讨论】:

  • 只等待您的回答 Eliseo,您对我的帮助很棒.. 请提供 stackblitz Eliseo..
  • 只是你的 stackblitz 分叉添加函数 getPrice 并添加 ngOnInit。选择距离时必须改调用函数-我不碰这部分-stackblitz.com/edit/…
  • 谢谢Eliseo,我认为最后的部分,比如票价的计算需要我自己来完成。如果可能的话,请在你的最后加上总票价,并在解决方案中更新你的stackblitz,这样这对读者很有用..
  • 查看工作原理,使用参考变量#distance,#vehicleID 和total,在选择中您可以执行(change)="total.value=getPrice(vehicleID.value,distance.value)
【解决方案2】:

给你。我只输入了为了解决您的问题而必须添加或调整的代码。请注意,这不是一个完整的解决方案,而是为您提供正确方向的提示。

在您的AppModule 中添加FormsModule 以便能够使用ngModule 指令。

import { ReactiveFormsModule, FormsModule } from '@angular/forms';

@NgModule({
imports:      [ BrowserModule, ReactiveFormsModule, FormsModule ],
...

在您的 TS 文件中添加以下变量:

protected totalFare: string;
protected chosenVehicle: any;
protected selectedDistance: any;

同时扩展您的车辆列表

vehicles: any = [
{ id: "V1", vehicleName: "Vehicle One", fares: [{ from: "0", to: "5", farePerKm: "10" }, { from: "6", to: "20", farePerKm: "20" }] },
{ id: "V2", vehicleName: "Vehicle Two", fares: [{ from: "0", to: "10", farePerKm: "15" }, { from: "11", to: "20", farePerKm: "12" }] }

]

并添加以下方法

protected onDistanceSelection(): void {
    const vehicle = this.vehicles.filter(el => el.id === this.chosenVehicle)[0];
    this.totalFare = vehicle.fares[0].farePerKm;
}

在您的 HTML 文件中进行以下调整:

<select (change)="selectedValues($event)" [(ngModel)]="chosenVehicle">
  <option value="undefined" disabled selected> Choose a vehicle </option>
  <option *ngFor="let vehicle of vehicles" [value]="vehicle.id"> {{  vehicle.vehicleName  }} </option>
</select>



 <select (change)="onDistanceSelection()" [(ngModel)]="selectedDistance">
     <option value="undefined"  disabled selected> Total distance of travel</option>
     <option value="10"> 10 KM </option>
     <option value="20"> 20 KM </option>
     <option value="30"> 30 KM </option>
     <option value="40"> 20 KM </option>
</select>


<input type="text" [(ngModel)]="totalFare" placeholder="Total fare">

请注意,您在大部分代码中都写错了“未定义”。我在这里更正了。否则,下拉菜单将不会在开始时显示“选择...”文本。

使用此代码可以看到,通过选择车辆并单击距离,当前车辆的 farePerKm 会显示在总票价文本字段中。

有了这个,您应该可以在onDistanceSelection() 内部开始自己的计算。您还可以在此处访问selectedDistance

玩得开心! ;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    相关资源
    最近更新 更多