I'm trying to figure out how to display & populate a table with data after the AJAX call has taken place and the data has been retrieved.
The user starts off at AppointmentHome, which has a datepicker, where they can pick the day of appointments they want to retrieve. Once they choose the date and click on Search, getAppointmentData() is called, and the data is retrieved.
Currently, I'm retrieving the data into the var appointmentArray in the callback for getAppointmentData(), and then call CreateTableRows() in the callback, so that it gets called after the data has been retrieved and not before.
Now, I'm having a difficult time figuring out how to get the output of CreateTableRows() in the render of AppointmentList so that it gets populated along with the rest of the table. I believe I may need to move some code into some lifecycle methods, but am stuck on how to move from one component to the next without messing up retrieving the data.
var appointmentArray = null;
var table;
function getAppointmentData(){
window.getAppointments(date, 'callback');
}
window.callback = function(objects){
appointmentArray = objects;
table = createTableRows();
}
function createTableRows(){
var output = "<tbody>";
for (var i = 0; i < 1; i++){
output += "<tr><th scope='row'>"+appointmentArray[i]+ "</th></tr>";
}
output += "</tbody>";
return output;
}
export class AppointmentList extends React.Component {
constructor(props){
super(props);
}
render(){
return(
<div className = "appointment-table">
<table className="table table-hover">
<thead>
<tr>
<th scope="col">Patient Name</th>
<th scope="col">Time</th>
<th scope="col">Duration</th>
<th scope="col">Provider</th>
<th scope="col">Appointment Status</th>
</tr>
</thead>
</table>
</div>
);
}
}
export class AppointmentHome extends React.Component {
constructor(props){
super(props);
this.handleClick = this.handleClick.bind(this);
}
handleClick(){
getAppointmentData();
}
render(){
return(
<div>
<h3> Appointment Search </h3>
<div className = 'search'>
<h3> Date </h3>
<Calendar />
<div className = 'searchButton'>
<Link to="appointments">
<RaisedButton label="Search" onClick = {this.handleClick}/>
</Link>
</div>
</div>
</div>
);
}
}