Copied to clipboard

Flag this post as spam?

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


  • Peter Grillo Frederiksen 34 posts 67 karma points
    Aug 03, 2011 @ 09:53
    Peter Grillo Frederiksen
    1

    XSLT Paging Google-style

    Hey all,

    I've been working with some paging. Taking base in what Nibble did: nibble.be/?p=11

    Now i have 20 pages in the paging, and it's slowly growing. Could anyone helt me making, so it looks something like: 1 2 3 4 5 6 7 8 ... (ind the begining) and then later: 1 2 3 4 ... 9 10 11 12 - hope you get the drift ;)

    This is my code:

    <xsl:for-each select="BlogLibrary:GetCommentsForPost($currentPage/@id)//comment">
        <xsl:sort select="umbraco.library:FormatDateTime(@created, 'yyyy-MM-dd HH:mm')" order="descending"/>
            <xsl:if test="position() &gt; $recordsPerPage * number($pageNumber - 1) and position() &lt;= number($recordsPerPage * number($pageNumber - 1) + $recordsPerPage )">
            <xsl:variable name="creator" select="./name"/>
                <table width="500px">
                    <tr valign="bottom">
                        <td width="43px">
                            <xsl:variable name="creatorPicture" select="umbraco.library:GetMember($creator)/data[@alias='playerpicture']"/>   
                                        <xsl:variable name="picture" select="umbraco.library:GetMedia($creatorPicture, 0)/data [@alias = 'umbracoFile']" />
                            <img src="{$picture}" width="40px"/>
                        </td>
                        <td>
                            <font class="postedby">Oprettet af <xsl:value-of select="umbraco.library:GetMember($creator)/data[@alias='aka']"/><br/>
                            <xsl:value-of select="umbraco.library:LongDate(@created,'true',' kl. ')"/></font>
                        </td>
                    </tr>
                </table>
                <xsl:value-of select="umbraco.library:ReplaceLineBreaks( umbraco.library:HtmlEncode( ./message ))" disable-output-escaping="yes"/><hr/>
                  </xsl:if>
    </xsl:for-each>
    <br/>
    <xsl:if test="$pageNumber &gt; 1">
                <a href="{umbraco.library:NiceUrl($currentPage/@id)}?page={$pageNumber - 1}" class="commentLink">Forrige</a>&nbsp;
            </xsl:if>
            <xsl:call-template name="for.loop">
                <xsl:with-param name="i">1</xsl:with-param>
                <xsl:with-param name="page" select="$pageNumber"></xsl:with-param>
                <xsl:with-param name="count" select="ceiling(count($comments) div $recordsPerPage)"></xsl:with-param>
            </xsl:call-template>
    
    
            <xsl:if test="(($pageNumber) * $recordsPerPage) &lt; ($numberOfRecords)">
                &nbsp;<a href="{umbraco.library:NiceUrl($currentPage/@id)}?page={$pageNumber + 1}" class="commentLink">Næste</a>
            </xsl:if>
    <br/><br/>
        </xsl:template>
        <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">
                    <a href="?page={$i}" class="commentLink">
                        <xsl:value-of select="$i" />
                    </a>
                </xsl:if>
                <xsl:if test="$page = $i">
                    <font class="currentPage"><xsl:value-of select="$i" /></font>
                </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>

    Thx in advance.

     

    // Peter

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Aug 03, 2011 @ 10:11
    Lee Kelleher
    0

    Hi Peter,

    I haven't got much time today to help work through your XSLT specifically, but I've posted up my own generic pagination XSLT (along with how to include it in an example XSLT) over on GitHub, take a look: https://gist.github.com/1122148 - it does the "Google style" distancing of the page numbers too!

    Feel free to use it, or take the good bits from it ... all good.

    Cheers, Lee.

  • Peter Grillo Frederiksen 34 posts 67 karma points
    Aug 03, 2011 @ 12:47
    Peter Grillo Frederiksen
    0

    Hey Lee,
    Thank you very much, you get a solved for this.
    However, I have one tiny question.

    When using the code (slightly modified)

     <xsl:template name="for.loop">
            <xsl:param name="i"/>
            <xsl:param name="count"/>
            <xsl:param name="page"/>
    
            <xsl:variable name="distanceFromCurrent" select="$i - $page"/>
    
            <xsl:if test="($distanceFromCurrent &gt; -5 and $distanceFromCurrent &lt; 5)">
                <xsl:choose>
                    <xsl:when test="$i = $page">
                        <font class="currentPage"><xsl:value-of select="$i" /></font>
                    </xsl:when>
                    <xsl:otherwise>
                        <a href="?page={$i}" class="commentLink">
                            <xsl:value-of select="$i" />
                        </a>
                    </xsl:otherwise>
                </xsl:choose>
            </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>

    I get shown 21 pages, but when i "extract" $count, it only says 20. How come, when i get to page 17, it goes all the way up to page 21?
    I think I'm just missing a "-1" somewhere - can you show mere where?

    Cheers, Peter 

  • Jignesh 31 posts 51 karma points
    Jan 25, 2012 @ 13:33
    Jignesh
    0

    Hello all,

    I am doing some change for paging. if any querystring is there in url so previous xslt is not working properly..

     <?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"  xmlns:ucomponents.urls="urn:ucomponents.urls" 
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">

      <xsl:variable name="resultsPerPage" select="number(6)" />  
      <xsl:template name="pagination">
        <xsl:param name="page" />    
        <xsl:param name="matchedNodes" />
        <xsl:param name="qs" />
        <div class="pagginginarea">    

          <!-- previous page -->
          <xsl:if test="$page &gt; 1">        
              <!--<a class="paging-text-prev" href="{concat('?page=', $page - 1, $qs)}" title="Previous page">Prev</a>        -->
            <class="paging-text-prev" href="{ucomponents.urls:AppendOrUpdateQueryString('page',$page - 1)}" title="Previous page">Prev</a>
            
          </xsl:if>

          <!-- each paged set of results is listed, with a link to that page set -->
          <xsl:call-template name="pageNumbers">
            <xsl:with-param name="pageIndex" select="1" />
            <xsl:with-param name="page" select="$page" />
            <xsl:with-param name="matchedNodes" select="$matchedNodes" />
            <xsl:with-param name="qs" select="$qs" />
          </xsl:call-template>

          <!-- next page -->
          <xsl:if test="$page * $resultsPerPage &lt; count($matchedNodes)">        
              <!--<a class="paging-text-next" href="{concat('?page=', $page + 1, $qs)}" title="Next page">Next</a>-->
            <class="paging-text-next" href="{ucomponents.urls:AppendOrUpdateQueryString('page',$page + 1)}" title="Next page">Next</a>
          </xsl:if>
        </div>
      </xsl:template>

      <xsl:template name="pageNumbers">
        <xsl:param name="page" />
        <xsl:param name="pageIndex" />
        <xsl:param name="matchedNodes" />    
        <xsl:param name="qs" />

        <xsl:variable name="distanceFromCurrent" select="$pageIndex - $page"/>

        <!-- show a maximum of nine paged sets on either side of the current paged set; just like Google does it -->
        <xsl:if test="($distanceFromCurrent &gt; -6 and $distanceFromCurrent &lt; 6)">
          <xsl:choose>
            <xsl:when test="$pageIndex = $page">          
                <href="#" class="paging-number-sel"><xsl:value-of select="$pageIndex"/></a>          
            </xsl:when>
            <xsl:otherwise>          
                <!--<a class="paging-number" href="{concat('?page=', $pageIndex, $qs)}" title="Page {$pageIndex}">-->
                <class="paging-number" href="{ucomponents.urls:AppendOrUpdateQueryString('page',$pageIndex)}" title="Page {$pageIndex}">
                  <xsl:value-of select="$pageIndex" />
                </a>          
            </xsl:otherwise>
          </xsl:choose>
        </xsl:if>

        <!-- recursively call the template for all the paged sets -->
        <xsl:if test="$pageIndex * $resultsPerPage &lt; count($matchedNodes)">
          <xsl:call-template name="pageNumbers">
            <xsl:with-param name="pageIndex" select="$pageIndex + 1" />
            <xsl:with-param name="page" select="$page" />
            <xsl:with-param name="matchedNodes" select="$matchedNodes" />
            <xsl:with-param name="qs" select="$qs" />        
          </xsl:call-template>
        </xsl:if>

      </xsl:template>

    </xsl:stylesheet>

    Thanks...

Please Sign in or register to post replies

Write your reply to:

Draft