Copied to clipboard

Flag this post as spam?

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


  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Jan 23, 2009 @ 11:44
    Dan Diplo
    2

    Flexible XSLT Paging and Sorting Example

    I noticed a few people asking for examples of XSLT pagination so I thought I'd post some example code that allows for flexible paging and sorting of child pages. It's based very much on the example at http://www.nibble.be/?p=11 which I've adapted (this version starts at page '1' rather than '0', which is more natural). It can also sort pages by date or title. If the sort field is a date then that date is displayed. There are lots of tweakable parameters. You just need to write a Macro to call it. (It's my first XSLT attempt, so be kind!).

    [code]


    ]>
    <>
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxml="urn:schemas-microsoft-com:xslt"
    xmlns:umbraco.library="urn:umbraco.library"
    xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes"
    exclude-result-prefixes="msxml umbraco.library">







































    DEBUG


    numberOfRecords =

    recordsPerPage =

    current page =

    sortBy =

    orderBy =

    sortDirection =




























    [/code]

  • Tom Maton 387 posts 660 karma points
    Oct 14, 2009 @ 19:32
    Tom Maton
    0

    Hi Dan,

    First off thanks for the great example. Only thing I dont have working is the page numbers, they are not displaying.

    I haven't changed the for.loop and was just wondering if you have the page numbers showing on your pagination?

    Thanks

    Tom

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Oct 14, 2009 @ 19:38
    Jan Skovgaard
    0

    Hi Dan

    Good initiative. Please make sure that you put it in the wiki as well.

    /Jan

  • James Telfer 65 posts 165 karma points
    Nov 05, 2009 @ 11:47
    James Telfer
    0

    Hi Dan,

    Nice work, thanks!

    A small bug: when determining the page number, the test condition:

    $pageQS &lt;= 0 or string($pageQS) = '' or string($pageQS) = 'NaN'

    should be (I think):

    $pageQS &lt;= 0 or string($pageQS) = '' or string(number($pageQS)) = 'NaN'

    or even:

    not(number($pageQS) &gt; 0)

     

    Hope this is helpful (and correct). I'll pass through others if I see them.

    JT

  • James Telfer 65 posts 165 karma points
    Nov 05, 2009 @ 13:45
    James Telfer
    0

    Hi Dan,

    The numberOfRecords variable may have the wrong count under some conditions I think because the node set used for the count is different to the node set in the apply-templates call.

    You can select the node set into a variable:

    <xsl:variable name="records" select="$currentPage/node[(@nodeTypeAlias = $nodeTypeAlias or $nodeTypeAlias = not(string(.))) and string(data [@alias='umbracoNaviHide']) != '1']"/>

    Then use it for the count and apply:

    <xsl:variable name="numberOfRecords" select="count($records)"/>
    ...
    <xsl:apply-templates select="$records"/>
    

    I know this is just an example, but it's helped me out, so hopefully these fixes can help someone else.

    Thanks,
    JT

  • Iain Smallwood 8 posts 27 karma points
    Jan 13, 2010 @ 18:38
    Iain Smallwood
    0

    Hi Dan,

    Just a word of appreciation from myslf - I easily butchered your code to fulfil my strange requirements thanks to your fine commenting and flexible code.

    Much obliged to you sir.

  • Dan 1288 posts 3921 karma points c-trib
    Jan 29, 2010 @ 17:19
    Dan
    0

    I've implemented this pagination script on my site, and it doesn't return anything (no errors, but no content, page numbers or anything).  I appreciate it's a long script, but can anyone see why it's not working?  I can't figure out how it's supposed to be outputting the 'node' template as that's not called from anywhere, so perhaps that's the problem?

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxml="urn:schemas-microsoft-com:xslt"
    xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets"
    exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">

    <xsl:output method="xml" omit-xml-declaration="yes" />

    <xsl:param name="currentPage"/>


    <xsl:variable name="pageQS" select="umbraco.library:RequestQueryString('page')" />
    <xsl:variable name="pageNumber">
    <xsl:choose>
    <xsl:when test="$pageQS &lt;= 0 or string($pageQS) = '' or string(number($pageQS)) = 'NaN'">1</xsl:when>
    <xsl:otherwise>
    <xsl:value-of select="umbraco.library:RequestQueryString('page')"/>
    </xsl:otherwise>
    </xsl:choose>
    </xsl:variable>
    <xsl:variable name="recordsPerPage" select="10"/>


    <xsl:template match="/">
    <xsl:variable name="numberOfRecords" select="count($currentPage/ancestor-or-self::node [@level=1]/node [(@id = 1050) or (@id = 1051) or (@id = 1052)]/node)"/>

    <!-- Start Paging Links -->
    <xsl:if test="$recordsPerPage &lt; $numberOfRecords">
    <hr />
    <!-- Previous Link -->
    <xsl:if test="$pageNumber &gt; 1">
    <a href="?page={$pageNumber -1}"> &lt; Previous </a>
    </xsl:if>

    <!-- Output page number links -->
    <xsl:call-template name="for.loop">
    <xsl:with-param name="i">1</xsl:with-param>
    <xsl:with-param name="page" select="$pageNumber +1"></xsl:with-param>
    <xsl:with-param name="count" select="ceiling(count($currentPage/node)div $recordsPerPage)"></xsl:with-param>
    </xsl:call-template>

    <!-- Next Link -->
    <xsl:if test="(($pageNumber ) * $recordsPerPage) &lt; ($numberOfRecords)">
    <a href="?page={$pageNumber +1}"> Next &gt; </a>
    </xsl:if>
    </xsl:if>
    <!-- End Paging Links -->
    </xsl:template>


    <xsl:template match="node">
    <xsl:for-each select="$currentPage/ancestor-or-self::node [@level=1]/node [(@id = 1050) or (@id = 1051) or (@id = 1052)]/node">
    <xsl:sort select="data [@alias='articleDate']" order="descending"/>
    <xsl:if test="position() &gt; $recordsPerPage * number($pageNumber -1) and position() &lt;= number($recordsPerPage * number($pageNumber -1) + $recordsPerPage )">
    <xsl:variable name="pageNode" select="$currentPage/node [string(data [@alias='umbracoNaviHide']) != '1']"/>
    <div class="item">
    <div class="copy">
    <h2>
    <a href="{umbraco.library:NiceUrl(@id)}">
    <xsl:value-of select="@nodeName"/>
    </a> / <xsl:value-of select="umbraco.library:FormatDateTime(current()/data[@alias='articleDate'], 'dd.MM.yyyy')"/>
    </h2>
    <xsl:value-of select="current()/data[@alias='articleTeaser']" disable-output-escaping="yes"/>
    </div>
    <xsl:variable name="mediaId" select="current()/data[@alias='mainImage']" />
    <xsl:variable name="vimeoClipId" select="current()/data[@alias='vimeoClipId']" />
    <xsl:choose>
    <xsl:when test="$mediaId &gt; 0">
    <xsl:variable name="mediaNode" select="umbraco.library:GetMedia($mediaId, 0)" />
    <img src="{$mediaNode/data[@alias='umbracoFile']}" alt="" title="" />
    </xsl:when>
    <xsl:otherwise>
    <object width="512" height="386"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id={$vimeoClipId}&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=898a58&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id={$vimeoClipId}&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=898a58&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="512" height="386"></embed></object>
    </xsl:otherwise>
    </xsl:choose>
    </div>
    </xsl:if>
    </xsl:for-each>
    </xsl:template>


    <!-- Generate Pagination Numbers Loop -->
    <xsl:template name="for.loop">
    <xsl:param name="i"/>
    <xsl:param name="count"/>
    <xsl:param name="page"/>
    <xsl:if test="$i &lt;= $count">
    <xsl:if test="$page != ($i + 1)">
    <a href="{umbraco.library:NiceUrl($currentPage/@id)}?page={$i}" style="padding-left:1em" >
    <xsl:value-of select="$i" />
    </a>
    </xsl:if>

    <xsl:if test="$page = ($i + 1) ">
    <span style="padding-left:1em">
    <xsl:value-of select="$i" />
    </span>
    </xsl:if>
    </xsl:if>

    <xsl:if test="$i &lt;= $count">
    <xsl:call-template name="for.loop">
    <xsl:with-param name="i">
    <xsl:value-of select="$i + 1" />
    </xsl:with-param>
    <xsl:with-param name="count">
    <xsl:value-of select="$count"/>
    </xsl:with-param>
    <xsl:with-param name="page">
    <xsl:value-of select="$page"/>
    </xsl:with-param>
    </xsl:call-template>
    </xsl:if>

    </xsl:template>

    </xsl:stylesheet>
  • Steven Wilber 103 posts 98 karma points
    Feb 17, 2010 @ 11:55
    Steven Wilber
    0

    Hi,

    I don't know if it helps, but, refering to the first post, you can use a variable in the sort by wrapping it in braces, e.g. '... order="{$sortOrder}"...'

    <xsl:apply-templates select="$currentPage/node [(@nodeTypeAlias = $nodeTypeAlias or $nodeTypeAlias = not(string(.))) and string(data [@alias='umbracoNaviHide']) != '1']">
    <xsl:sort select="@*[name()=$orderBy]" order="{$sortOrder}" data-type="text" />
    </xsl:apply-templates>

    I hope that that helps someone out (probably me when I google again) in the future.

    Cheers

    Steve

  • Daniel 19 posts 51 karma points
    Feb 22, 2011 @ 11:57
    Daniel
    0

    Hey, sorry for bumping an old thread but I'd like to get this thing to work. :)

    I get the same problem as Dan. The script doesn't generate any errors, nor any result! Does the code need any customization before it can work? My page layout look something like this:

    Website
    - Page
    - Page
     - Subpage
     - Subpage
     - Subpage
     - Subpage
    - Page

    I'd like to list just the subpages from a specific node. Anyone care to point me in the right directions?

    Cheers!

  • Connie DeCinko 931 posts 1160 karma points
    Feb 08, 2012 @ 17:07
    Connie DeCinko
    0

    I know this is an old thread, but the topic is still valid.  I need to paginate a list of several hundred nodes.  I don't see how I can use your example however since I group the items by month and year.  If I just do a simple xsl:if inside my first for-each, I get nothing.

     

Please Sign in or register to post replies

Write your reply to:

Draft