编辑检查项
完善页面#
用户点击编辑按钮时,需要弹出编辑窗口并且将当前记录的数据进行回显,用户修改完成后点击确定按钮将修改后的数据提交到后台进行数据库操作。
绑定单击事件#
需要为编辑按钮绑定单击事件,并且将当前行数据作为参数传递给处理函数
<el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
handleUpdate(row) {
alert(row);
}
弹出编辑窗口回显数据#
当前页面中的编辑窗口已经提供好了,默认处于隐藏状态。在handleUpdate方法中需要将编辑窗口展示出来,并且需要发送ajax请求查询当前检查项数据用于回显
// 弹出编辑窗口 handleUpdate(row) { //发送请求获取检查项信息 axios.get("/checkitem/findById.do?id=" + row.id).then((res)=>{ if(res.data.flag){ //设置编辑窗口属性,dialogFormVisible4Edit为true表示显示 this.dialogFormVisible4Edit = true; //为模型数据设置值,基于VUE双向数据绑定回显到页面 this.formData = res.data.data; }else{ this.$message.error("获取数据失败,请刷新当前页面"); } }); }
发送请求#
在编辑窗口中修改完成后,点击确定按钮需要提交请求,所以需要为确定按钮绑定事件并提供处理函数handleEdit
<el-button type="primary" @click="handleEdit()">确定</el-button>
//编辑 handleEdit() { //表单校验 this.$refs['dataEditForm'].validate((valid)=>{ if(valid){ //表单校验通过,发送请求 axios.post("/checkitem/edit.do",this.formData).then((response)=> { //隐藏编辑窗口 this.dialogFormVisible4Edit = false; if(response.data.flag){ //编辑成功,弹出成功提示信息 this.$message({ message: response.data.message, type: 'success' }); }else{ //编辑失败,弹出错误提示信息 this.$message.error(response.data.message); } }).finally(()=> { //重新发送请求查询分页数据 this.findPage(); }); }else{ //表单校验失败 this.$message.error("表单数据校验失败"); return false; } }); }
后台代码#
Controller#
在CheckItemController中增加编辑方法
//编辑 @RequestMapping("/edit") public Result edit(@RequestBody CheckItem checkItem){ try { checkItemService.edit(checkItem); }catch (Exception e){ return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL); } return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS); } @RequestMapping("/findById") public Result findById(Integer id){ try{ CheckItem checkItem = checkItemService.findById(id); return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem); }catch (Exception e){ e.printStackTrace(); //服务调用失败 return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL); } }
服务接口#
在CheckItemService服务接口中扩展编辑方法
public void edit(CheckItem checkItem); public CheckItem findById(Integer id);
服务实现类#
在CheckItemServiceImpl实现类中实现编辑方法
//编辑 public void edit(CheckItem checkItem) { checkItemDao.edit(checkItem); } public CheckItem findById(Integer id) { return checkItemDao.findById(id); }
Dao接口#
在CheckItemDao接口中扩展edit方法
public void edit(CheckItem checkItem); public CheckItem findById(Integer id);
Mapper映射文件#
在CheckItemDao.xml中扩展SQL语句
<!--编辑--> <update id="edit" parameterType="com.itheima.pojo.CheckItem"> update t_checkitem <set> <if test="name != null"> name = #{name}, </if> <if test="sex != null"> sex = #{sex}, </if> <if test="code != null"> code = #{code}, </if> <if test="age != null"> age = #{age}, </if> <if test="price != null"> price = #{price}, </if> <if test="type != null"> type = #{type}, </if> <if test="attention != null"> attention = #{attention}, </if> <if test="remark != null"> remark = #{remark}, </if> </set> where id = #{id} </update> <select id="findById" parameterType="int" resultType="com.itheima.pojo.CheckItem"> select * from t_checkitem where id = #{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新功能体验(一)