My question is hard to explain but I hope I can describe it well enough for it to make sense. Essentially I have a form that saves employee data, and I have a section within the form that holds the employees secondary phone number. I have two drop down lists next to this that specify the times that the secondary number is available for contact reasons. I then save the form to our sql database after it is completed. I'm not sure if it's possible, but when I go to pull up this employees data from our application I want the drop down lists to be selected with the current values from before. Now I'm not sure if using select is the best route to go with this, or if I should be doing it a different way, any suggestions would help, thanks!
It would be good to note that this object that I'm receiving from the database is a JSON object that looks like this: {"From":"1:00pm", "To":"8:00pm"}. When that employee is selected in our application I am grabbing this information and parsing it into a state variable.
employee.js
export default class Employee extends Component{
if(this.props.employeeKey){
constructor(props){
super(props);
this.state = {
Name: this.props.name,
Phone: this.props.phone,
SecondaryPhone: this.props.secondaryPhone,
AvailTimes: JSON.parse(this.props.availTimes),
times: this.props.times,
TimeFrom: "",
TimeTo: ""
}else{
this.state = {
Name: "",
Phone: "",
SecondaryPhone: "",
AvailTimes: "",
times: [],
TimeFrom: "",
TimeTo: "",
}
}
}
onChange(value, e){
switch(value){
case "Phone": this.setState(Phone: e.target.value)
break;
case "Name": this.setState(Name: e.target.value)
break;
case "SecondaryPhone": this.setState(SecondaryPhone: e.target.value)
break;
case "FromTime": this.setState(TimeFrom: e.target.value)
break;
case "ToTime": this.setState(TimeTo: e.target.value)
break;
}
render(){
var divToRender = (
<label>
From:
<select onChange={this.onChange.bind(this, 'FromTime')}>{this.state.times.map(time => {return (<option value={time}> {time} </option>)})}</select>
</label>
<label>
To:
<select onChange={this.onChange.bind(this, 'ToTime')}>{this.state.times.map(time => {return (<option value={time}> {time} </option>)})}</select>
</label>
);
return divToRender
}
}
I was thinking of calling the onChange() method before my state to load these values in upon loading the page, but my guess is that it probably won't work.