【发布时间】:2017-10-23 03:52:33
【问题描述】:
我想知道如何只重新加载表格,而不是整个页面的反应。我试过使用 history.go(0);但是,它会重新加载整个页面,请检查如何重新加载它,如果我要使用 forceUpdate,根据研究,你应该避免使用它。我正在尝试做一个 AJAX,但我不知道该放什么放在哪里...它与 php 不同..
我的 onclick 代码
handleUpdate(id, name, address,department){
const data = {
'Employee_ID': id,
'Employee_Name': name,
'Address': address,
'Department': department
}
return fetch('http://localhost:5118/api/employeedetails/PutEmployeeDetail/'+id, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
.then(function(response) {
console.log(response)
return response.json();
})
.then((result)=> {
var jsonReturnedValue = [...this.state.jsonReturnedValue];
jsonReturnedValue[data].Department = department,
jsonReturnedValue[data].Employee_name = name,
jsonReturnedValue[data].Address = address
this.setState({jsonReturnedValue})
})
.catch(function(error) {
console.log(error);
})
}
渲染和表格的代码
render() {
const isEnabled = this.canBeSubmitted();
let {jsonReturnedValue} = this.state;
return(
<div>
<div className="container">
<h1> Listof Employees </h1>
<button className ='btn btn-warning right ' data-toggle="modal" data-target="#AddEmployee"> Add an Employee</button>
<table className= "table table-bordered" id="result">
<tbody>
<tr>
<th>ID</th>
<th>Name</th>
<th>Address</th>
<th>Update</th>
<th>Delete</th>
</tr>
{jsonReturnedValue.map((d,i) => this.renderItem(d,i))}
</tbody>
</table>
</div>
{/*Updating*/}
<div className="modal fade" id="UpdateEmployee" role="dialog">
<div className="modal-dialog">
<div className="modal-content">
<div className="modal-header">
<button type="button" className="close" data-dismiss="modal">×</button>
<h4 className="modal-title">Update Employee</h4>
</div>
<form >
<div className="container">
<div className="modal-body">
<table>
<tbody>
<tr>
<td>Name</td>
<td>
<input type="text"
name="Employee_Name"
value={this.state.Employee_Name}
required
onChange={(e) => this.handleChange(e, "Employee_Name")}/>
</td>
</tr>
<tr>
<td>Address</td>
<td>
<input type="text"
name="Address"
value={this.state.Address}
required
onChange={(e) => this.handleChange(e, "Address")}/>
</td>
</tr>
<tr>
<td>Department</td>
<td>
<input type="text"
name='Department'
value={this.state.Department}
required
onChange={(e) => this.handleChange(e, "Department")}/>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div className="modal-footer">
<input type="button" className="btn btn-info" disabled={!isEnabled} onClick = { this.handleUpdate.bind(this, this.state.Employee_ID , this.state.Employee_Name ,this.state.Address ,this.state.Department)} value =" Update Employee" data-dismiss="modal"/>
<button type="button" className="btn btn-default" data-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
</div>
【问题讨论】:
-
这似乎是您之前的一个问题的两个重复项中的第二个,正如我所指出的,您在其他地方也重复了您的问题。
标签: javascript asp.net api reactjs web