Foros de discusión

Problems detecting a selected nav item in velocity template

E S, modificado hace 13 años.

Problems detecting a selected nav item in velocity template

Junior Member Mensajes: 38 Fecha de incorporación: 21/04/10 Mensajes recientes
I am having a problem in my navigation.vm template. Here is the code for the site's navigation:


<ul>
	#foreach ($nav_item in $nav_items)
		#if ($nav_item.isSelected())
			#set ($nav_item_class = "selected")
			<b>Selected!</b>
		#else
			#set ($nav_item_class = "")
		#end

		#if ($nav_item.getName() != "Home")
			<li class="$nav_item_class">
				<a href="$nav_item.getURL()" $nav_item.gettarget()><span>$nav_item.getName()</span></a>
			</li>
		#end
	#end
</ul>


For some reason, none of my navigation items on the site ever get the "selected" class assigned to it. I am having a lot of trouble figuring out why. Can anyone see a problem with my template?
Robert Kornmesser, modificado hace 12 años.

RE: Problems detecting a selected nav item in velocity template

Junior Member Mensajes: 39 Fecha de incorporación: 3/11/11 Mensajes recientes
Same problem here!
In portal_normal.vm, i include navigation 2 times. First time isSelected(() works as desired. second time not.
WHY?
Bradley Wood, modificado hace 11 años.

RE: Problems detecting a selected nav item in velocity template

Mason Stein, modificado hace 11 años.

RE: Problems detecting a selected nav item in velocity template

Junior Member Mensajes: 47 Fecha de incorporación: 5/12/12 Mensajes recientes
Hey,

strange..I have the same problem.
When I visit the public site my li elements never get the selected class. When I login , everything is fine ?!?

	#foreach ($nav_item in $nav_items)	
			#if ($nav_item.isSelected())
				<li class="selected">
			#else
				</li>
  • #end


  • Any help?
    Michal Sima, modificado hace 10 años.

    RE: Problems detecting a selected nav item in velocity template

    New Member Mensajes: 24 Fecha de incorporación: 27/02/13 Mensajes recientes
    Did you solve it?
    James charley, modificado hace 10 años.

    RE: Problems detecting a selected nav item in velocity template

    Junior Member Mensajes: 44 Fecha de incorporación: 12/06/13 Mensajes recientes
    Any one solved the above issue ?
    Aldi Tirane, modificado hace 10 años.

    RE: Problems detecting a selected nav item in velocity template

    Junior Member Mensajes: 25 Fecha de incorporación: 23/10/12 Mensajes recientes
    Hello to everyone.
    Did anybody solve this issue?

    Thanks.

    Mason Stein:
    Hey,

    strange..I have the same problem.
    When I visit the public site my li elements never get the selected class. When I login , everything is fine ?!?

    	#foreach ($nav_item in $nav_items)	
    			#if ($nav_item.isSelected())
    				<li class="selected">
    			#else
    				</li>
  • #end


  • Any help?
    Laxman Rana, modificado hace 10 años.

    RE: Problems detecting a selected nav item in velocity template

    New Member Mensajes: 23 Fecha de incorporación: 15/07/13 Mensajes recientes
    Hi,

    Try with this code

    
    <nav class="$nav_css_class" id="nav_menu">
    <ul class="nav">
    #foreach ($nav_item in $nav_items)
    #if ($nav_item.isSelected())
    <li class="selected active">
    #else
    </li><li>
    #end
    </li></ul>
    </nav>


    Thanks,
    Laxman Rana
    Surekha Technologies
    www.surekhatech.com
    Aldi Tirane, modificado hace 10 años.

    RE: Problems detecting a selected nav item in velocity template

    Junior Member Mensajes: 25 Fecha de incorporación: 23/10/12 Mensajes recientes
    The problem is that $nav_item.isSelected() return always false.

    I found why this happens. I have included in header.vm the sign-in portlet with the following code:

    
    #set ($locPortletId = "58")
    $velocityPortletPreferences.setValue("portlet-setup-show-borders","false")		
    #set($locRenderedPortletContent = $theme.runtime($locPortletId, "", $velocityPortletPreferences.toString()))
    $locRenderedPortletContent
    $velocityPortletPreferences.reset()
    


    If i remove this code in the header the $nav_item.isSelected() behaves normally returning true for the page where the user is in.

    I don't know why this happens, maybe someone can give a more detailed explanation.

    Thanks, Aldi.
    thumbnail
    Puneet Malode, modificado hace 10 años.

    RE: Problems detecting a selected nav item in velocity template

    New Member Mensajes: 17 Fecha de incorporación: 20/07/12 Mensajes recientes
    Hi,
    I have created the portlet to access Navigation. But I am not able to get the selected NavItem. Please help me to solve this problem.
    Please find the below code for your reference.

    In my Portlet Controller class.

    
    public String getChildPages(RenderRequest request, RenderResponse response, ExtendedModelMap model){
    		
    		
    		
    		try {
    			ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
    			List<layout> layouts  = themeDisplay.getLayouts();
    			Layout layout = themeDisplay.getLayout();
    			HttpServletRequest httpServletRequest = PortalUtil.getHttpServletRequest(request);
    			for (Layout lay : layouts) {
    				System.out.println("Is Layout Selected ? "+lay.isSelected(true, lay, lay.getAncestorPlid()));
    				System.out.println("Is Child Selected ? "+lay.isChildSelected(true, lay));
    			}
    			
    			VelocityContext velocityContext = VelocityEngineUtil.getWrappedStandardToolsContext();
    			RequestVars requestVars = new RequestVars(httpServletRequest, themeDisplay, layout.getAncestorPlid(), layout.getAncestorLayoutId(), velocityContext);
    			
    			List<navitem> navItems = NavItem.fromLayouts(requestVars, layouts);
    			
    			for (NavItem navItem : navItems) {
    				System.out.println("NavItem Selected ? "+navItem.isSelected());
    				System.out.println("Is Child Selected ? "+navItem.isChildSelected());
    			}
    			
    			model.put("NavItems", navItems);
    			
    		} catch (PortalException e) {
    			e.printStackTrace();
    		} catch (SystemException e) {
    			e.printStackTrace();
    		}
    		
    		
    		return "LeftNav";
    	}
    </navitem></layout>



    In my JSP

    <ul class="list-group">
    <c:foreach items="${NavItems}" var="navItem">
    	<c:if test="${navItem.hasChildren()}">
    		<c:if test="${navItem.isChildSelected()}">
    			<c:foreach items="${navItem.getChildren()}" var="child1">
    				<c:if test="${child1.isSelected()}">
    					<li class="list-group-item current">
    				
    				<c:if test="${!child1.isSelected()}">
    					</c:if></li><li class="list-group-item">
    				
    						<a href="${child1.getURL() }">
    							${child1.getName() }
    						</a>
    					</li>
    			</c:if></c:foreach>
    		</c:if>
    	</c:if>
    </c:foreach>
    </ul>
    thumbnail
    Dominique Ebert, modificado hace 9 años.

    RE: Problems detecting a selected nav item in velocity template

    Junior Member Mensajes: 29 Fecha de incorporación: 1/02/13 Mensajes recientes
    Hi everybody,
    I know the thread is a little bit old but I found a workaround for the problem someone mentioned in here and I thought it would be better to post it here instead of creating a new thread. I faced the same problem as Aldi Tirane did (see quote). I embedded a Portlet into my theme with the liferay suggested code (see quote again ;) ). But whenever i did that, the isSelected() Function always returned false.

    So I tried to embedd the portlet after the navigation was built and that worked. So I came up with this workaround, maybe its useful to somebody with the same issue:

    - My navigation code was stored in the navigation.vm so I parsed the content of the navigation into a variable:

    
    #set($navigationPath = "$full_templates_path/navigation.vm")
    #set ($navigationParsed="#parse($navigationPath )")
    


    This made sure the navigation had been created before I embedded the portlet. Afterwards I embedded the portlet as suggested in the quote. Afterwards I just printed the content of the variable where I wanted the navigation to appear.

    That worked fine for me, I hope this may be useful to somebody.

    Last but not least - does anybody know why this problem occurs in the first place, or wether it has been resolved after 6.1GA2?

    Regards

    Aldi Tirane:
    The problem is that $nav_item.isSelected() return always false.

    I found why this happens. I have included in header.vm the sign-in portlet with the following code:

    
    #set ($locPortletId = "58")
    $velocityPortletPreferences.setValue("portlet-setup-show-borders","false")		
    #set($locRenderedPortletContent = $theme.runtime($locPortletId, "", $velocityPortletPreferences.toString()))
    $locRenderedPortletContent
    $velocityPortletPreferences.reset()
    


    If i remove this code in the header the $nav_item.isSelected() behaves normally returning true for the page where the user is in.

    I don't know why this happens, maybe someone can give a more detailed explanation.

    Thanks, Aldi.