如何在 XSLT 中进行分页 [英] How to make pagination in XSLT

查看:25
本文介绍了如何在 XSLT 中进行分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 XSLT:

<div id="dokumentliste"><xsl:variable name="alleNyheder" select="$currentPage//node"/><xsl:for-each select="$alleNyheder"><xsl:sort data-type="text" select="@createDate" order="descending"/><xsl:if test="./data[@alias='manchet'] !=''"><div class="newsitem"><h2><xsl:value-of select="./data[@alias='title']"/><xsl:if test="./data[@alias = 'manchet'] !=''"><div class="nyhedContent"><p><span class="dokumentListeDato"><xsl:when test="./data[@alias='date'] !=''"><xsl:value-of select="umbraco.library:FormatDateTime(./data[@alias='date'], 'dd.MMMM yyyy')"/></xsl:when><xsl:否则><xsl:value-of select="umbraco.library:FormatDateTime(./@createDate, 'dd.MMMM yyyy')"/></xsl:否则></xsl:选择></span><xsl:value-of select="./data[@alias = 'manchet']"/></p>

</xsl:if><div class="dokumentListe_laes_mere"><a href="{umbraco.library:NiceUrl(@id)}">Læs mere</a>

<!-- 结束新闻--></xsl:if></xsl:for-each>

</xsl:模板>

我正在制作新闻列表,并且想要制作某种分页.几乎和谷歌上看到的一样.你知道通常的".

但我不知道该怎么做.

每页上的新闻条目数并不重要,但可以说每页上有 10 个.当显示前 10 条新闻时,我希望显示分页.使用数字右侧和左侧的下一个"和上一个"按钮.

是否有可能做到这一点,我是否已经足够好地解释了我的问题?顺便说一句,我使用 Umbraco CMS :)

非常感谢.

-金

解决方案

是这样的:我还在那里留下了一些代码来处理您列表中的图片:-)

<xsl:param name="currentPage"/><xsl:template match="/"><xsl:variable name="recordsPerPage" select="2"/><xsl:variable name="pageNumber"><!--第一页--><xsl:when test="umbraco.library:RequestQueryString('page') &lt;= 0 or string(umbraco.library:RequestQueryString('page')) = '' or string(umbraco.library:RequestQueryString('page')) = 'NaN'">0<!-- 传入的内容--><xsl:否则><xsl:value-of select="umbraco.library:RequestQueryString('page')"/></xsl:否则></xsl:选择></xsl:变量><xsl:variable name="numberOfRecords" select="count($currentPage/node)"/><!-- 乐趣从这里开始--><xsl:call-template name="分页"><xsl:with-param name="pageNumber" select="$pageNumber"/><xsl:with-param name="recordsPerPage" select="$recordsPerPage"/><xsl:with-param name="numberOfRecords" select="$numberOfRecords"/></xsl:call-template><ul class="listing self-clear"><xsl:for-each select="$currentPage/node [string(data [@alias='umbracoNaviHide']) !='1']"><xsl:sort order="descending" select="data[@alias='releasedOn']"></xsl:sort><xsl:if test="position() &gt; $recordsPerPage * number($pageNumber) and position() &lt;= number($recordsPerPage * number($pageNumber) + $recordsPerPage )"><li><xsl:attribute name="class"><xsl:if test="data[@alias='image'] = ''">无图像</xsl:if><xsl:if test="position() = $recordsPerPage * (number($pageNumber) + 1)">最后的</xsl:if></xsl:attribute><h3><a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="@nodeName"/></a><xsl:if test="data[@alias='image'] !=''"><img src="{data[@alias='image']}" class="drop-shadow"/></xsl:if><p class="date"><xsl:value-of select="umbraco.library:LongDate(data[@alias='releasedOn'])"/></p><xsl:value-of select="data[@alias='abstract']" disable-output-escaping="yes"/><a href="{umbraco.library:NiceUrl(@id)}" class="read-more">阅读更多</a></xsl:if></xsl:for-each><xsl:call-template name="分页"><xsl:with-param name="pageNumber" select="$pageNumber"/><xsl:with-param name="recordsPerPage" select="$recordsPerPage"/><xsl:with-param name="numberOfRecords" select="$numberOfRecords"/></xsl:call-template></xsl:模板><xsl:template name="分页"><xsl:param name="pageNumber"/><xsl:param name="recordsPerPage"/><xsl:param name="numberOfRecords"/><div class="分页"><div class="wrapper"><xsl:if test="(($pageNumber +1 ) * $recordsPerPage) &lt; ($numberOfRecords)"><a href="?page={$pageNumber +1}" class="next">Next</a></xsl:if><xsl:if test="$pageNumber &gt; 0"><a href="?page={$pageNumber -1}" class="prev">Prev</a></xsl:if><span class="page-nos">页<xsl:call-template name="for.loop"><xsl:with-param name="i">1</xsl:with-param><xsl:with-param name="page" select="$pageNumber +1"></xsl:with-param><xsl:with-param name="count" select="ceiling(count($currentPage/node)div $recordsPerPage)"></xsl:with-param></xsl:call-template></span>

</xsl:模板><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="{umbraco.library:NiceUrl($currentPage/@id)}?page={$i - 1}" ><xsl:value-of select="$i"/></a></xsl:if><xsl:if test="$page = $i"><xsl:value-of select="$i"/></xsl:if></span></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:模板>

I have the following XSLT:

<xsl:template match="/">        
  <div id="dokumentliste">
    <xsl:variable name="alleNyheder" select="$currentPage//node" />

    <xsl:for-each select="$alleNyheder">
    <xsl:sort data-type="text" select="@createDate" order="descending" />            

        <xsl:if test="./data[@alias='manchet'] != ''">
            <div class="newsitem">
                <h2>
                    <xsl:value-of select="./data[@alias='title']"/>
                </h2>

                <xsl:if test="./data[@alias = 'manchet'] != ''">
                    <div class="nyhedContent">
                        <p>
                            <span class="dokumentListeDato">
                                <xsl:choose>
                                    <xsl:when test="./data[@alias='date'] != ''">
                                        <xsl:value-of select="umbraco.library:FormatDateTime(./data[@alias='date'], 'dd. MMMM yyyy')"/>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <xsl:value-of select="umbraco.library:FormatDateTime(./@createDate, 'dd. MMMM yyyy')"/>
                                    </xsl:otherwise>
                                </xsl:choose>
                            </span>
                            <xsl:value-of select="./data[@alias = 'manchet']"/>
                        </p>
                    </div>
                </xsl:if>
                <div class="dokumentListe_laes_mere">
                    <a href="{umbraco.library:NiceUrl(@id)}">
                        Læs mere<img src="/frontend/images/macro/macro_laes_mere.png" alt="Læs mere"/>
                    </a>
                </div>
            </div>
            <!-- End newsitem -->
        </xsl:if>
      </xsl:for-each>
   </div> 
 </xsl:template>

I am making a newslist, and would like to make some sort of pagination. Almost the same one as seen on Google. You know "the usual one".

But I can't figure out how to do this.

The number of newsitems on each page isn't that important, but lets say 10 on each page. When the 10 first newsitems are shown, I would like the pagination to show up. With the "Next" and "Previous" buttons to the right and the left of the numbers.

Is it possible to make this, and have I explained my problem good enough? I use the Umbraco CMS by the way :)

Thank you very much.

-Kim

解决方案

Something like this: I've left some code in there for dealing with images in your listing too :-)

<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:param name="currentPage"/>
<xsl:template match="/">
    <xsl:variable name="recordsPerPage" select="2"/>
    <xsl:variable name="pageNumber">
        <xsl:choose>
            <!-- first page -->
            <xsl:when test="umbraco.library:RequestQueryString('page') &lt;= 0 or string(umbraco.library:RequestQueryString('page')) = '' or string(umbraco.library:RequestQueryString('page')) = 'NaN'">0</xsl:when>
            <!-- what was passed in -->
            <xsl:otherwise>
                <xsl:value-of select="umbraco.library:RequestQueryString('page')"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:variable name="numberOfRecords" select="count($currentPage/node)"/>

    <!-- The fun starts here -->

    <xsl:call-template name="pagination">
        <xsl:with-param name="pageNumber" select="$pageNumber"/>
        <xsl:with-param name="recordsPerPage" select="$recordsPerPage" />
        <xsl:with-param name="numberOfRecords" select="$numberOfRecords" />
    </xsl:call-template>

<ul class="listing self-clear">
        <xsl:for-each select="$currentPage/node [string(data [@alias='umbracoNaviHide']) != '1']">
            <xsl:sort order="descending" select="data[@alias='releasedOn']"></xsl:sort>

            <xsl:if test="position() &gt; $recordsPerPage * number($pageNumber) and position() &lt;= number($recordsPerPage * number($pageNumber) + $recordsPerPage )">
                <li>
                    <xsl:attribute name="class">
                        <xsl:if test="data[@alias='image'] = ''">
                            no-img
                        </xsl:if>
                        <xsl:if test="position() = $recordsPerPage * (number($pageNumber) + 1)">
                            last
                        </xsl:if>
                    </xsl:attribute>
                    <h3>
                        <a href="{umbraco.library:NiceUrl(@id)}">
                            <xsl:value-of select="@nodeName"/>
                        </a>
                    </h3>
                    <xsl:if test="data[@alias='image'] != ''">
                        <img src="{data[@alias='image']}" class="drop-shadow" />
                    </xsl:if>
                    <p class="date"><xsl:value-of select="umbraco.library:LongDate(data[@alias='releasedOn'])"/></p>
                    <xsl:value-of select="data[@alias='abstract']" disable-output-escaping="yes"/>
                    <a href="{umbraco.library:NiceUrl(@id)}" class="read-more">Read More</a>
                </li>
            </xsl:if>
        </xsl:for-each>
    </ul>

    <xsl:call-template name="pagination">
        <xsl:with-param name="pageNumber" select="$pageNumber"/>
        <xsl:with-param name="recordsPerPage" select="$recordsPerPage" />
        <xsl:with-param name="numberOfRecords" select="$numberOfRecords" />
    </xsl:call-template>

</xsl:template>

<xsl:template name="pagination">
    <xsl:param name="pageNumber"/>
    <xsl:param name="recordsPerPage"/>
    <xsl:param name="numberOfRecords"/>

    <div class="pagination">
    <div class="wrapper">

        <xsl:if test="(($pageNumber +1 ) * $recordsPerPage) &lt; ($numberOfRecords)">
            <a href="?page={$pageNumber +1}" class="next">Next</a>
        </xsl:if>

        <xsl:if test="$pageNumber &gt; 0">
            <a href="?page={$pageNumber -1}" class="prev">Prev</a>
        </xsl:if>

        <span class="page-nos">
        Page
        <xsl:call-template name="for.loop">
            <xsl:with-param name="i">1</xsl:with-param>
            <xsl:with-param name="page" select="$pageNumber +1"></xsl:with-param>
            <xsl:with-param name="count" select="ceiling(count($currentPage/node)div $recordsPerPage)"></xsl:with-param>
        </xsl:call-template>
        </span>

    </div>
    </div>
</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">
        <span>
        <xsl:if test="$page != $i">
            <a href="{umbraco.library:NiceUrl($currentPage/@id)}?page={$i - 1}" >
                <xsl:value-of select="$i" />
            </a>
        </xsl:if>
        <xsl:if test="$page = $i">
            <xsl:value-of select="$i" />
        </xsl:if>
        </span>
    </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>

这篇关于如何在 XSLT 中进行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆