发新话题
打印

[原创]ColdFusion的自定义分页标签

[原创]ColdFusion的自定义分页标签

这个分页是针对SQL的,不能用于ac上的哦,不过可以学习一下分页原理:嘿嘿:
复制内容到剪贴板
代码:
<cfif thisTag.HasEndTag is 'false'><!--- 如果没有结束标签</cf_page>时报错 --->
        <cfabort showerror="An end tag is required.">
</cfif>

<cfparam name="url.page" default="1"><!--- 默认为第一页 --->
<cfparam name="flag" default="0">


<cfparam name="Attributes.perpage" default="10"><!--- 每页显示多少条数据 --->
<cfparam name="Attributes.SQL" default=""><!--- SQL语句,不带最后的limit --->
<cfparam name="Attributes.url" default=""><!--- page前的地址 --->
<cfparam name="Attributes.datasource" default=""><!--- datasource name  --->
<cfparam name="Attributes.name" default=""><!--- return Query --->
<cfparam name="Attributes.html" default=""><!--- return page list html --->

<cfset start_limit = "#(url.page-1)*Attributes.perpage#" ><!--- 起始位置 --->

<cfset numofpage = "#ceiling(7602/Attributes.perpage)#"><!--- 总共多少页 --->

<cfif url.page lte 0 ><!--- 当前页小于等于0时 page=1 --->
        <cfset url.page = "1">
</cfif>
<cfif url.page gte numofpage><!--- 当前页大于等于总页数时 page=总页数 --->
        <cfset url.page = "#numofpage#" >
</cfif>

<cfquery datasource="#Attributes.datasource#" name='tmp'>#Attributes.SQL# limit #start_limit#,#Attributes.perpage#</cfquery><!--- 查询数据库 --->
<cfset "caller.#Attributes.name#" = tmp><!--- 返回Query Values --->

<cfif thisTag.ExecutionMode is 'start'><!--- 开始标记处理 --->
        <cfsavecontent variable="pagelist">
                <cfoutput>
                        <a href="#Attributes.url#page=1">首页</a><!--- 首页 --->
                        <cfloop from="#url.page-3#" to="#url.page-1#" index="i"><!--- 当前页的前面 --->
                                <cfif i gte 1>
                                        <a href="#Attributes.url#page=#i#">&nbsp;#i#&nbsp;</a>
                                </cfif>
                        </cfloop>
                        &nbsp;<span class="current_page">#i#</span>&nbsp;<!--- 当前页 --->
                        <cfif url.page lt numofpage><!--- 当前页的后面 --->
                                <cfloop from="#url.page+1#" to="#numofpage#" index="j">
                                        <a href="#Attributes.url#page=#j#">&nbsp;#j#&nbsp;</a>
                                        <cfscript>
                                                flag = flag + 1;
                                        </cfscript>
                                        <cfif flag eq 3><!--- 偏移量 --->
                                                <cfbreak>
                                        </cfif>
                                </cfloop>
                        </cfif>
                        <input type="text" class="input_jump" onkeydown="javascript: if(event.keyCode==13) location='#Attributes.url#page='+this.value;">&nbsp;<a href="#Attributes.url#page=#numofpage#">尾页</a>
                </cfoutput>
        </cfsavecontent>
        <cfset "caller.#Attributes.html#" = pagelist>
</cfif>

<!---

for example:

<style type="text/css">
    a {text-decoration:none;font-size:12px;color:blue;}
    a:hover {text-decoration:underline;font-size:12px;color:red;}
    .current_page {font-size:12px;color:red;}
    .input_jump {font-size:12px;width:20px; height:16px;}
</style>

<cfset SQL = "select * from vip order by id desc">


<cf_page datasource="testdb" name="getdata" perpage="20" SQL="#SQL#" url="?" html='pagelist'></cf_page>

<cfoutput>

#pagelist#

<br>
<br>

<cfloop query="getdata">
<li>#id#</li>
</cfloop>

<br>
<br>

#pagelist#

</cfoutput>


--->

TOP

发新话题