aisell-easyui-CRUD总结2

1员工Employee的CRUD

1.1员工的删除

删除:

​ (1)选中表格里面一行数据,如果没有选择提示选中数据

(2) 选中数据之后,在发送ajax请求删除数据库数据

​ (3)重新加载表格

del:function(){
            //判断表格里面是否选中得数据
            var row = employeeGrid.datagrid('getSelected');
            if(row){
                //是否确认要删除数据
                $.messager.confirm('温馨提示','你确定要删除吗?',function(value){
                    if(value){
                        //获取id值

                        //发送ajax到后台进行删除数据
                        $.get('/employee/delete',{"id":row.id},function(data){
                            //返回json对象
                            if(data.success){
                                $.messager.alert('温馨提示:','删除成功','info');
                                //重新加载数据
                                employeeGrid.datagrid('reload');
                            }else{
                                $.messager.alert('温馨提示:','删除失败'+data.msg,'error');
                            }
                        });
                    }
                })

            }else{
                //提示用户
                $.messager.alert('温馨提示:','请选中一条数据进行删除','info');
                return;
            }

1.2 新增和修改保存

思路:

​ 弹出对话框

​ 填写信息

​ 调用save方法–>发送form表单提交

 save:function(){
            var url = "/employee/save";
            //获取隐藏域里面id值
            var id = $("#employeeId").val();
            if(id){
                url = "/employee/update?cmd=update"
            }
            //保存方法 --提交表单的数据到后台
            employeeForm.form('submit', {
                    url:url,
                    onSubmit: function(){
                        // 提交之前的验证
                       return employeeForm.form('validate');
                     },
                success:function(data){
                        //字符串 转换成json对象
                      var dataJson =  $.parseJSON(data);
                      if(dataJson.success){
                          $.messager.alert('温馨提示:','操作成功','info');
                          //重新加载数据
                          employeeGrid.datagrid('reload');
                          //关闭对话框
                          employeeDialog.dialog('close');
                      }else{
                          $.messager.alert('温馨提示:','保存失败'+dataJson.msg,'error');
                          employeeDialog.dialog('close');
                      }
                }
            });
        }

1.3 处理验证问题

密码和用户名是否匹配的问题

//自己去扩展easyui验证的功能
$.extend($.fn.validatebox.defaults.rules, {
    equals: {
        validator: function(value,param){
            //返回true 验证通过 不显示信息
            return value == $(param[0]).val();
        },
        message: '密码不匹配.'
    }
});

//扩展用户名是否重复的功能
$.extend($.fn.validatebox.defaults.rules, {s
    checkUsername: {
        validator: function(value,param){
            //当然输入框value 和数据库的username进行比较
            //发送ajax请求 --异步(ajax同步)
            var id = $("#employeeId").val();
           var result =  $.ajax({
                type: "POST",
                url: "/employee/checkUsername",
                data: "username="+value+"&id="+id,
                async: false //同步
            }).responseText;
            //返回true 验证通过 不显示信息
           // alert(result);
            return result == 'true';
        },
        message: '用户名不能重复.'
    }
});

1.4 处理数据丢失问题

丢失方案:

​ (1)采用隐藏域 --安全性不高

​ (2)使用jpa的注解配置 – updatable=true 在更新的时候不会去更新

​ (3)springmvc的注解ModelAttribute

​ (4)手动去模拟ModelAttribute

猜你喜欢

转载自blog.csdn.net/weixin_44671176/article/details/98544633