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"






        <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="/">



                <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: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"/>


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

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

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


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

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






            <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)"/>


                    <a href="{$pageLink}">

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


                    <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 name="max">

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


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

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






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


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

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


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

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



                        Item is out of page






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


                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"/>







Input Xml:

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



            <Row Title="E_Blog1"/>

            <Row Title="A_Blog2"/>

            <Row Title="D_Blog3"/>

            <Row Title="B_Blog4"/>

            <Row Title="C_Blog5"/>

            <Row Title="F_Blog6"/>





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