如何使用javascript隐藏liferay中的portlet

Amr*_*esh 4 liferay

实际上在一个页面上我有2个portlet但是我想通过单击submit按钮来隐藏第一个portlet ,只有第二个portlet应该可见我使用了以下代码:

document.getElementById("portlet-id").style.visibility='none'
Run Code Online (Sandbox Code Playgroud)

但刷新页面之后,再次看到portlet可以让任何人为我提供如何继续的解决方案.

Pra*_*h K 5

您可以使用以下代码在JSP中设置visibilityportlet false:

<%
renderRequest.setAttribute(WebKeys.PORTLET_CONFIGURATOR_VISIBILITY, Boolean.FALSE);
%>
Run Code Online (Sandbox Code Playgroud)

这会将您的portlet隐藏在用户的视图之外.

每次呈现portlet时,您都可以检查在请求或会话中设置的参数(您的选择),以显示portlet或不显示portlet,如:

<%
String paramFromRequestToHide = renderRequest.getParameter("hidePortlet");
// can also fetch from session: portletSession.getAttribute("hidePortlet");

if (paramFromRequestToHide .equals("YES")) { // you can use your favorite data-type
    renderRequest.setAttribute(WebKeys.PORTLET_CONFIGURATOR_VISIBILITY, Boolean.FALSE);
} else {
    renderRequest.setAttribute(WebKeys.PORTLET_CONFIGURATOR_VISIBILITY, Boolean.TRUE);
}
%>
Run Code Online (Sandbox Code Playgroud)

另一种方法:


如果您不想使用上述方法,那么您可以将javascript方法与参数方法结合使用,如下所示:

<%
String paramFromRequestToHide = renderRequest.getParameter("hidePortlet");

if (paramFromRequestToHide .equals("YES")) {
%>

<aui:script>
    Liferay.Portlet.ready(

    /*
    This function gets loaded after each and every portlet on the page.

    portletId: the current portlet's id
    node: the Alloy Node object of the current portlet
    */
        function(portletId, node) {
            document.getElementById(portletId).style.display = 'none';
            // or alternatively using pure Alloy UI
            // node.hide();
        }
    );
</aui:script>

<%
} else {
%>

<aui:script>
    Liferay.Portlet.ready(
        function(portletId, node) {
            document.getElementById(portletId).style.display = 'block';
            // or alternatively using pure Alloy UI
            // node.show();
        }
    );
</aui:script>

<%
}
%>
Run Code Online (Sandbox Code Playgroud)

如果您想要签出Alloy UI API和一些演示来学习Alloy UI,因为从Liferay 6.1开始,Alloy UI是liferay的事实上的javascript库.现在Alloy UI有一个官方网站,里面有很多有用的教程和例子.

希望这能为您提供充足的资料:-)