Custom Search

Thursday, January 24, 2008

Tutorial : Pagination (Component based)

hi,
this is the final part of pagination example.here i use object oriented coldfusion.
this create a pagination object which you can call anywhere in your application.

here you need to create to file named pagination.cfc and pagination.cfm
for simpler issue you should place the both file in a common folder.
the code goes bellow.

pagination.cfc
 <cfcomponent author="cfsuman@gmail.com">
  <cffunction name="Pagination" access="remote" returntype="any" output="yes">
   <cfargument name="dsname" required="yes">
   <cfargument name="TableName" required="yes">
   <cfargument name="MaxRow" default="10" required="no">
   
   <cfparam name="url.PageNumber" default="1">
   
   <cfif NOT IsDefined("session.qSelectQuery") OR IsDefined("url.CreateNewSession")>
    <cfquery name="session.qSelectQuery" datasource="#dsname#">
  SELECT *
  FROM #TableName#
    </cfquery>
   </cfif>
   
   <cftable query="session.qSelectQuery" startrow="#((url.PageNumber*MaxRow)-MaxRow)+1#" maxrows="#MaxRow#" colheaders="yes">
    <cfloop list="#session.qSelectQuery.ColumnList#" index="Column">
   <cfcol text="#Evaluate(Column)#" header="#Column#">
    </cfloop>
   </cftable>
   
   <cfoutput>
    <cfloop from="1" to="#Ceiling(session.qSelectQuery.RecordCount/MaxRow)#" index="i">
   <a href="#CGI.SCRIPT_NAME#?PageNumber=#i#">#i#</a>
    </cfloop>
    .........page #url.PageNumber# of #Ceiling(session.qSelectQuery.RecordCount/MaxRow)#......
    <a href="#CGI.SCRIPT_NAME#?CreateNewSession">run the select query again</a>
   </cfoutput>
  </cffunction>
 </cfcomponent>
   
pagination.cfm
 <cfscript>
  PaginationObject=Createobject("Component","pagination");
 </cfscript>
 
 <cfset PaginationObject.Pagination("suman","admin")>
   
hei now run the pagination.cfm file in your browser.
whats hapend?yes this call object oriented web programming.