Hi,
Many of us are using <liferay-ui:tabs ..> </ liferay-ui:tabs> and there are good road map given in Liferay’s site too in shape of blogs and forums, but one question is frequently asked by people that “How to persist current tab ? or How to remember current tab even though page gets refresh ? all are these open question ”
This is basic question but no clear answer given anywhere.
I would to provide neat and clean explanation for same.
Scenario:
1. I have view1, view2 and view3 which includes in master.jsp and one controller called MyController.java [JSR-286] portlet which extends MVCPortlet class of Liferay.
2. I have used following code in my master.jsp
<%
PortletURL portletURL = renderResponse.createRenderURL();
String currTAB = ParamUtil.getString(request, "currTAB", tabs1);
portletURL.setParameter("currTAB", currTAB);
%>
<liferay-ui:tabs param="currTAB" names="Tab1,Tab2,Tab3" refresh="<%= true %>" url="<%=portletURL.toString() %>">
<liferay-ui:section >
<%@ includefile="/jsp/view1.jsp" %>
</liferay-ui:section>
<liferay-ui:section>
<%@ include file="="/jsp/view2.jsp " %>
</liferay-ui:section>
<liferay-ui:section>
<%@ include file="="/jsp/view3.jsp " %>
</liferay-ui:section>
</liferay-ui:tabs>
Problem:
Suppose you are on view2.jsp and there you are submitting a form [or in other words your refreshing a current page which is view2.jsp] and you will be redirected to default page [in our case it is view1.jsp], it should not happened cause you have performed an action on view2.jsp page you suppose to rendered to view2.jsp instead of view1.jsp
Root cause:
When you refresh the page by actionURL or renderURL your client that means your browser does not know where you were earlier other words on which tab? So it will be redirecting to default page which is view1.jsp in our case.
How to resolve this?
So we know root cause now question is how to tell browser current tab? An obvious answer is keep live one parameter in each URL which will always tell to your browser that where to redirect.
How to achieve this?
As you might have observed ”currTAB” parameter in <liferay-ui:tabs> which will help you to achieve this, when you click on either of tab value of “currTAB” is automatically maintain accordingly. For ex. When user click on 3rd tab which name is “Tab3” it automatically store currTAB=”Tab3” as parameter in url.
Now you just need to maintain this variable. Like as below,
String currTAB = ParamUtil.getString(request, "currTAB", “Tab1”);
portletURL.setParameter("currTAB", currTAB);
What does above code says “It will try to get currTAB variable and if it is not found the same it will pass Tab1 as parameter value”
You need to write above code at everywhere you require maintaining tab value.
Make sure you keep refresh="<%= true %>" otherwise page will be not refreshed.
Hope this helps.
In case of more information require you can contact on
Thanks,
Sagar Vyas
Hi Sagar,
ReplyDeletecan you please tell me how to redirect to particular tab after some action ?