将页码添加到分页数据

时间:2012-06-10 23:00:20

标签: coldfusion coldfusion-9

我已对我的数据实施了分页,但问题是我只有Next和Previous链接,如下所示:

enter image description here

我想要做的是添加页码,我的页码是普通文本,其他页面是链接。所以,如果我在5页的第3页,它看起来像这样:

enter image description here

这是我到目前为止所做的:

<cfset data = queryNew("id,name,age,active","integer,varchar,integer,bit")>

<cfloop index="x" from="1" to="50">
<cfset queryAddRow(data)>
<cfset querySetCell(data,"id",x)>
<cfset querySetCell(data,"name","User #x#")>
<cfset querySetCell(data,"age",randRange(20,90))>
<cfset querySetCell(data,"active",false)>
</cfloop>

<cfset perpage = 10>

<cfparam name="url.start" default="1">
<cfif not isNumeric(url.start) or url.start lt 1 or url.start gt data.recordCount or round(url.start) neq url.start>
<cfset url.start = 1>
</cfif>

<h2>Random People</h2>

<cfoutput query="data" startrow="#url.start#" maxrows="#perpage#">
#currentrow#) #name#<br />
</cfoutput>

<p align="right">
[
<cfif url.start gt 1>
<cfset link = cgi.script_name & "?start=" & (url.start - perpage)>
<cfoutput><a href="#link#">Previous Page</a></cfoutput>
<cfelse>
Previous Page
</cfif>
/
<cfif (url.start + perpage - 1) lt data.recordCount>
<cfset link = cgi.script_name & "?start=" & (url.start + perpage)>
<cfoutput><a href="#link#">Next Page</a></cfoutput>
<cfelse>
Next Page
</cfif>
]
</p>

2 个答案:

答案 0 :(得分:2)

RIAForge上有一个非常好的ColdFusion开源分页项目:http://paginationcfc.riaforge.org/。它涵盖了您需要的一切,并带有许多预定义的样式。至少您可以分析代码并对其进行自定义以满足您的要求。

答案 1 :(得分:1)

这是一个有趣的问题。如何构建页面链接列表:

<cfset pageList = "">
<cfloop from="1" to="#ceiling(data.RecordCount/perpage)#" index="i">
    <!--- Determine the start record for selected page --->
    <cfset targetRecord = 1 + (perpage * (i - 1))>
    <cfif ceiling(url.start/perpage) NEQ i>
        <cfset link = cgi.script_name & "?start=" & targetRecord>
        <cfset pageList = listAppend(pageList, "<a href=""#link#"">#i#</a>", " ")>
    <cfelse>
        <cfset pageList = listAppend(pageList, i, " ")>
    </cfif>
</cfloop>

现在您可以将pageList放入导航部分,如下所示:

<p align="right">
[
<cfif url.start gt 1>
    <cfset link = cgi.script_name & "?start=" & (url.start - perpage)>
    <cfoutput><a href="#link#">Previous Page</a></cfoutput>
<cfelse>
    Previous Page
</cfif>
/
#pageList#
/
<cfif (url.start + perpage - 1) lt data.recordCount>
    <cfset link = cgi.script_name & "?start=" & (url.start + perpage)>
    <cfoutput><a href="#link#">Next Page</a></cfoutput>
<cfelse>
    Next Page
</cfif>
]
</p>