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”
1 2 3 4 |
<xsl:choose> <xsl:when test="results/currentpage = 1"><!-- display what you want --></xsl:when> <xsl:otherwise><a href="..."><<previous</a></xsl:otherwise> </xsl:choose> |
Handling weither to display “>> next”
1 2 3 4 |
<xsl:choose> <xsl:when test="results/currentpage >= ceiling(results/nbresult div results/nbperpage)"><!-- display what you want for the latest page--></xsl:when> <xsl:otherwise><a href="...">next >></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”/>)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
<xsl:template name="num_page"> <xsl:variable name="end"> <xsl:choose> <xsl:when test="(results/currentpage < 5) and (ceiling(results/nbresult div results/nbperpage) > 5)"> <xsl:value-of select="5"/> </xsl:when> <xsl:when test="results/currentpage > (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 < 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 > 6) and ($i = ($end - 10))"> ...</xsl:if> <xsl:if test="results/currentpage > 6"></xsl:if> <a href="next"><xsl:value-of select="$i"/></a> <xsl:if test="($i = $end) and ($end < ceiling(results/nbresult div results/nbperpage))"> ...</xsl:if> </xsl:otherwise> </xsl:choose> <xsl:if test="$i < $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> |