I have two dropdowns,start time and end time, the first dropdown uses a time array, where time is selected, and then index of this time is used to slice , and array is made for the second dropdown. So that end time is always greater than start time.
Here I am trying to set default values when the submit button is clicked, but when I click the submit button then I am
- not getting the set Start time vale,it appears blank and
- getting end time value in the dropdown, but the drop down value array also shows time values less than the start time as well.
template
Start at:
<select
[(ngModel)]="time1"
(change)="onSelect($event.target.value)" id="fTime">
<option value="0" selected>Select</option>
<option [value]="i" *ngFor="let type of startTimeArray; let i = index">
{{type}}
</option>
</select>
End at:
<select
[(ngModel)]="time2"
id="tTime">
<option value="0" selected>Select</option>
<option [value]="type" *ngFor="let type of tempEndTimeArray">
{{type}}
</option>
</select>
<div><button (click)="submit()">click</button></div>
TS
import { Component } from "@angular/core";
import { FormGroup, FormArray, FormBuilder, FormControl } from "@angular/forms";
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent {
forTableArray: any = [];
time1:any=[];
time2:any=[];
startTimeArray:any=["07:00 AM","08:00 AM", "09:00 AM", "10:00 AM", "11:00 AM", "12:00 PM", "1:00 PM", "2:00 PM", "3:00 PM", "4:00 PM", "5:00 PM", "6:00 PM", "7:00 PM", "8:00 PM"];
endTimeArray:any=["07:00 AM","08:00 AM", "09:00 AM", "10:00 AM", "11:00 AM", "12:00 PM", "1:00 PM", "2:00 PM", "3:00 PM", "4:00 PM", "5:00 PM", "6:00 PM", "7:00 PM", "8:00 PM"];
public tempEndTimeArray: Array<any>;
public enableEndTime: boolean = true;
submit(){
this.time1="";
this.time2="";
this.tempEndTimeArray =this.startTimeArray;
this.time1="7:00 AM";
this.time2="5:00 PM";
}
public onSelect(val){
console.log(val)
let index = parseInt(val) + 1;
console.log(index)
this.tempEndTimeArray = this.endTimeArray.slice(index);
}
convertTime12to24(time12h) {
const [time, modifier] = time12h.split(" ");
let [hours, minutes] = time.split(":");
if (hours === "12") {
hours = "00";
}
if (modifier === "PM") {
hours = parseInt(hours, 10) + 12;
}
return `${hours}:${minutes}`;
}
}
Apart from that I am not able to get selected time value in my console, example if 01:00 PM is selected from the dropdown, I want it to display in the console, instead I am getting index value(as I have set [value]= i, in my html select dropdown, so that i can use slice for time)