删除功能
delete() {
//先判断有没有选中
var row = employeeDataGrid.datagrid("getSelected");
//没有选中就给出提示
if (!row) {
$.messager.alert("提示", "请选中", "info");
return;
}
//选中就让用户是否确认删除
$.messager.confirm('确认删除', '真的狠心删除我吗', function (r) {
if (r) {
//确认删除直接访问后台
$.get("/employee/delete", {id: row.id}, function (result) {
if (result.success) {
//成功后刷新
employeeDataGrid.datagrid("reload");
} else {
$.messager.alert("提示", `出错了,原因是:${result.msg}`, "error");
}
})
}
})
}
添加的时候显示密码,并启用
修改的时候隐藏密码,并禁用
添加功能
引入验证
<%--验证扩展的样式与js引入--%>
<link rel="stylesheet" type="text/css" href="/easyui/plugin/validatebox/jeasyui.extensions.validatebox.css">
<script src="/easyui/plugin/validatebox/jeasyui.extensions.validatebox.rules.js"></script>
添加
add() {
//把所有带data-show的元素显示起来
$("*[data-show]").show();
//开启验证功能
$("*[data-show] input").validatebox("enable");
//打开对话框
employeeDialog.dialog("center").dialog("open");
//把form中的数据清空
employeeForm.form("clear");
},
自定义验证
//定义我们自己的规则(验证重复)
$.extend($.fn.validatebox.defaults.rules, {
checkName: {
//验证规则 value:表单中的值 params:规则中传过来的值(数组形式)
validator: function(value, param){
//拿到相应的id
var employeeId = $("#employeeId").val();
//使用同步的方式进行查询
var isSuccess = $.util.requestAjaxBoolean('/employee/checkUsername',
{id:employeeId,username:value});
//使用同步的方式进行Ajax请求
return isSuccess;
},
//验证失败的提示
message: '用户名已经被占用!'
}
});
修改功能
edit() {
var row = employeeDataGrid.datagrid("getSelected");
if (!row) {
$.messager.alert("提示", "请选中", "info");
return;
}
//把有data-show的所有的元素隐藏起来
$("*[data-show]").hide();
//让验证的失效
$("*[data-show] input").validatebox("disable");
//打开对话框
employeeDialog.dialog("center").dialog("open");
//把form表单中的数据清空
employeeForm.form("clear");
if (row.department) {
row["department.id"] = row.department.id;
}
//完成回显
employeeForm.form("load", row);
}
数据丢失问题
@ModelAttribute("editEmployee")//odelAttribute : 在路径访问这个方法的时候会先执行它
public Employee beforeEdit(Long id,String cmd){
//修改的时候才查询(只要有id会就进行一次查询,这是不对的)
if(id!=null && "update".equals(cmd)) {
Employee dbEmp = employeeService.findOne(id);
//把要传过来的关联对象都清空,就可以解决n-to-n的问题
dbEmp.setDepartment(null);
return dbEmp;
}
return null;
}
//这里的ModelAttribute和上面的名称是对应上的
@RequestMapping("/update")
@ResponseBody
public JsonResult update(@ModelAttribute("editEmployee")Employee employee){
return saveOrUpdate(employee);
}

7949

被折叠的 条评论
为什么被折叠?



