【发布时间】:2018-06-14 14:42:57
【问题描述】:
我试图弄清楚如何在 AJAX 调用发生并检索数据后显示和填充数据表。
用户从 AppointmentHome 开始,它有一个日期选择器,在那里他们可以选择他们想要检索的约会日期。一旦他们选择日期并单击搜索,就会调用 getAppointmentData() 并检索数据。
目前,我在 getAppointmentData() 的回调中将数据检索到 var 约会数组中,然后在回调中调用 CreateTableRows(),以便在检索到数据之后而不是之前调用它。
现在,我很难弄清楚如何在 AppointmentList 的呈现中获取 CreateTableRows() 的输出,以便它与表的其余部分一起填充。我相信我可能需要将一些代码移动到一些生命周期方法中,但我被困在如何从一个组件移动到下一个组件而不会弄乱检索数据。
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>
);
}
}
【问题讨论】:
标签: reactjs