[size=large]是否采用dialog增加/修改bean信息根据项目需求而定,一般而言,若需添加/修改的信息较多或者其他因素则应跳转至相应页面进行操作,若添加/修改的信息较少,可采用这种方式。[/size]
[size=large]一、添加[/size]
[size=medium]1.编辑jsp
看起来比较乱,也不好排版,请见谅..[/size]
[size=large]2.编辑js[/size]
[size=medium]说明:
1.因为每次打开dialog打开的是上次dialog的缓存,所以在添加操作时要先清空表单中的值以及设定默认选项;
2.因为表单验证的关系--像这句“$("#_job").combobox('setValue','-1')”就会触发验证,此外提交表单也会触发表单验证,所以为了在打开dialog前清除验证效果,故而增加一个onBeforeOpen事件,在其中删除相关的验证css样式,这样,打开的dialoh显示的就是一个干净的界面。
3. $('#addOrEditForm').form("submit",{..})这个语句块是easyui的“form”中自带的方法,它会将表单提交的方式自动转为ajax提交。当然,也可以采用传统的ajax提交,那样会麻烦一些。[/size]
[size=large]3.编辑控制器[/size]
[size=medium]说明:1.这里的控制器采用的是struts2;2.这里需注意的是,前台采用的是ajax的提交方式,要让struts2以这种方式封装对象,需导入一个jar包:struts2-json-plugin-2.3.20.jar,在struts的配置文件中也需简单的设置一下,具体参见“4.编辑struts2配置文件”[/size]
[size=large]4.编辑struts2配置文件[/size]
[size=medium]非常简单,设置extends="json-default"即可。[/size]
[size=medium]大功告成,效果如下:[/size]
[img]http://dl2.iteye.com/upload/attachment/0112/1011/95ea1067-8687-366b-bb1b-ab352b98238e.png[/img]
[size=large]一、添加[/size]
[size=medium]1.编辑jsp
看起来比较乱,也不好排版,请见谅..[/size]
<!-- 1.页面引入样式 -->
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/jquery-easyui-1.4.2/themes/metro/easyui.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/jquery-easyui-1.4.2/themes/icon.css">
<!-- 2.页面引入脚本 -->
<script type="text/javascript"src="${pageContext.request.contextPath }/jquery-easyui-1.4.2/jquery-1.8.0.min.js"></script>
<script type="text/javascript"src="${pageContext.request.contextPath }/jquery-easyui-1.4.2/jquery.easyui.min.js"></script>
<script type="text/javascript"src="${pageContext.request.contextPath }/jquery-easyui-1.4.2/datagrid-detailview.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/jquery-easyui-1.4.2/locale/easyui-lang-zh_CN.js"></script>
<!-- 3.页面引入自定义脚本 -->
<script type="text/javascript" src="${pageContext.request.contextPath }/js/userManager.js" ></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/validate.js" ></script>
<!-- 4.页面引入其他脚本 -->
<link href="${pageContext.request.contextPath }/myDatePicker_2.2/datePicker.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="${pageContext.request.contextPath }/myDatePicker_2.2/jquery.datePicker-min.js"></script>
...
<form name="form1" id="form1">
<a href="#" onclick="obj.add()" id="add" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加用户</a>
<a href="#" onclick="obj.edit()" id="edit" class="easyui-linkbutton" iconCls="icon-edit" plain="true">修改用户</a>
<a href="#" id="save" onclick="obj.save()" class="easyui-linkbutton" iconCls="icon-save" plain="true">保存</a>
<a href="#" id="redo" onclick="obj.redo()" class="easyui-linkbutton" iconCls="icon-redo" plain="true">取消编辑</a>
</form>
...
<!-- 添加/修改用户 -->
<div id="mydialog" style="display: none;padding: 5px;width: 361px;height: 326px" >
<form id="addOrEditForm" name="addOrEditForm" method="post">
<table border="0" cellpadding="0" cellspacing="0" >
<tr id="idTr" style="display:none;"><td nowrap align="right" style="height: 35px">用户id:</td><td><input type="text"id="_eid" name="id" class="easyui-validatebox" value="${user.uid}"></td></tr>
<tr><td nowrap align="right" style="height: 35px">姓名:</td><td><input id="_ename" name="empl.ename" type="text" class="easyui-validatebox" data-options="required:true" validType="_ename[]"/></td></tr>
<tr><td nowrap align="right" style="height: 35px">性别:</td><td><input id="_Man" name="empl.sex" type="radio" value="男" checked="checked">男<input id="_Women" name="empl.sex" type="radio" value="女" >女</td></tr>
<tr><td nowrap align="right" style="height: 35px">出生日期:</td><td><input id="_birthday" name="empl.birthday" type="text" class="easyui-validatebox" data-options="required:true" plugin="datepicker" validType="_date[]" /></td></tr>
<tr><td nowrap align="right" style="height: 35px">入职日期:</td><td><input id="_hiredate" name="empl.hiredate" type="text" class="easyui-validatebox" data-options="required:true" plugin="datepicker" validType="_date['_hiredate',$('#_birthday').val()]" /></td></tr>
<tr><td nowrap align="right" style="height: 35px">职责:</td><td><input id="_reception" name="empl.admin" type="radio" value="0" checked="checked">前台<input id="_backstage" name="empl.admin" type="radio" value="1" >后台</td></tr>
<tr><td nowrap align="right" style="height: 35px" >职位:</td><td><select id="_job" name="empl.job" class="easyui-combobox" panelHeight="auto" validType="_job[]">
<option selected value="-1">请选择该用户所属职位</option>
<c:forEach items="${job }" var="v">
<option value="${v.jid}">${v.jname }</option>
</c:forEach>
</select></td></tr>
<tr><td nowrap align="right" style="height: 35px" >所属部门:</td><td><select id="_dept" name="empl.deptno" class="easyui-combobox" panelHeight="auto" validType="_dept[]" >
<option selected value="-1">请选择该用户所属部门</option>
<c:forEach items="${dept }" var="v">
<option value="${v.did }">${v.dname }</option>
</c:forEach>
</select></td></tr>
</table>
</form>
</div>
[size=large]2.编辑js[/size]
[size=medium]说明:
1.因为每次打开dialog打开的是上次dialog的缓存,所以在添加操作时要先清空表单中的值以及设定默认选项;
2.因为表单验证的关系--像这句“$("#_job").combobox('setValue','-1')”就会触发验证,此外提交表单也会触发表单验证,所以为了在打开dialog前清除验证效果,故而增加一个onBeforeOpen事件,在其中删除相关的验证css样式,这样,打开的dialoh显示的就是一个干净的界面。
3. $('#addOrEditForm').form("submit",{..})这个语句块是easyui的“form”中自带的方法,它会将表单提交的方式自动转为ajax提交。当然,也可以采用传统的ajax提交,那样会麻烦一些。[/size]
obj = {
//在mydialog添加员工信息。
add:function(){
//权限验证
if(currUserStatus!=0){$.messager.alert('警告', '您不是管理员,无权执行此操作!', 'warning');return}
//清空#addOrEditForm中的输入框的值
$('#_eid').val('');
$("#_ename").val('');
$("#_Man").attr("checked","true");//性别默认选中男性
$("#_birthday").val('');
$("#_hiredate").val('');
$("#_reception").attr("checked","true");//职责默认选中前台
//设置下拉框的默认选项
$("#_job").combobox('setValue','-1')
$("#_dept").combobox('select','-1');
//隐藏用户编号行
$('#idTr').hide();
//显示并设置mydialog
$('#mydialog').show().dialog({
collapsible:true,
iconCls:'icon-add',
title:' 添加用户',
height:336,
width:390,
cache:false,
/**
* 取消校验标识。这并不影响最后提交表单的校验。
*/
onBeforeOpen:function(){
$("#_ename").removeClass('validatebox-invalid');
$("#_Man").removeClass('validatebox-invalid');
$("#_birthday").removeClass('validatebox-invalid');
$("#_hiredate").removeClass('validatebox-invalid');
$("#_reception").removeClass('validatebox-invalid');
$("#_job").next().removeClass('textbox-invalid');
$("#_dept").next().removeClass('textbox-invalid');
$("#_job").next().children(":text").removeClass('validatebox-invalid');
$("#_dept").next().children(":text").removeClass('validatebox-invalid');
},
buttons:[//底部按钮
{
text:'提交',
type:'submit',
iconCls:'icon-ok',
handler: function(data){
/**
* 将表单转为ajax提交
*/
$('#addOrEditForm').form("submit",{
url:'emplAction!add.action',
onSubmit:function(){
return $(this).form('validate');
},
success:function(data){
$.messager.show({
title : '提示',
msg : data ,
});
$('#mydialog').dialog('close');
$('#dg').datagrid('load');
$('#dg').datagrid('loaded');
}
});
}
},
{
text:'取消',
iconCls:'icon-cancel',
handler:function(){
$('#mydialog').dialog('close');
}
}
],
onClose: function () {
//解决弹出窗口关闭后,验证消息还显示在最上面
$('.validatebox-tip').remove();
}
}).dialog('open');
//取消当前表格的所有选中行
$('#dg').datagrid('unselectAll');
},
//在mydialog修改员工信息。
edit:function(){
if(currUserStatus!=0){$.messager.alert('警告', '您不是管理员,无权执行此操作!', 'warning');return}
//获取选择行
var rows = $('#dg').datagrid('getSelections');
$('#idTr').show()
if(rows.length == 1){
//设置#addOrEditForm中的输入框的值
//设置eid并设置其不可编辑
$("#_eid").val(rows[0].eid);
$('#_eid').attr('readonly','readonly');
$("#_eid").css("background-color",'#D8DFDD')
$("#_ename").val(rows[0].ename);
rows[0].sex=='男'?$("#_Man").attr("checked","true"):$("#_Women").attr("checked","true");
$("#_birthday").val(rows[0].birthday);
$("#_hiredate").val(rows[0].hiredate);
rows[0].admin=='0'?$("#_reception").attr("checked","true"):$("#_backstage").attr("checked","true");
//设置下拉框的默认选项
$("#_job").combobox('select',rows[0].jid)
$("#_dept").combobox('select',rows[0].did)
//显示mydialog
$('#mydialog').show();
$('#mydialog').dialog({
collapsible:true,
iconCls:'icon-edit',
title:' 修改用户',
height:370,
width:390,
onBeforeOpen:function(){
$("#_ename").removeClass('validatebox-invalid');
$("#_Man").removeClass('validatebox-invalid');
$("#_birthday").removeClass('validatebox-invalid');
$("#_hiredate").removeClass('validatebox-invalid');
$("#_reception").removeClass('validatebox-invalid');
$("#_job").next().removeClass('textbox-invalid');
$("#_dept").next().removeClass('textbox-invalid');
$("#_job").next().children(":text").removeClass('validatebox-invalid');
$("#_dept").next().children(":text").removeClass('validatebox-invalid');
},
buttons:[
{
text:'提交',
iconCls:'icon-ok',
handler: function(data){
/**
* 将表单转为ajax提交
*/
$('#addOrEditForm').form("submit",{
url:'emplAction!edite.action',
onSubmit:function(){
return $(this).form('validate');
},
success:function(data){
$.messager.show({
title : '提示',
msg : data ,
});
$('#mydialog').dialog('close');
$('#dg').datagrid('load');
$('#dg').datagrid('loaded');
}
});
}
},
{
text:'取消',
iconCls:'icon-cancel',
handler:function(){
$('#mydialog').dialog('close');
}
}
]
});
//取消当前表格的所有选中行
$('#dg').datagrid('unselectAll');
}else{
$.messager.alert('警告', '修改必须选中仅且一行!', 'warning');
}
}
}
[size=large]3.编辑控制器[/size]
[size=medium]说明:1.这里的控制器采用的是struts2;2.这里需注意的是,前台采用的是ajax的提交方式,要让struts2以这种方式封装对象,需导入一个jar包:struts2-json-plugin-2.3.20.jar,在struts的配置文件中也需简单的设置一下,具体参见“4.编辑struts2配置文件”[/size]
//添加用户
public void add() throws IOException{
empl.setPassword("123");
empl.setStatus(1);//新添员工状态为“试用期”
boolean b = empService.addEmpl(empl);
PrintWriter out = resp.getWriter();
if(b){
out.print("success");
}else{
out.print("fail");
}
}
//修改用户
public void edite() throws IOException{
boolean b = empService.editEmpl(empl, id);
PrintWriter out = resp.getWriter();
if(b){
out.print("success");
}else{
out.print("fail");
}
}
[size=large]4.编辑struts2配置文件[/size]
[size=medium]非常简单,设置extends="json-default"即可。[/size]
<package name="mypackage" extends="json-default" >
<action name="emplAction" class="com.chen.action.EmplAction">
<result name="tolist" type="redirectAction">emplAction!listEmpl.action</result>
<result name="false">/login.jsp</result>
<result name="list">/WEB-INF/jsp/userManager.jsp</result>
<!-- 显示员工详情的地址 -->
<result name="userDetail">/WEB-INF/jsp/userDetail.jsp</result>
<result name="toAddEmpl">/WEB-INF/jsp/addEmpl.jsp</result>
<result name="toEditEmpl" type="dispatcher">/WEB-INF/jsp/editEmpl.jsp</result>
</action>
</package>
[size=medium]大功告成,效果如下:[/size]
[img]http://dl2.iteye.com/upload/attachment/0112/1011/95ea1067-8687-366b-bb1b-ab352b98238e.png[/img]