Copied to clipboard

Flag this post as spam?

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


  • Rik Hodiamont 56 posts 156 karma points
    Nov 10, 2013 @ 15:59
    Rik Hodiamont
    0

    Filter nodes on value in nodename

    Hi!

    Is it possible to show all the nodes starting with a certain letter?

    I have a list of books in Umbraco, and when a user clicks on the A. I only want to show the books starting with an A.

    Thanks in advance for any hint or help.

    Rik

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Nov 10, 2013 @ 22:33
    Chriztian Steinmeier
    101

    Hi Rik,

    You can use the starts-with() function for that - if you create a list like this where you link each letter to itself:

    <ul>
        <li><a href="?letter=A">A</a></li>
        <li><a href="?letter=B">B</a></li>
        <li><a href="?letter=C">C</a></li>
        <!-- etc. -->
    </ul>
    

    Then you can grab the letter from the QueryString and show all the books starting with that letter, using something like this:

    <!-- Grab the root of the site -->
    <xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::*[@level = 1]" />
    
    <!-- Grab the container for the books -->
    <xsl:variable name="booksRoot" select="$siteRoot/Books" />
    
    <xsl:template match="/">
        <xsl:variable name="letter" select="umbraco.library:RequestQueryString('letter')" />
        <xsl:apply-templates select="$booksRoot//Book[starts-with(@nodeName, $letter)]" />
    </xsl:template>
    
    <!-- Template for a Book -->
    <xsl:template match="Book">
        <section class="book">
            <h1>
                <a href="umbraco.library:NiceUrl(@id)"><xsl:value-of select="@nodeName" /></a>
            </h1>
            <!-- etc. -->
        </section>
    </xsl:template>
    

    Note that you'll have to substitute the actual DocumentType aliases for Books and Book documents where they're used in the above code.

    /Chriztian

  • Rik Hodiamont 56 posts 156 karma points
    Nov 13, 2013 @ 11:22
    Rik Hodiamont
    0

    Tnx Chriztian. This will do the trick!

  • Gary 40 posts 129 karma points
    Mar 14, 2014 @ 16:07
    Gary
    0

    If you wanted to have a 'show all' button for this function - how could this be done?

    Thanks

  • Peter Gregory 408 posts 1614 karma points MVP 3x admin c-trib
    Mar 14, 2014 @ 16:40
    Peter Gregory
    0

    Yep totally can.

    You would need to do a little modification to the above script. basically just put in a test to see if the letter variable is passed. If its not just get to apply the template to all books. My XSLT is a little rusty as I havent written any in about 2 years but I think it should look a little like this.

    <xsl:variable name="letter" select="umbraco.library:RequestQueryString('letter')" />
    
    <xsl:choose>
        <xsl:when test="$letter != ''">
            <xsl:apply-templates select="$booksRoot//Book[starts-with(@nodeName, $letter)]" />
        </xsl:when>
        <xsl:otherwise>
            <xsl:apply-templates select="$booksRoot//Book"/>
        </xsl:otherwise>
    </xsl:choose>
    

    Then all you need is a link to the page without any querystring param.

Please Sign in or register to post replies

Write your reply to:

Draft