Sorting by attribute, Paging with links

08/12/2012 12:56

Following Xslt code takes pageNr from itself, but it's supposed to take it from queryString. That's why links on pages create querystring with 'pageNr' name and value. pageSize is settable in Xslt or by web part and passed as webpart property to xml/xslt.

 

<?xml version="1.0" encoding="utf-8"?>

<!-- Edited by Lukas Sevcik® -->

<xsl:stylesheet version="1.0"

    xmlns:xsl="https://www.w3.org/1999/XSL/Transform"

    xmlns:x="https://www.w3.org/2001/XMLSchema"

    xmlns:cmswrt="https://schemas.microsoft.com/WebPart/v3/Publishing/runtime"

    xmlns:cbq="urn:schemas-microsoft-com:ContentByQueryWebPart"

    xmlns:ddwrt="https://schemas.microsoft.com/WebParts/v2/DataView/runtime">

        <xsl:param name="pageNr" select="1"/>

        <xsl:param name="pageSize" select="2"/>

        <xsl:variable name="startPosition" select="$pageNr*$pageSize - $pageSize+1"/>

        <xsl:variable name="endPosition" select="$pageSize*$pageNr"/>

 

        <xsl:template match="/">

        <html>

            <body>

                <h2>Sorting by attribute, Paging with links</h2>

                    <xsl:call-template name="DisplayPagingStatus"/>

                        <xsl:for-each select="dsQueryResponse/Rows/Row">

                            <xsl:sort select="@Title"/>

                            <xsl:call-template name="DisplayRows"/>

                        </xsl:for-each>

                        <xsl:variable name="rowsCount" select="count(dsQueryResponse/Rows/Row)"/>

                        Rows count:<xsl:value-of select="$rowsCount"/>

                        <xsl:variable name="pagesCount" select="ceiling(number($rowsCount) div number($pageSize))" />

                        Pages to display: <xsl:value-of select="$pagesCount"/>

                        <br></br>

                        <xsl:call-template name="DisplayPagesLinks">

                            <xsl:with-param name="max">

                                <xsl:value-of select="$pagesCount"/>

                            </xsl:with-param>

                            <xsl:with-param name="i">1</xsl:with-param>

                            <xsl:with-param name="href">https://sitecollection/page.aspx</xsl:with-param>

                        </xsl:call-template>

                    </body>

                </html>

            </xsl:template>

 

            <xsl:template name="DisplayPagesLinks">

                <xsl:param name="href"/>

                <xsl:param name="max"/>

                <xsl:param name="i"/>

                <xsl:if test="not($i &gt; $max)">

                    <xsl:variable name="pageLink">

                        <xsl:value-of select="concat($href,'?pageNr=',$i)"/>

                    </xsl:variable>

                    <a href="{$pageLink}">

                        <xsl:value-of select="$i"/>

                    </a>

                    <xsl:text disable-output-escaping="yes"> </xsl:text>

                    <xsl:call-template name="DisplayPagesLinks">

                        <xsl:with-param name="href">

                            <xsl:value-of select="$href"/>

                        </xsl:with-param>

                        <xsl:with-param name="max">

                            <xsl:value-of select="$max"/>

                        </xsl:with-param>

                        <xsl:with-param name="i">

                            <xsl:value-of select="$i+1"/>

                        </xsl:with-param>

                     </xsl:call-template>

                  </xsl:if>

            </xsl:template>

 

            <xsl:template name="DisplayRows" match="Row">

                <p>

                Title: <xsl:value-of select="@Title"/>

                Position: <xsl:value-of select="position()"/>

                <xsl:choose>

                    <xsl:when test="(not(position() &gt; $endPosition)) and (position() &gt;= $startPosition)">

                        Item is on the page: <xsl:value-of select="$pageNr"/>

                    </xsl:when>

                    <xsl:otherwise>

                        Item is out of page

                    </xsl:otherwise>

                </xsl:choose>

                </p>

            </xsl:template>

 

            <xsl:template name="DisplayPagingStatus" match="Rows">

                <p>

                Page Nr: <xsl:value-of select="$pageNr"/>

                Page size: <xsl:value-of select="$pageSize"/>

                Start position: <xsl:value-of select="$startPosition"/>

                End position: <xsl:value-of select="$endPosition"/>

                <br></br>

                <xsl:text>----------------------------------</xsl:text>

                </p>

            </xsl:template>

</xsl:stylesheet>

 

Input Xml:

<?xml version="1.0" encoding="utf-8"?>

    <dsQueryResponse>

        <Rows>

            <Row Title="E_Blog1"/>

            <Row Title="A_Blog2"/>

            <Row Title="D_Blog3"/>

            <Row Title="B_Blog4"/>

            <Row Title="C_Blog5"/>

            <Row Title="F_Blog6"/>

        </Rows>

    </dsQueryResponse>

 

Result:

Sorting by attribute, Paging with links

Page Nr: 1 Page size: 2 Start position: 1 End position: 2
----------------------------------

Title: A_Blog2 Position: 1 Item is on the page: 1

Title: B_Blog4 Position: 2 Item is on the page: 1

Title: C_Blog5 Position: 3 Item is out of page

Title: D_Blog3 Position: 4 Item is out of page

Title: E_Blog1 Position: 5 Item is out of page

Title: F_Blog6 Position: 6 Item is out of page

Rows count:6 Pages to display: 3
1 2 3