javascript - 删除方法在 React js 中不起作用。无法删除行
问题描述
我是新来的反应,并试图实现一个删除方法来删除表中的行。我正在使用 web api 删除我的 sql 表中的行。但是,我无法做到这一点。
这是视觉工作室代码中用于反应的代码:
class Department extends Component{
constructor(props){
super(props);
this.refreshList = this.refreshList.bind(this);
this.state={deps:[],addModalShow:false,editModalShow:false}
}
componentDidMount(){
this.refreshList();
}
refreshList(){
const that =this;
fetch('https://localhost:44363/api/department')
.then(response=>response.json())
.then(data=>{
this.setState({deps:data});
}
);
}
componentDidUpdate(){
this.refreshList();
}
deleteDep(depid)
{
if(window.confirm('Are you sure?'))
{
fetch('https://localhost:44363/api/department'+depid,{
method:'DELETE',
header:{'Accept':'application/json',
'Content-Type':'application/json'
}
})
}
}
render(){
const {deps,depid,depname} = this.state;
let addModalClose = () => this.setState({addModalShow:false});
let editModalClose = () => this.setState({editModalShow:false});
return(
<div>
<Table className="mt-4" striped bordered hover size="sm">
<thead>
<tr>
<th>DepartmentId</th>
<th>DepartmentName</th>
<th>Option</th>
</tr>
</thead>
<tbody>
{deps.map(dep =>
<tr key = {dep.DepartmentID}>
<td>{dep.DepartmentID}</td>
<td>{dep.DepartmentName}</td>
<td>
<ButtonToolbar>
<Button
className="mr-2"
variant="info"
onClick={()=>
this.setState({editModalShow:true,depid:dep.DepartmentID,depname:dep.DepartmentName})
}>
Edit
</Button >
<Button className="mr-2"
onClick= {()=>this.deleteDep(dep.DepartmentID)}
variant ="danger" >
Delete
</Button>
<EditDepModal
show={this.state.editModalShow}
onHide={editModalClose}
depid = {depid}
depname={depname}
/>
</ButtonToolbar>
</td>
</tr>
)}
</tbody>
</Table>
<ButtonToolbar>
<Button
variant='primary'
onClick={()=>this.setState({addModalShow:true})}>
Add Department
</Button>
<AddDepModal
show={this.state.addModalShow}
onHide={addModalClose}/>
</ButtonToolbar>
</div>
)
}
}
export default Department;
这是我在 Visual Studio 中实现的 Delete 方法:
public string Delete(int id)
{
try
{
DataTable table = new DataTable();
string query = @"
delete from dbo.Departments where DepartmentID = " + id;
using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["EmployeeAppDB"].ConnectionString))
using (var cmd = new SqlCommand(query, con))
using (var da = new SqlDataAdapter(cmd))
{
cmd.CommandType = CommandType.Text;
da.Fill(table);
}
return "Deleted Successfully";
}
catch (Exception)
{
return "Failed to Delete";
}
}
}
}
我在我的 sql 表中使用 EmployeeAppDB 表
我不确定为什么我无法删除。窗口弹出来但是删除功能没有发生。请帮忙
解决方案
1-如果您的意思是请求未到达服务器端:
a: 我建议将 fetch 和 server 端的 http 动词更改为“Post”,然后再试一次
b: 看来你的 fetch 有问题,使用下面的链接添加 then 和 catch 函数来获取: fetch 示例
2-如果您的意思是在服务器端选择成功后不删除客户端,我看不到任何用于在获取后更新状态的日志(您应该从 deps 中删除项目并使用 this.setState 更新状态)
推荐阅读
- javascript - 按对象的子数组最大日期对对象数组进行排序
- reactjs - 与 Redux Thunk 结合使用时,React Native Redux Persist 不持久化状态
- asp.net-core-mvc - 公共托管服务器中的 ASP.NET Core 3.1 部署问题
- bash - 根据特定字符查找和重命名目录
- cocoa - NSApplication 箭头键的响应链
- python - How to graph a list with days ( y: number of days x:day)
- android - How to send data from Multiple Fragments to single activity on FAB button click
- java - I'm getting a GnssLocationProvider: 'Wakelock acquired' followed by 'Wakelock released' logcat error on my android studio app
- reactjs - Unit testing in Kotlin JS
- python - Django:Child Model Fields Not Rendering