删除和增加都差不多,不过还是遇到一点问题,1.下面的
$.post('user/removeUser',{id:row.userId},function(result)
要注是row.userId,而不是原来的row.id ,其名称要和field字段是要一致的!!这个函数就是向后台发送user/removeUser请求,其中还传了一个参数id,它的值是row.userId
function removeUser(){
var row = $('#dg').datagrid('getSelected');
if (row){
$.messager.confirm('Confirm','Are you sure you want to remove this user?',function(r){
if (r){
$.post('user/removeUser',{id:row.userId},function(result){
if (result.success){
$('#dg').datagrid('reload'); // reload the user data
} else {
$.messager.show({ // show error message
title: 'Error',
msg: result.msg
});
}
},'json');
}
});
}
}
后台处理:
@RequestMapping("/removeUser")
public String removeUser(HttpServletRequest request,HttpServletResponse response) throws Exception{
Json json = new Json();//用于向前端发送消息
String userId=request.getParameter("id");
try{
userService.delete((User)userService.getById(userId));
json.setMsg("删除成功!");
json.setSuccess(true);
writeJson(json,response);
return null;
}catch (Exception e){
json.setMsg("删除失败!"+e.getMessage());
writeJson(json,response);
return null;
}
}
其中
userService.delete((User)userService.getById(userId));
要注意userService.getById前面要加上(User)