1.前端页面:
<button class="btn btn-warning updateProType" onclick="modifyName()">修改</button>
function modifyName(){
$.ajax({
type:'post',
url:'${pageContext.request.contextPath}/backend/productType/modifyName',
data:{'id': $('#proTypeNum').val(),'name':$('#proTypeName').val()},
dataType:'json',
success:function(result){
if(result.status==1){
layer.msg(result.message,{
time:2000,
skin:'successMsg'
},function(){
//关闭以后重新加载数据
location.href='${pageContext.request.contextPath}/backend/productType/findAll?pageNum='+${pageInfo.pageNum};
})
}else{
layer.msg(result.message,{
time:2000,
skin:'errorsMsg'
})
}
}
});
}
2.Controller
@RequestMapping("/modifyName")
@ResponseBody
public ResponseResult modifyName(int id,String name){
try {
productTypeService.modifyName(id,name);
return ResponseResult.success("修改成功");
} catch (ProductTypeExistException e) {
//e.printStackTrace();
return ResponseResult.fail(e.getMessage());
}
}
3.Service
@Override
public void modifyName(int id, String name) throws ProductTypeExistException {
ProductType productType = productTypeDao.selectByName(name);
if(null!=productType){
throw new ProductTypeExistException("商品类型名称已存在");
}
productTypeDao.updateName(id,name);
}
4.Dao
public void updateName(@Param("id") int id, @Param("name") String name);
mapper.xml
<update id="updateName">
update t_product_type
set name=#{name}
where id=#{id}
</update>
删除功能:
1.前端
删除的按钮
<input type="button" class="btn btn-warning btn-sm doProTypeDelete" onclick="showDeleteModal(${productType.id})" value="删除">
点击后,利用js函数弹出删除面板
//显示确认删除的提示 点击后将展开提示栏
function showDeleteModal(id){
$('#deleteProductTypeId').val(id);
$('#deleteProductTypeModal').modal('show');
}
删除的面板html代码:
<!-- 确认删除 start -->
<div class="modal fade" tabindex="-1" id="deleteProductTypeModal">
<!-- 窗口声明 -->
<div class="modal-dialog">
<!-- 内容声明 -->
<div class="modal-content">
<!-- 头部、主体、脚注 -->
<div class="modal-header">
<button class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">提示消息</h4>
</div>
<div class="modal-body text-center">
<h4>确认要删除该商品类型吗?</h4>
</div>
<div class="modal-footer">
<input type="hidden" id="deleteProductTypeId">
<button class="btn btn-primary updateProType" onclick="deleteProductType()" data-dismiss="modal">删除</button>
<button class="btn btn-primary cancel" data-dismiss="modal">取消</button>
</div>
</div>
</div>
</div>
确定删除的js代码:
//删除商品类型
function deleteProductType(){
$.get(
'${pageContext.request.contextPath}/backend/productType/removeById',
{'id':$('#deleteProductTypeId').val()},
function(result){
if(result.status==1){
layer.msg('删除成功',{
time:2000,
skin:'successMsg'
},function(){
//重新加载数据
location.href='${pageContext.request.contextPath}/backend/productType/findAll?pageNum='+${pageInfo.pageNum};
})
}else{
layer.msg('删除失败',{
time:2000,
skin:'errorsMsg'
})
}
}
);
}
2.Controller:
@RequestMapping("/removeById")
@ResponseBody
public ResponseResult removeById(int id){
productTypeService.removeById(id);
return ResponseResult.success();
}
service、dao、mapper省略