删除检查项
完善页面#
为了防止用户误操作,点击删除按钮时需要弹出确认删除的提示,用户点击取消则不做任何操作,用户点击确定按钮再提交删除请求。
绑定单击事件#
需要为删除按钮绑定单击事件,并且将当前行数据作为参数传递给处理函数
<el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
// 删除 handleDelete(row) { alert(row.id); }
弹出确认操作提示#
用户点击删除按钮会执行handleDelete方法,此处需要完善handleDelete方法,弹出确认提示信息。ElementUI提供了$confirm方法来实现确认提示信息弹框效果
// 删除 handleDelete(row) { //alert(row.id); this.$confirm("确认删除当前选中记录吗?","提示",{type:'warning'}).then(()=>{ //点击确定按钮时只需此处代码 alert('用户点击的是确定按钮'); }); }
发送请求#
如果用户点击确定按钮就需要发送ajax请求,并且将当前检查项的id作为参数提交到后台进行删除操作
// 删除 handleDelete(row) { //alert(row.id); this.$confirm("确认删除吗?","提示",{type:'warning'}).then(()=>{ //点击确定按钮时只需此处代码 //alert('用户点击的是确定按钮'); axios.get("/checkitem/delete.do?id=" + row.id).then((res)=> { if(!res.data.flag){ //删除失败 this.$message.error(res.data.message); }else{ //删除成功 this.$message({ message: res.data.message, type: 'success' }); //调用分页,获取最新分页数据 this.findPage(); } }); }); }
后台代码#
Controller#
在CheckItemController中增加删除方法
//删除 @RequestMapping("/delete") public Result delete(Integer id){ try { checkItemService.delete(id); }catch (RuntimeException e){ return new Result(false,e.getMessage()); }catch (Exception e){ return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL); } return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS); }
服务接口#
在CheckItemService服务接口中扩展删除方法
public void delete(Integer id);
服务实现类#
注意:不能直接删除,需要判断当前检查项是否和检查组关联,如果已经和检查组进行了关联则不允许删除
//删除 public void delete(Integer id) throws RuntimeException{ //查询当前检查项是否和检查组关联 long count = checkItemDao.findCountByCheckItemId(id); if(count > 0){ //当前检查项被引用,不能删除 throw new RuntimeException("当前检查项被引用,不能删除"); } checkItemDao.deleteById(id); }
Dao接口#
在CheckItemDao接口中扩展方法findCountByCheckItemId和deleteById
public void deleteById(Integer id); public long findCountByCheckItemId(Integer checkItemId);
Mapper映射文件#
在CheckItemDao.xml中扩展SQL语句
<!--删除--> <delete id="deleteById" parameterType="int"> delete from t_checkitem where id = #{id} </delete> <!--根据检查项id查询中间关系表--> <select id="findCountByCheckItemId" resultType="long" parameterType="int"> select count(*) from t_checkgroup_checkitem where checkitem_id = #{checkitem_id} </select>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)