Copied to clipboard

Flag this post as spam?

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


  • Torben Warberg Rohde 35 posts 89 karma points
    May 14, 2010 @ 11:50
    Torben Warberg Rohde
    0

    Showing only x previous/next pages

    Good idea making a small project for this sample :-)

    A suggestion for a modification:
    You will easily run into the problem of the list of pages becoming waaay too long (e.g. 10000 records with 20 shown on each page).
    Luckily the "XSLTSearch guys" already solved this. It is relatively simple to replace the "for.loop" template call with their "pageNumbers" template. Here's how my version ended up looking:

    <!-- paged results template (modified from XSLTSearch) -->
    <xsl:template name="pageNumbers">
     <xsl:param name="page"/>
     <xsl:param name="pageIndex"/>
     <xsl:param name="numberOfRecords"/>
    <xsl:variable name="distanceFromCurrent" select="$pageIndex - $page"/>
     <xsl:choose>
      <xsl:when test="$pageIndex = $page">
       <!-- current paged set -->
       <strong>
        <xsl:value-of select="$pageIndex"/>
       </strong>&nbsp;
      </xsl:when>
      <!-- show a maximum of nine paged sets on either side of the current paged set; just like Google does it -->
      <xsl:when test="($distanceFromCurrent &gt; -10 and $distanceFromCurrent &lt; 10)">
       <a href="{umbraco.library:NiceUrl($currentPage/@id)}/?page={$pageIndex}" >
        <xsl:value-of select="$pageIndex" />
       </a>&nbsp;
      </xsl:when>
     </xsl:choose>
     <!-- recursively call the template for all the paged sets -->
     <xsl:if test="$pageIndex * $recordsPerPage &lt; $numberOfRecords">
      <xsl:call-template name="pageNumbers">
       <xsl:with-param name="pageIndex" select="$pageIndex + 1"/>
       <xsl:with-param name="page" select="$page"/>
       <xsl:with-param name="numberOfRecords" select="$numberOfRecords"/>
      </xsl:call-template>
     </xsl:if>
    </xsl:template>
  • Torben Warberg Rohde 35 posts 89 karma points
    May 14, 2010 @ 13:30
    Torben Warberg Rohde
    0

    Made a version where the amount of pages shown is always the same. So if you are at the first page, there will be more "next pages", and if you are at the last page, more "previous" pages will be shown:

    (not thoroughly tested - but it seems to work :-)

    <?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"/>
    <!-- Settings -->
    <xsl:variable name="recordsPerPage" select="20"/>
    <xsl:variable name="defaultPagesBeforeCurrent" select="10"/>
    <xsl:variable name="defaultPagesAfterCurrent" select="10"/>


    <!-- Fetch current page from query string -->
    <xsl:variable name="pageNumber">
     <xsl:choose>
      <xsl:when test="umbraco.library:RequestQueryString('page') &lt;= 1 or string(umbraco.library:RequestQueryString('page')) = '' or string(umbraco.library:RequestQueryString('page')) = 'NaN'">1</xsl:when>
      <xsl:otherwise>
       <xsl:value-of select="umbraco.library:RequestQueryString('page')"/>
      </xsl:otherwise>
     </xsl:choose>
    </xsl:variable>
    <xsl:variable name="numberOfRecords" select="count($currentPage/descendant::node [@nodeTypeAlias = 'clubMessage'])"/>
    <xsl:variable name="totalPageCount" select="ceiling($numberOfRecords div $recordsPerPage)" />


    <xsl:template match="/">
    <!-- TODO: Show the actual data list for the current page -->
    <!-- Show paging previous/next and numbers -->
    <xsl:if test="$pageNumber &gt; 1">
     <a href="{umbraco.library:NiceUrl($currentPage/@id)}/?page={$pageNumber - 1}">forrige</a>&nbsp;
    </xsl:if>
    <!-- We always want the same amount of pages listed. So if we are e.g. at the first page, we use spare page numbers for showing "next pages" -->
    <xsl:variable name="actualPagesBeforeCurrent">
     <xsl:choose>
      <xsl:when test="($pageNumber + $defaultPagesAfterCurrent) &gt; $totalPageCount">
       <xsl:value-of select="$defaultPagesBeforeCurrent + (($pageNumber + $defaultPagesBeforeCurrent) - $totalPageCount)" />
      </xsl:when>
      <xsl:otherwise>
       <xsl:value-of select="$defaultPagesBeforeCurrent" />
      </xsl:otherwise>
     </xsl:choose>
    </xsl:variable>
    <xsl:variable name="actualPagesAfterCurrent">
     <xsl:choose>
      <xsl:when test="($pageNumber - $defaultPagesBeforeCurrent) &lt; 0">
       <xsl:value-of select="$defaultPagesAfterCurrent + (-($pageNumber - $defaultPagesBeforeCurrent))" />
      </xsl:when>
      <xsl:otherwise>
       <xsl:value-of select="$defaultPagesAfterCurrent" />
      </xsl:otherwise>
     </xsl:choose>
    </xsl:variable>
    <!-- List pages -->
    <xsl:call-template name="pageNumbers">
     <xsl:with-param name="pageIndex" select="1"/>
     <xsl:with-param name="page" select="$pageNumber"/>
     <xsl:with-param name="numberOfRecords" select="$numberOfRecords"/>
     <xsl:with-param name="pageToShowBeforeCurrent" select="$actualPagesBeforeCurrent"/>
     <xsl:with-param name="pageToShowAfterCurrent" select="$actualPagesAfterCurrent"/>
    </xsl:call-template>
    <xsl:if test="(($pageNumber) * $recordsPerPage) &lt; ($numberOfRecords)">
     <a href="{umbraco.library:NiceUrl($currentPage/@id)}/?page={$pageNumber + 1}">n&#230;ste</a>
    </xsl:if>
    </xsl:template>
    <!-- Paged results template (from XSLTSearch) -->
    <xsl:template name="pageNumbers">
     <xsl:param name="page"/>
     <xsl:param name="pageIndex"/>
     <xsl:param name="numberOfRecords"/>
     <xsl:param name="pageToShowBeforeCurrent"/>
     <xsl:param name="pageToShowAfterCurrent"/>
     <xsl:variable name="distanceFromCurrent" select="$pageIndex - $page"/>
     <xsl:choose>
      <xsl:when test="$pageIndex = $page">
       <!-- current paged set -->
       <strong>
        <xsl:value-of select="$pageIndex"/>
       </strong>&nbsp;
      </xsl:when>
      <!-- show a maximum of nine paged sets on either side of the current paged set; just like Google does it -->
      <xsl:when test="($distanceFromCurrent &gt;= -$pageToShowBeforeCurrent and $distanceFromCurrent &lt;= $pageToShowAfterCurrent)">
       <a href="{umbraco.library:NiceUrl($currentPage/@id)}/?page={$pageIndex}" >
        <xsl:value-of select="$pageIndex" />
       </a>&nbsp;
      </xsl:when>
     </xsl:choose>
     <!-- recursively call the template for all the paged sets -->
     <xsl:if test="$pageIndex * $recordsPerPage &lt; $numberOfRecords">
      <xsl:call-template name="pageNumbers">
       <xsl:with-param name="pageIndex" select="$pageIndex + 1"/>
       <xsl:with-param name="page" select="$page"/>
       <xsl:with-param name="numberOfRecords" select="$numberOfRecords"/>
       <xsl:with-param name="pageToShowBeforeCurrent" select="$pageToShowBeforeCurrent"/>
       <xsl:with-param name="pageToShowAfterCurrent" select="$pageToShowAfterCurrent"/>
      </xsl:call-template>
     </xsl:if>
    </xsl:template>
    </xsl:stylesheet>
Please Sign in or register to post replies

Write your reply to:

Draft