Foren

Problems detecting a selected nav item in velocity template

E S, geändert vor 13 Jahren.

Problems detecting a selected nav item in velocity template

Junior Member Beiträge: 38 Beitrittsdatum: 21.04.10 Neueste Beiträge
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, geändert vor 12 Jahren.

RE: Problems detecting a selected nav item in velocity template

Junior Member Beiträge: 39 Beitrittsdatum: 03.11.11 Neueste Beiträge
Same problem here!
In portal_normal.vm, i include navigation 2 times. First time isSelected(() works as desired. second time not.
WHY?
Bradley Wood, geändert vor 11 Jahren.

RE: Problems detecting a selected nav item in velocity template

Mason Stein, geändert vor 11 Jahren.

RE: Problems detecting a selected nav item in velocity template

Junior Member Beiträge: 47 Beitrittsdatum: 05.12.12 Neueste Beiträge
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, geändert vor 10 Jahren.

    RE: Problems detecting a selected nav item in velocity template

    New Member Beiträge: 24 Beitrittsdatum: 27.02.13 Neueste Beiträge
    Did you solve it?
    James charley, geändert vor 10 Jahren.

    RE: Problems detecting a selected nav item in velocity template

    Junior Member Beiträge: 44 Beitrittsdatum: 12.06.13 Neueste Beiträge
    Any one solved the above issue ?
    Aldi Tirane, geändert vor 10 Jahren.

    RE: Problems detecting a selected nav item in velocity template

    Junior Member Beiträge: 25 Beitrittsdatum: 23.10.12 Neueste Beiträge
    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, geändert vor 10 Jahren.

    RE: Problems detecting a selected nav item in velocity template

    New Member Beiträge: 23 Beitrittsdatum: 15.07.13 Neueste Beiträge
    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, geändert vor 10 Jahren.

    RE: Problems detecting a selected nav item in velocity template

    Junior Member Beiträge: 25 Beitrittsdatum: 23.10.12 Neueste Beiträge
    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, geändert vor 10 Jahren.

    RE: Problems detecting a selected nav item in velocity template

    New Member Beiträge: 17 Beitrittsdatum: 20.07.12 Neueste Beiträge
    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, geändert vor 9 Jahren.

    RE: Problems detecting a selected nav item in velocity template

    Junior Member Beiträge: 29 Beitrittsdatum: 01.02.13 Neueste Beiträge
    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.