我想知道如何仅重新加载表格,而不是整个页面的反应。我尝试过使用
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>
jsonReturnedValue
应使用 index
而不是 object
。利用 findIndex
获取数组 中带有
Employee_ID = id
的对象的索引
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];
var idx = jsonReturnedValue.findIndex((val) => val.Employee_ID == id)
jsonReturnedValue[idx].Department = department,
jsonReturnedValue[idx].Employee_name = name,
jsonReturnedValue[idx].Address = address
this.setState({jsonReturnedValue})
})
.catch(function(error) {
console.log(error);
})
}