js中showModalDialog函数用法

showModalDialog用法:
1.jsp页面(父窗体页面)
function openW_(ID){
var time =new Date().getTime();
//关于加上那个 (new Data()) 是要避免showModalDialog页面自动缓存的问题,导致第二次打开页面,数据没有被刷新,这是因为showModalDialog页面如果每次的URL一样的话,它会自动显示以前在缓存里面的数据。

var url = 'itsmOndutyPlanWork-selectUser.action?date='+time+'&onduty_plan_id='+ID;
window.showModalDialog(url, self , "dialogHeight:600px; dialogWidth:400px");
}
//刷新当前页面
function loadDiv(){
var month = document.getElementById('month').value;
document.getElementById('itsmOndutyPlanWorkPage.month').value=month;
window.document.fm.submit(); //提交父窗体
<td class="centerContentTd">
<input type="button" value="修 改" class="mainButton" οnclick="openW_('${b.onduty_plan_id}');" >
</td>

2.action

public String selectUser()
{
String onduty_plan_id = this.httpServletRequest.getParameter("onduty_plan_id");
String ondutyUserIds = itsmOndutyPlanWorkService.getOndutyUserIds(onduty_plan_id);// 值班人ID串
String ondutyScheduleId=itsmOndutyPlanWorkService.getOndutyScheduleId(onduty_plan_id);
List<Map> list = itsmOndutyPlanWorkService.getUserListByScheduleId(ondutyScheduleId);
if (null != list && list.size() > 0) {
for (Map map : list) {
String USER_ID = String.valueOf(map.get("USER_ID"));

if (null != ondutyUserIds && !"".equals(ondutyUserIds)) {// 已设置值班人了
if (ondutyUserIds.indexOf(USER_ID) > -1) {// 如果包含某用户
map.put("isCheck", true);
} else {
map.put("isCheck", false);
}
} else {// 值班人为null
map.put("isCheck", false);
}
}// end for
}
this.httpServletRequest.setAttribute("list", list);

this.httpServletRequest.setAttribute("onduty_plan_id", onduty_plan_id);
return Action.SUCCESS;
}
3.selectUser.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/app.tld" prefix="a"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<a:head/>
<base target="_self">
<head>
<title>用户列表</title>

<SCRIPT type="text/javascript">
function subForm(){

var is_Have_Checked = false;//
var userIdBox = document.fm.userIdBox;
if(userIdBox){//存在时
var isUserIdBox_Len = userIdBox.length;
if(isUserIdBox_Len > 0){//多个时
//
for(var i=0 ; i<isUserIdBox_Len; i++){
if(userIdBox[i].checked){
is_Have_Checked = true;
break;
}
}
if(is_Have_Checked==false){
alert("还没有选择用户!");
userIdBox[0].focus();
return;
}

}else{//一个时
if(document.fm.userIdBox.checked = false){
alert("还没有选择用户!");
document.fm.userIdBox.focus();
return;
}
}
}else{//不存在时

}
fm.submit();
}
</SCRIPT>
</head>
<body οnlοad="general.addNbsp();" scroll="auto">
<form action="itsmOndutyPlanWork-editOnDutyUse.action" name="fm" method="post">
<input type="hidden" name="onduty_plan_id" value="${onduty_plan_id}">
<table class="queryResult">
<tr>
<td height="100%" valign="top">
<table class="fixed">
<tr class="fixedHeaderTr">
<td class="centerListTitle" width="5%">
<input type="checkBox" οnclick="general.selectAll()" />
</td>

<td class="centerListTitle">
<s:text name="用户名称"></s:text>
</td>
</tr>
<s:iterator value="#request.list" id="b">
<tr class="contentTr" οnmοuseοver="javascript:general.overColor();" οnmοuseοut="javascript:general.outColor();">
<td class="centerContentTd" width="5%">
<s:if test="#b.isCheck==true">
<input type="checkBox" name="userIdBox" value="${b.USER_ID}" checked="checked" οnclick="general.setAllBox()" />
</s:if>
<s:else>
<input type="checkBox" name="userIdBox" value="${b.USER_ID}" οnclick="general.setAllBox()" />
</s:else>
</td>
<td class="centerContentTd">
${b.USER_NAME}
</td>
</tr>
</s:iterator>
</table>
</td>
</tr>
<tr>
<td colspan="2" align="center" height="30"><a:button text="提 交" οnclick="subForm();" /> &nbsp;&nbsp; <a:button text="关 闭" οnclick="window.close();" /></td>
</tr>

</table>
</form>
</body>
</html>
<a:foot/>

4.action
public void editOnDutyUse() {
String onduty_plan_id = this.httpServletRequest.getParameter("onduty_plan_id");
String[] userIdBox = this.httpServletRequest.getParameterValues("userIdBox");
String userIds = StringUtils.join(userIdBox, ";");
System.out.println("onduty_plan_id == " + onduty_plan_id);
System.out.println("userIds == " + userIds);
itsmOndutyPlanWorkService.editOnDutyUse(userIds, onduty_plan_id);
this.outPrintStrToPage("<script> window.close(); window.dialogArguments.loadDiv();</script>");
//执行完子窗体,子窗体提交后刷新父窗体,调用了父窗体的loadDiv()脚本方法,提交父窗体的表单

}

--------------------------------------------------------------------------------------------------------------------------------------------
在web开发中,经常会遇到需要弹出一个子窗口的情况,并且子窗口和父窗口之间需要保持良好的通信。如弹出一个子查询窗口,并把查询出的某项信息填充到父窗口的表单中,这时使用window对象的showModalDialog打开一个模态对话框来实现这一功能是非常有用的。
下面是一个简单示例

// src.html
<script type="text/javascript">
window.showModalDialog("des.html");
</script>

// des.html
<script type="text/javascript">
alert("this is des.html");
</script> 2. 为使打开的模态对话框的外观满足我们的要求,必须要设置对话框的样式。该方法的第三个参数可以用来设置我们需要的外观样式。

<script type="text/javascript">
window.showModalDialog("des.html", "", "dialogWidth: 600px; dialogHeight: 600px");
</script> 样式属性的设置是键值对的形式进行的,多个属性设置之间用分号“;”隔开。

可以设置的样式属性如下:

dialogHeight :对话框高度,不小于100pxpx,IE4中dialogHeight 和 dialogWidth 默认的单位是em,而IE5中是px,为方便其见,在定义modal方式的对话框时,用px做单位。
dialogWidth: 对话框宽度。
dialogLeft: 离屏幕左的距离。
dialogTop: 离屏幕上的距离。
center: {yes | no | 1 | 0 }:窗口是否居中,默认yes,但仍可以指定高度和宽度。
help: {yes | no | 1 | 0 }:是否显示帮助按钮,默认yes。
resizable: {yes | no | 1 | 0 } [IE5+]:是否可被改变大小。默认no。
status: {yes | no | 1 | 0 } [IE5+]:是否显示状态栏。默认为yes[ Modeless]或no[Modal]。
scroll:{ yes | no | 1 | 0 | on | off }:指明对话框是否显示滚动条。默认为yes。
下面几个属性是用在HTA中的,在一般的网页中一般不使用。
dialogHide:{ yes | no | 1 | 0 | on | off }:在打印或者打印预览时对话框是否隐藏。默认为no。
edge:{ sunken | raised }:指明对话框的边框样式。默认为raised。
unadorned:{ yes | no | 1 | 0 | on | off }:默认为no。
3. 但是最重要的在于如何向模态对话框中传递参数了,相信很多朋友都发现在上面的例子中showModalDialog方法的第二个参数为空字符串,其实我们就是通过它把信息传递到模态对话框中的。



// src.html
<script type="text/javascript">
window.showModalDialog("des.html", "my name is tuozixuan", "dialogWidth: 600px; dialogHeight: 600px");
</script>

在模态对话框窗体中需要用到window对象的dialogArguments属性来获取传递过来的参数(中英文字符都可)。

// des.html
<script type="text/javascript">
var msg = window.dialogArguments
alert(msg);
</script> 在向模态对话框传递参数时,不仅可以是字符串,还可以是Number、数组、对象等类型。

数字类型
// src.html
<script type="text/javascript">
window.showModalDialog("des.html", 1, "dialogWidth: 600px; dialogHeight: 600px");
</script>

// des.html
<script type="text/javascript">
var msg = window.dialogArguments
alert(msg);
</script>
数组类型
// src.html
<script type="text/javascript">
window.showModalDialog("des.html", [1,2,3], "dialogWidth: 600px; dialogHeight: 600px");
</script>

// des.html
<script type="text/javascript">
var msg = window.dialogArguments
alert(msg[2]);
</script> 对象类型
// src.html
<script type="text/javascript">
var obj = new Object();
obj.name = "tuozixuan";
obj.age = 1000;
window.showModalDialog("des.html", obj, "dialogWidth: 600px; dialogHeight: 600px");
</script>

// des.html
<script type="text/javascript">
var msgObj = window.dialogArguments;
alert(msgObj.name + " " + msgObj.age);
</script> 4. 模态对话框向父窗口传递信息

如果需要把模态对话框中的数据传递给父窗口中,需要用到模态对话框中window对象的returnValue属性。当用户关闭模态对话框时,浏览器将会把returnValue属性的值返回给父窗口,若该属性未设置,则返回undefined。

// src.html
<script type="text/javascript">
var obj = new Object();
obj.name = "tuozixuan";
obj.age = 1000;
var resultData = window.showModalDialog("des.html", obj, "dialogWidth: 600px; dialogHeight: 600px");
alert(resultData);
</script>

// des.html
<script type="text/javascript">
window.returnValue = "hello";
</script> 其中window对象的returnValue属性可以设置为数字、字符串、数字、对象等数据类型,情况和从父窗口向模态对话框中传递数据类似,在此不再赘述。



5. 模态对话框调用父窗口的js方法

可以把父窗口的window对象作为参数传递给模态对话框,模态对话框获取到参数对象的句柄后就可以调用父窗口的js方法了。

// src.html
<script type="text/javascript">
window.showModalDialog("des.html", window, "dialogWidth: 600px; dialogHeight: 600px");

function showMsg()
{
alert("飞雪连天射白鹿,笑书神侠倚碧鸳");
}
</script>

// des.html
<script type="text/javascript">
window.dialogArguments.showMsg();
</script>


当我们用showModelessDialog()打开窗口时,不必用window.close()去关闭它
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值