Copied to clipboard

Flag this post as spam?

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


  • Craig Cronin 304 posts 503 karma points
    Sep 03, 2010 @ 21:40
    Craig Cronin
    0

    Sort descending when retrieving x number of items

    I'm new to umbraco and xslt and really struggling with this.  I'm trying to retrieve all event items for a site and display the two newest in a summary control.

    I can retrieve the items but due to be retrieving items with a position less than 3 its ignoring the sort.

        <xsl:template match="/">
     <xsl:variable name="rootNode" select="$currentPage/ancestor-or-self::node [@level = 1 and @nodeTypeAlias = 'Home']" />
     <xsl:variable name="Items" select="$rootNode/descendant::node [@nodeTypeAlias = 'Event_Item']" />

     <div class="events-summary">
         <xsl:for-each select="$Items [position() &lt; 3]">
      <xsl:sort select="@createDate" order="descending" />
      <div class="item">
       <div class="item-left">
        <img>
         <xsl:attribute name="src">
          <xsl:value-of select="data [@alias = 'primaryImage']"/>
           </xsl:attribute>
        </img>
       </div>
       <div class="item-right">                   
        <span class="date"><xsl:value-of select="umbraco.library:FormatDateTime(@createDate, 'dd.MM.yy')"/></span><br />
        <xsl:value-of select="umbraco.library:TruncateString(umbraco.library:StripHtml(data [@alias = 'primaryContent']), 80, '...')" />
        <a href="{umbraco.library:NiceUrl(@id)}">Read More...</a>
       </div>
      </div>
            </xsl:for-each>
     <div class="clear">&nbsp;</div> 
     </div>
        </xsl:template>

  • Jeff Grine 149 posts 189 karma points
    Sep 03, 2010 @ 22:01
    Jeff Grine
    1

    Pretty close, just need to move the position() &lt; 3 to an if after the sort.

    <xsl:for-each select="$Items">
    
    
    <xsl:sort select="@createDate" order="descending" />
    <xsl:if test="position() &lt; 3">

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Sep 03, 2010 @ 22:03
    Chriztian Steinmeier
    1

    Hi Craig,

    It's not ignoring the sort - it's just sorting the wrong nodes - the way you need to do it is to first sort all the nodes and then only do something to the first two:

    <xsl:for-each select="$Items">
        <xsl:sort select="@createDate" order="descending" />
        <xsl:if test="position() &lt; 3">
    
        <!-- Etc. -->
    
        </xsl:if>
    </xsl:for-each>
    

    /Chriztian

  • Craig Cronin 304 posts 503 karma points
    Sep 04, 2010 @ 09:41
    Craig Cronin
    0

    Hi Jeff and Chriztian,

    Just tried the code and worked like a charm.  I was stuck on this for about 3 hours last night and really do appreciate your help.

     

    Thanks

     

    Craig

Please Sign in or register to post replies

Write your reply to:

Draft