coldfusion 分页(下一页和上一页)
coldfusion pagination (Next & Previous)
我必须在我的项目中进行分页:
下面的代码工作正常,只是我无法弄清楚(下一个/上一个)按钮我该如何处理。我正在使用 mysql 数据库
如果你能给我一些最佳实践技巧,那将很有帮助&
如果有任何其他简单的方法?请给我参考这将是很大的帮助
这是我的代码:
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" type="application/javascript"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!---
<cfquery name="data" datasource="#application.dsn#">
select * from yourTable
</cfquery>
--->
<!--- Generate fake data --->
<cfset data = queryNew("id,name","integer,varchar")>
<cfloop index="x" from="1" to="80">
<cfset queryAddRow(data, {id:x,name:"User #x#"})>
</cfloop>
<cfparam name="URL.PageId" default="0">
<cfset RecordsPerPage = 5>
<cfset TotalPages = (data.Recordcount/RecordsPerPage)-1>
<cfset StartRow = (URL.PageId*RecordsPerPage)+1>
<cfset EndRow = StartRow+RecordsPerPage-1>
<cfoutput>
<table border="1">
<tr>
<th>No.</th>
<th>PARKNAME</th>
<th>REGION </th>
<th>STATE</th>
</tr>
<cfloop query="data">
<cfif CurrentRow gte StartRow >
<tr>
<td>#CurrentRow#</td>
<td>#name#</td>
<td>US</td>
<td>MO</td>
</tr>
</cfif>
<cfif CurrentRow eq EndRow>
<cfbreak>
</cfif>
</cfloop>
</cfoutput>
<tr>
<td colspan="4">
<cfloop index="Pages" from="0" to="#TotalPages#">
<cfoutput>
<cfset DisplayPgNo = Pages+1>
<cfif URL.PageId eq pages>
<strong>#DisplayPgNo#</strong>
<cfelse>
<a href="?PageId=#Pages#">#DisplayPgNo#</a>
</cfif>
</cfoutput>
</cfloop>
</td>
</tr>
</table>
<nav aria-label="...">
<ul class="pagination">
<li class="page-item disabled">
<a class="page-link" href="#" tabindex="-1">Previous</a>
</li>
<cfloop index="Pages" from="0" to="#TotalPages#">
<cfoutput>
<cfset DisplayPgNo = Pages+1>
<cfif URL.PageId eq pages>
<li class="page-item">
<li class="page-item active">
<a class="page-link" href="##">#DisplayPgNo#</a>
</li>
<cfelse>
<li class="page-item"><a class="page-link" href="?PageId=#Pages#">#DisplayPgNo#</a></li>
</cfif>
</cfoutput>
</cfloop>
<li class="page-item">
<a class="page-link" href="#">Next</a>
</li>
</ul>
</nav>
上一个应该是这样的
<li class="page-item <cfif url.pageid LTE 1>disabled</cfif>">
<a class="page-link" href="?Pagesid=#(url.pageid - 1)#" tabindex="-1">Previous</a>
</li>
下一步的类似代码
这是一个完整的分页解决方案。尽管您可能希望将 paginateBS
函数放在可重用的应用程序变量中。
<!--- CURRENT PAGE --->
<cfparam name="url.page" default="1">
<cfparam name="request.PAGI_pagenum" default="#url.page#">
<!--- RECORDS PER PAGE (ON ALL PAGES) --->
<cfparam name="request.PAGI_allperpage" default="20">
<!--- RECORDS PER PAGE (ON FIRST PAGE) --->
<cfparam name="request.PAGI_firstperpage" default="#request.PAGI_allperpage#">
<!--- NUMBER OF PAGE LINKS AT A TIME --->
<cfparam name="request.PAGI_blocksof" default="5">
<cfset fromRecord = request.PAGI_firstperpage+(request.PAGI_allperpage*(request.PAGI_pagenum-1))-request.PAGI_allperpage>
<cfquery name="quotes" >
SELECT SQL_CALC_FOUND_ROWS first_name, last_name, email, id, datetime, published
FROM quotes
ORDER by datetime DESC
LIMIT #fromRecord#,
<cfif request.PAGI_pagenum is 1>
#request.PAGI_firstperpage#
<cfelse>
#request.PAGI_allperpage#
</cfif>
</cfquery>
<Cfquery name="result_count" >
SELECT FOUND_ROWS() as totalRecords
</cfquery>
<div class="pull-right" id="page">
<div class="pagination pagination-custom">
<ul>
<cfoutput>#paginateQuotes(totalRecords=result_count.totalRecords)#</cfoutput>
</ul>
</div>
</div>
<cffunction name="paginateBS">
<cfargument name="totalRecords" default="0">
<cfparam name="request.PAGI_attribute" default="">
<!--- 'TOP' IS THE LAST PAGE NUMBER LINK --->
<Cfset top = request.PAGI_blocksof * ceiling((request.PAGI_pagenum)/request.PAGI_blocksof)>
<cfoutput>
<!--- IF WE'RE PAST THE FIRST BLOCK OF PAGES, SHOW THE PREV LINK --->
<cfif request.PAGI_pagenum gt request.PAGI_blocksof>
<li>
<a href="?page=#top-request.PAGI_blocksof#<cfif request.PAGI_attribute neq ''>&#request.PAGI_attribute#=#request.PAGI_attributevalue#</cfif>">
<i class="icon-double-angle-left"></i>
</a>
</li>
</cfif>
<!--- LOOP THROUGH THE PAGES IN THIS BLOCK --->
<cfloop from="#top-request.PAGI_blocksof+1#" to="#top#" index="a">
<li <cfif request.PAGI_pagenum is not a>class="active"</cfif>>
<a href="?page=#a#<cfif request.PAGI_attribute neq ''>&#request.PAGI_attribute#=#request.PAGI_attributevalue#</cfif>">#a#</a>
</li>
<!--- IF WE'RE ON THE LAST BLOCK AND DON'T HAVE ENOUGH RECORDS TO COMPLETE THE BLOCK,
STOP CREATING THE LINKS FOR MORE PAGES, AND SET A FLAG TO NOT SHOW THE LINK FOR THE NEXT BLOCK --->
<cfif ((a-1) * request.PAGI_allperpage) + request.PAGI_firstperpage gte arguments.totalRecords>
<cfset arguments.noNext=1>
<cfbreak>
</cfif>
</cfloop>
<!--- IF WE RAN OUT OF RECORDS, DO NOT SHOW THE LINK FOR THE NEXT BLOCK OF PAGES --->
<cfif not isdefined("arguments.noNext")>
<li>
<a href="?page=#top+1#<cfif request.PAGI_attribute neq ''>&#request.PAGI_attribute#=#request.PAGI_attributevalue#</cfif>">
<i class="icon-double-angle-right"></i>
</a>
</li>
</cfif>
</cfoutput>
</cffunction>
我最终使用 https://datatables.net/
分页,在这里发帖希望它能帮助像我这样的人:)
谢谢你们帮助我...
我必须在我的项目中进行分页:
下面的代码工作正常,只是我无法弄清楚(下一个/上一个)按钮我该如何处理。我正在使用 mysql 数据库 如果你能给我一些最佳实践技巧,那将很有帮助& 如果有任何其他简单的方法?请给我参考这将是很大的帮助
这是我的代码:
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" type="application/javascript"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!---
<cfquery name="data" datasource="#application.dsn#">
select * from yourTable
</cfquery>
--->
<!--- Generate fake data --->
<cfset data = queryNew("id,name","integer,varchar")>
<cfloop index="x" from="1" to="80">
<cfset queryAddRow(data, {id:x,name:"User #x#"})>
</cfloop>
<cfparam name="URL.PageId" default="0">
<cfset RecordsPerPage = 5>
<cfset TotalPages = (data.Recordcount/RecordsPerPage)-1>
<cfset StartRow = (URL.PageId*RecordsPerPage)+1>
<cfset EndRow = StartRow+RecordsPerPage-1>
<cfoutput>
<table border="1">
<tr>
<th>No.</th>
<th>PARKNAME</th>
<th>REGION </th>
<th>STATE</th>
</tr>
<cfloop query="data">
<cfif CurrentRow gte StartRow >
<tr>
<td>#CurrentRow#</td>
<td>#name#</td>
<td>US</td>
<td>MO</td>
</tr>
</cfif>
<cfif CurrentRow eq EndRow>
<cfbreak>
</cfif>
</cfloop>
</cfoutput>
<tr>
<td colspan="4">
<cfloop index="Pages" from="0" to="#TotalPages#">
<cfoutput>
<cfset DisplayPgNo = Pages+1>
<cfif URL.PageId eq pages>
<strong>#DisplayPgNo#</strong>
<cfelse>
<a href="?PageId=#Pages#">#DisplayPgNo#</a>
</cfif>
</cfoutput>
</cfloop>
</td>
</tr>
</table>
<nav aria-label="...">
<ul class="pagination">
<li class="page-item disabled">
<a class="page-link" href="#" tabindex="-1">Previous</a>
</li>
<cfloop index="Pages" from="0" to="#TotalPages#">
<cfoutput>
<cfset DisplayPgNo = Pages+1>
<cfif URL.PageId eq pages>
<li class="page-item">
<li class="page-item active">
<a class="page-link" href="##">#DisplayPgNo#</a>
</li>
<cfelse>
<li class="page-item"><a class="page-link" href="?PageId=#Pages#">#DisplayPgNo#</a></li>
</cfif>
</cfoutput>
</cfloop>
<li class="page-item">
<a class="page-link" href="#">Next</a>
</li>
</ul>
</nav>
上一个应该是这样的
<li class="page-item <cfif url.pageid LTE 1>disabled</cfif>">
<a class="page-link" href="?Pagesid=#(url.pageid - 1)#" tabindex="-1">Previous</a>
</li>
下一步的类似代码
这是一个完整的分页解决方案。尽管您可能希望将 paginateBS
函数放在可重用的应用程序变量中。
<!--- CURRENT PAGE --->
<cfparam name="url.page" default="1">
<cfparam name="request.PAGI_pagenum" default="#url.page#">
<!--- RECORDS PER PAGE (ON ALL PAGES) --->
<cfparam name="request.PAGI_allperpage" default="20">
<!--- RECORDS PER PAGE (ON FIRST PAGE) --->
<cfparam name="request.PAGI_firstperpage" default="#request.PAGI_allperpage#">
<!--- NUMBER OF PAGE LINKS AT A TIME --->
<cfparam name="request.PAGI_blocksof" default="5">
<cfset fromRecord = request.PAGI_firstperpage+(request.PAGI_allperpage*(request.PAGI_pagenum-1))-request.PAGI_allperpage>
<cfquery name="quotes" >
SELECT SQL_CALC_FOUND_ROWS first_name, last_name, email, id, datetime, published
FROM quotes
ORDER by datetime DESC
LIMIT #fromRecord#,
<cfif request.PAGI_pagenum is 1>
#request.PAGI_firstperpage#
<cfelse>
#request.PAGI_allperpage#
</cfif>
</cfquery>
<Cfquery name="result_count" >
SELECT FOUND_ROWS() as totalRecords
</cfquery>
<div class="pull-right" id="page">
<div class="pagination pagination-custom">
<ul>
<cfoutput>#paginateQuotes(totalRecords=result_count.totalRecords)#</cfoutput>
</ul>
</div>
</div>
<cffunction name="paginateBS">
<cfargument name="totalRecords" default="0">
<cfparam name="request.PAGI_attribute" default="">
<!--- 'TOP' IS THE LAST PAGE NUMBER LINK --->
<Cfset top = request.PAGI_blocksof * ceiling((request.PAGI_pagenum)/request.PAGI_blocksof)>
<cfoutput>
<!--- IF WE'RE PAST THE FIRST BLOCK OF PAGES, SHOW THE PREV LINK --->
<cfif request.PAGI_pagenum gt request.PAGI_blocksof>
<li>
<a href="?page=#top-request.PAGI_blocksof#<cfif request.PAGI_attribute neq ''>&#request.PAGI_attribute#=#request.PAGI_attributevalue#</cfif>">
<i class="icon-double-angle-left"></i>
</a>
</li>
</cfif>
<!--- LOOP THROUGH THE PAGES IN THIS BLOCK --->
<cfloop from="#top-request.PAGI_blocksof+1#" to="#top#" index="a">
<li <cfif request.PAGI_pagenum is not a>class="active"</cfif>>
<a href="?page=#a#<cfif request.PAGI_attribute neq ''>&#request.PAGI_attribute#=#request.PAGI_attributevalue#</cfif>">#a#</a>
</li>
<!--- IF WE'RE ON THE LAST BLOCK AND DON'T HAVE ENOUGH RECORDS TO COMPLETE THE BLOCK,
STOP CREATING THE LINKS FOR MORE PAGES, AND SET A FLAG TO NOT SHOW THE LINK FOR THE NEXT BLOCK --->
<cfif ((a-1) * request.PAGI_allperpage) + request.PAGI_firstperpage gte arguments.totalRecords>
<cfset arguments.noNext=1>
<cfbreak>
</cfif>
</cfloop>
<!--- IF WE RAN OUT OF RECORDS, DO NOT SHOW THE LINK FOR THE NEXT BLOCK OF PAGES --->
<cfif not isdefined("arguments.noNext")>
<li>
<a href="?page=#top+1#<cfif request.PAGI_attribute neq ''>&#request.PAGI_attribute#=#request.PAGI_attributevalue#</cfif>">
<i class="icon-double-angle-right"></i>
</a>
</li>
</cfif>
</cfoutput>
</cffunction>
我最终使用 https://datatables.net/ 分页,在这里发帖希望它能帮助像我这样的人:)
谢谢你们帮助我...