XSLT: Make a smart pagination in HTML

Use XSLT to make a smart pagination rendering result in HTML, more or less like the google one. The following is extracted from an old project of my own and the code is not expected to works as is. It took every needed parts, but as this code is out of its original context, it probably needs some work. Nevertheless, it can be a begining.

Description

The purpose is to show N pages with “…” at the end for the first time results are displayed. Then, if you click on the last shown page, you will see N pages before and N pages after the one you clicked. for example: if you have:

1 – 2 – 3 – 4 – 5 … next >>

clicking on “5” will display

<< previous 1 – 2 – 3 – 4 – 5 – 6 – 7 – 8 – 9 – 10 … next >>/>

then clicking on 10 will display

<< previous … 6 – 7 – 8 – 9 – 10 – 11 – 12 – 13 – 14 – 15 … next >>

XSLT code

Handling weither to display “<< previous”

  <xsl:choose>
    <xsl:when test="results/currentpage = 1"><!-- display what you want --></xsl:when>
    <xsl:otherwise><a href="...">&lt;&lt;previous</a></xsl:otherwise>
  </xsl:choose>

Handling weither to display “>> next”

<xsl:choose>
  <xsl:when test="results/currentpage &gt;= ceiling(results/nbresult div results/nbperpage)"><!-- display what you want for the latest page--></xsl:when>
  <xsl:otherwise><a href="...">next &gt;&gt;</a></xsl:otherwise>
</xsl:choose>

Between previous and next link: show page numbers

just place a call to the following template (eg. <xsl:call-template name=”num_page”/>)

<xsl:template name="num_page">
 
 <xsl:variable name="end">
  <xsl:choose>
     <xsl:when test="(results/currentpage &lt; 5) and (ceiling(results/nbresult div results/nbperpage) &gt; 5)">
       <xsl:value-of select="5"/>
    </xsl:when>
    <xsl:when test="results/currentpage &gt; (ceiling(results/nbresult div results/nbperpage)- 5)">
      <xsl:value-of select="ceiling(results/nbresult div results/nbperpage)"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="results/currentpage + 5"/>
    </xsl:otherwise>
  </xsl:choose>
 </xsl:variable>
 
 <xsl:variable name="i">
  <xsl:choose>
    <xsl:when test="results/currentpage &lt; 6">
     <xsl:value-of select="1"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$end - 10"/>
    </xsl:otherwise>
  </xsl:choose>
 </xsl:variable>
 
 <xsl:call-template name="num_page_helper">
   <xsl:with-param name="i" select="$i"/>
   <xsl:with-param name="end" select="$end"/>
 </xsl:call-template>
</xsl:template>
 
 
<xsl:template name="num_page_helper">
 <xsl:param name="i"/>
 <xsl:param name="end"/>
 
 <xsl:choose>
   <xsl:when test="$i = results/currentpage">
     <xsl:value-of select="$i"/>
   </xsl:when>
   <xsl:otherwise>
    <xsl:if test="(results/currentpage &gt; 6) and ($i = ($end - 10))"> ...</xsl:if>
    <xsl:if test="results/currentpage &gt; 6"></xsl:if>
     <a href="next"><xsl:value-of select="$i"/></a>
    <xsl:if test="($i = $end) and ($end &lt; ceiling(results/nbresult div results/nbperpage))"> ...</xsl:if>
   </xsl:otherwise>
  </xsl:choose>
 
  <xsl:if test="$i &lt; $end">
    <xsl:call-template name="num_page_helper">
      <xsl:with-param name="i" select="$i + 1"/>
      <xsl:with-param name="end" select="$end"/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>

Leave a Reply

Your email address will not be published. Required fields are marked *


*