Fórum

Problems detecting a selected nav item in velocity template

E S, modificado 13 Anos atrás.

Problems detecting a selected nav item in velocity template

Junior Member Postagens: 38 Data de Entrada: 21/04/10 Postagens Recentes
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 12 Anos atrás.

RE: Problems detecting a selected nav item in velocity template

Junior Member Postagens: 39 Data de Entrada: 03/11/11 Postagens Recentes
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 11 Anos atrás.

RE: Problems detecting a selected nav item in velocity template

Mason Stein, modificado 11 Anos atrás.

RE: Problems detecting a selected nav item in velocity template

Junior Member Postagens: 47 Data de Entrada: 05/12/12 Postagens Recentes
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 10 Anos atrás.

    RE: Problems detecting a selected nav item in velocity template

    New Member Postagens: 24 Data de Entrada: 27/02/13 Postagens Recentes
    Did you solve it?
    James charley, modificado 10 Anos atrás.

    RE: Problems detecting a selected nav item in velocity template

    Junior Member Postagens: 44 Data de Entrada: 12/06/13 Postagens Recentes
    Any one solved the above issue ?
    Aldi Tirane, modificado 10 Anos atrás.

    RE: Problems detecting a selected nav item in velocity template

    Junior Member Postagens: 25 Data de Entrada: 23/10/12 Postagens Recentes
    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 10 Anos atrás.

    RE: Problems detecting a selected nav item in velocity template

    New Member Postagens: 23 Data de Entrada: 15/07/13 Postagens Recentes
    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 10 Anos atrás.

    RE: Problems detecting a selected nav item in velocity template

    Junior Member Postagens: 25 Data de Entrada: 23/10/12 Postagens Recentes
    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 10 Anos atrás.

    RE: Problems detecting a selected nav item in velocity template

    New Member Postagens: 17 Data de Entrada: 20/07/12 Postagens Recentes
    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 9 Anos atrás.

    RE: Problems detecting a selected nav item in velocity template

    Junior Member Postagens: 29 Data de Entrada: 01/02/13 Postagens Recentes
    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.