描述:在文档库中,“Add Shortcut”会弹出一个先选择Group,后选择文档的对话框;
实现机制:
在document_library/edit_file_shortcut.jsp中
<input type="button" value="<liferay-ui:message key="select" />" onClick="var toGroupWindow = window.open('<portlet:renderURL windowState="<%= LiferayWindowState.POP_UP.toString() %>"><portlet:param name="struts_action" value="/document_library/select_group" /></portlet:renderURL>', 'toGroup', 'directories=no,height=640,location=no,menubar=no,resizable=yes,scrollbars=no,status=no,toolbar=no,width=680'); void(''); toGroupWindow.focus();" /> |
在onClick事件中,打开一个窗口,该窗口打开一个renderURL,该URL会执行对应的Action.render方法,find the right forwarding, 最终打开select_group.jsp页面。为什么不直接打开select_group.jsp页面呢?这样可以用liferay的方式来进行参数传递,还可以指定窗口的模式;
需要注意的是使用renderURL时,引号的使用方法。一般是:在外围用但引号,里面正常的使用双引号, <portlet:renderURL>中的双引号与html input中的双引号不会冲突,因为系统会将<Portlet:renderURL>解析为对应的http://xxxxxxx字符串。
执行正确的话,上面会打开弹出窗口。
在select_xxxx.jsp页面中,查询到一个list之后,会通过:
StringMaker sm = new StringMaker();
sm.append("javascript: opener.");
sm.append(renderResponse.getNamespace());
sm.append("selectGroup('");
sm.append(group.getGroupId());
sm.append("', '");
sm.append(UnicodeFormatter.toString(group.getName()));
sm.append("'); window.close();");
String rowHREF = sm.toString();
来调用父窗口中的selectXxxx JS 代码,向父窗口传递一个id参数,然后将本窗口关闭。
在父窗口(edit_file_shortcut.jsp)中,声明JS function如下:
function <portlet:namespace />selectGroup(groupId, groupName) {
if (document.<portlet:namespace />fm.<portlet:namespace />toGroupId.value != groupId) {
<portlet:namespace />selectFileEntry("", "", "");
}
document.<portlet:namespace />fm.<portlet:namespace />toGroupId.value = groupId;
document.<portlet:namespace />fm.<portlet:namespace />toFolderId.value = "";
document.<portlet:namespace />fm.<portlet:namespace />toName.value = "";
var nameEl = document.getElementById("<portlet:namespace />toGroupName");
nameEl.innerHTML = groupName + " ";
document.getElementById("<portlet:namespace />selectToFileEntryButton").disabled = false;
}
这样就完成了子窗口向父窗口的值传递过程。