A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 我是楠楠 于 2019-10-23 16:13 编辑

【郑州校区】传智健康项目讲义第二章预约管理-检查项管理八
6. 编辑检查项
6.1 完善页面
用户点击编辑按钮时,需要弹出编辑窗口并且将当前记录的数据进行回显,用户修改完成后点击确定按钮将修改后的数据提交到后台进行数据库操作。
6.1.1 绑定单击事件
需要为编辑按钮绑定单击事件,并且将当前行数据作为参数传递给处理函数
[AppleScript] 纯文本查看 复制代码
<el‐button type="primary" size="mini" @click="handleUpdate(scope.row)">编
辑</el‐button>

[AppleScript] 纯文本查看 复制代码
handleUpdate(row) {
alert(row);
} 

6.1.2 弹出编辑窗口回显数据当前页面中的编辑窗口已经提供好了,默认处于隐藏状态。在handleUpdate方法中需要
将编辑窗口展示出来,并且需要发送ajax请求查询当前检查项数据用于回显

[AppleScript] 纯文本查看 复制代码
// 弹出编辑窗口
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("获取数据失败,请刷新当前页面");
}
});
} 

6.1.3 发送请求
在编辑窗口中修改完成后,点击确定按钮需要提交请求,所以需要为确定按钮绑定事件并提供处理函数handleEdit

[AppleScript] 纯文本查看 复制代码
<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;
}
});
}

6.2 后台代码
6.2.1 Controller
CheckItemController中增加编辑方法

[AppleScript] 纯文本查看 复制代码
//编辑
@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);
}
} 

6.2.2 服务接口
CheckItemService服务接口中扩展编辑方法

[AppleScript] 纯文本查看 复制代码
public void edit(CheckItem checkItem);
public CheckItem findById(Integer id); 

6.2.3 服务实现类
CheckItemServiceImpl实现类中实现编辑方法

[AppleScript] 纯文本查看 复制代码
//编辑
public void edit(CheckItem checkItem) {
checkItemDao.edit(checkItem);
}
public CheckItem findById(Integer id) {
return checkItemDao.findById(id);
} 

6.2.4 Dao接口
CheckItemDao接口中扩展edit方法

[AppleScript] 纯文本查看 复制代码
public void edit(CheckItem checkItem);
public CheckItem findById(Integer id); 

6.2.5 Mapper映射文件
CheckItemDao.xml中扩展SQL语句

[AppleScript] 纯文本查看 复制代码
<!‐‐编辑‐‐>
<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>


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马