Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Martin 278 posts 662 karma points
    Mar 19, 2012 @ 12:29
    Martin
    0

    Expand & Collapse Nav - Apply class if there is child nodes

    Hi, Im having some trouble applying a class to an "a" tag for a collapse/expand nav.

    Im trying to replicate the following jquery based nav...

    http://www.mathachew.com/sandbox/collapsible-menu/

    The problem im having is applying the "collapse" class to the "a" tag, only if there is child nodes.

    Would be greatful if someone could help and steer me in the direct of a better way.

    My xslt is...

    Thanks

    Martin

    <!-- Input the documenttype you want here -->
    <xsl:variable name="levelroot" select="1"/>
    <xsl:variable name="level" select="2"/>
    <xsl:template match="/">
        <!-- Root Node -->
        <xsl:variable name="rootNode" select="$currentPage/ancestor-or-self::root" />
      
    <!-- The fun starts here -->
    <div class="nav">  
      <h4>../<xsl:value-of select="$currentPage/ancestor-or-self::*[@level=$level]/@nodeName "/></h4>
      <ul>
        
      <xsl:attribute name="class">
        <xsl:value-of select="$currentPage/ancestor-or-self::*[@level=$level]/@urlName"/>
      </xsl:attribute>
            
      <xsl:for-each select="$currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']">
        <li>
          <xsl:if test="$currentPage/ancestor-or-self::*[@isDoc]/@id = current()/@id">
            <xsl:attribute name="class">
              <xsl:text>selected</xsl:text>
            </xsl:attribute>
          </xsl:if>
          
            <xsl:if test="count($currentPage/child::*) &gt; 0">
              <xsl:attribute name="class">
                <xsl:text>collapse</xsl:text>
              </xsl:attribute>
          </xsl:if>
          
          <href="{umbraco.library:NiceUrl(@id)}">  
            <xsl:value-of select="@nodeName"/>
          </a>
          
          <!--SUB NAV-->
               <xsl:if test="*[umbracoNaviHide != 1]">
                <ul class="subnav">
                  <xsl:for-each select="*[umbracoNaviHide != 1]">
                    <li><href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="@nodeName"/></a></li>
                  </xsl:for-each>
                </ul>
               </xsl:if>
        </li>
      </xsl:for-each>
        
      </ul>
    </div>
    </xsl:template>
  • Rodion Novoselov 694 posts 859 karma points
    Mar 19, 2012 @ 12:36
    Rodion Novoselov
    1

    Hi.

    <xsl:if test="./*[@isDoc]">
        ...
    </xsl:if>

    should work I suppose. Since a nodeset evaluates to "true" if and only if it's not empty.

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Mar 19, 2012 @ 12:36
    Chriztian Steinmeier
    0

    Hi Martin,

    Create the <a> element like this:

    <a href="{umbraco.library:NiceUrl(@id)}">  
        <xsl:if test="$currentPage/*[@isDoc][not(umbracoNaviHide = 1)]">
            <xsl:attribute name="class">collapse</xsl:attribute>
        </xsl:if>
        <xsl:value-of select="@nodeName" />
    </a>

    (And remove the chunk right above where you use the count() function)

    /Chriztian

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Mar 19, 2012 @ 12:38
    Chriztian Steinmeier
    1

    - ah yes @Rodion, forgot to check if it was inside a for-each - this would be right, then (dot-slash isn't necessary here):

    <a href="{umbraco.library:NiceUrl(@id)}">  
        <xsl:if test="*[@isDoc][not(umbracoNaviHide = 1)]">
            <xsl:attribute name="class">collapse</xsl:attribute>
        </xsl:if>
        <xsl:value-of select="@nodeName" />
    </a>

    /Chriztian

  • Martin 278 posts 662 karma points
    Mar 19, 2012 @ 12:47
    Martin
    0

    Thanks guys,

    Thats worked great.

    Just for my learning, what is this line doing?

    <xsl:iftest="*[@isDoc][not(umbracoNaviHide = 1)]">

    Martin

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Mar 19, 2012 @ 13:09
    Chriztian Steinmeier
    1

    Hi Martin,

    The XPath selector in the test attribute will select all child elements (*) of the current node of the for-each that have an isDoc attribute ([@isDoc]) and that hasn't got an umbracoNaviHide childnode set to 1 ([not(umbracoNaviHide = 1)]) - this always results in a nodeset (although it could be empty). Because this is a test attribute the final result will either be true() or false(), depending on the set containing any nodes.

    So, it effectively checks to see if there are any childpages below the current.

    Does that help?

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft