Copied to clipboard

Flag this post as spam?

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


  • Alec Colebrook-Clark 134 posts 258 karma points
    Dec 17, 2009 @ 16:31
    Alec Colebrook-Clark
    0

    String manipulation

    Hey All,

     

    Quick query/brain picker. I have a list of nodes (1300 in total) and i want to sort them alphabetically and show and hide them by the letter (a user clicks the letter A and only sees the nodes starting with the letter A). What is the best way to sort them and add the div class needed?

    Thanks

    Alec

  • dandrayne 1138 posts 2262 karma points
    Dec 17, 2009 @ 17:41
    dandrayne
    1

    For sorting, In xslt 1.0 (...)  You'd need to look at something like http://www.jenitennison.com/xslt/grouping/muenchian.html

    Here's something that I have, not sure if it will work for you.  The intention in the below was to get a list of locations, and list them by country order.  In this case the nodes had a property of "country" that I was sorting by.  You'd probably need to truncate a property to be the first character, then use that as headings

    <xsl:for-each select="$currentPage/node [@nodeTypeAlias='docToSort'][count(. | key('locations-by-country', data[@alias='propertyToSortBy'])[1]) = 1]">

    <h3><xsl:value-of select="current()/data[@alias='propertyToSortBy']" /></h3>
    <ul>
    <xsl:for-each select="key('locations-by-country', data[@alias='propertyToSortBy'])">

    </xsl:for-each>
    </ul>

    </xsl:for-each>

     

    On another idea, although 1300 nodes is a probably too much for JS, you might want to look at at http://www.ihwy.com/Labs/Demos/Current/jquery-listnav-plugin.aspx

    Dan

     

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Dec 18, 2009 @ 00:37
    Chriztian Steinmeier
    1

    Hi Alec,

    Here's a sample stylesheet using Muenchian Grouping (as dandrayne suggested) for grouping the nodes by first-letter (of @nodeName in this sample) and using the generate-id() function to generate unique ids for the nodes.

    <?xml version="1.0" encoding="utf-8" ?>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:umb="urn:umbraco.library"
        exclude-result-prefixes="umb"
    >
    
        <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    
        <xsl:key name="node-by-first-letter" match="node" use="substring(@nodeName, 1, 1)" />
    
        <xsl:param name="currentPage" />
        <xsl:variable name="root" select="$currentPage/ancestor-or-self::root" />
    
        <xsl:template match="/">
            <script type="text/javascript"><![CDATA[
    function show(id) {
        document.getElementById(id).style.display = 'block';
    }
            ]]>
            </script>
    
            <xsl:for-each select="$root//node[count(. | key('node-by-first-letter', substring(@nodeName, 1, 1))[1]) = 1]">
                <xsl:sort select="@nodeName" />
                <xsl:apply-templates select="." mode="group" />
    
                <div id="{generate-id(.)}" style="display:none">
                    <xsl:apply-templates select="key('node-by-first-letter', substring(@nodeName, 1, 1))" mode="item">
                        <xsl:sort select="firstname" />
                    </xsl:apply-templates>
                </div>
            </xsl:for-each>
        </xsl:template>
    
    
        <xsl:template match="node" mode="group">
            <a href="#" onclick="show('{generate-id(.)}'); return false;"><xsl:value-of select="substring(@nodeName, 1, 1)" /></a>
        </xsl:template>
    
    
        <xsl:template match="node" mode="item">
            <p><xsl:value-of select="@nodeName" /></p>
        </xsl:template>
    
    </xsl:stylesheet>

    Hopefully it gets you on the right track...

    /Chriztian 

  • Alec Colebrook-Clark 134 posts 258 karma points
    Dec 21, 2009 @ 11:53
    Alec Colebrook-Clark
    0

    Thanks for that, i havent had time to try it out but ill let you know when i have!

    Alec

  • Alec Colebrook-Clark 134 posts 258 karma points
    Dec 23, 2009 @ 17:00
    Alec Colebrook-Clark
    0

    Thanks for that! Both did the trick!

    Alec

Please Sign in or register to post replies

Write your reply to:

Draft