Skip to main content
Known Participant
June 16, 2010
Question

CFoutput group with pagination

  • June 16, 2010
  • 2 replies
  • 1782 views

Hi,

I've a db table with lot of records that I'd like to create pagination but with cfoutput group.  I'm wondering if there is code examples or any suggestions on how would do it.

Here is a psedo code:

<table>

<thead>

     <th>id</th>

     <th>name</th>

     <th>company</th>

</thead>

<cfoutput query="getdata" group="name">

     <tr>

          <td>Name: #name#</td>

     </tr>

     <cfoutput group="">

     <tr>

          <td>#id#</td>

          <td>#company#</td>

     </tr>

     </cfoutput>

</cfoutput>

</table>

Thanks.

    This topic has been closed for replies.

    2 replies

    Participant
    June 17, 2010

    It's a hairy problem. I've been fighting with it for years..sad huh?

    Anyway, I believe I have a decent solution using a session variable. I'll try to explain:


    Start by setting up some variables:

    <cfparam name="iPageSize" default="4">
    <cfparam name="iStartRow" default="1">
    <cfparam name="myix" default="1">
    <cfparam name="iPageNbr" default="1">


    <cfif NOT IsDefined('SESSION.MMStartRow')>
        <cfset SESSION.MMStartRow = ArrayNew(1)>
        <cfset SESSION.MMStartRow[1] = 1>
        <cfset iPageNbr = 1>
    </cfif>

    Use the session variable in your cfoutput tag:

    <cfoutput query="qryMembers" group="MemberID" StartRow="#SESSION.MMStartRow[iPageNbr]#" maxrows="#iPageSize#">
         <cfset iLastRow = CurrentRow>
          <cfset iLastCount = 0>
          <cfoutput>
                 <cfset iLastCount = iLastCount + 1>
         </cfoutput>

         <!--- Your output goes here --->


    </cfoutput>

    Setup the paging:

    <cfif qryMembers.recordcount GT 0>
          <!--- get the next start row --->
          <cfset iNextStart = iLastRow + iLastCount>
          <cfset iNextPage = iPageNbr + 1>
          <!--- store the iStartRow in a SESSION variable --->
          <cfset SESSION.MMStartRow[iNextPage] = iNextStart>
                   
           <!--- determine nbr of pages taking into account group attribute --->
           <cfoutput query="qryMembers" group="MemberID">
                  <cfset iDisplayCount = iDisplayCount + 1>   
           </cfoutput>
                           
           <!--- how many pages of data do we have? --->
           <cfset iNbrPages = Ceiling(iDisplayCount / iPageSize)>
                       
          <cfoutput>
          <cfif SESSION.MMStartRow[iPageNbr] neq 1>
                  <a href="member_mnt.cfm?iPageNbr=#Evaluate(iPageNbr-1)#" class="rolln">&lt;&lt; prev</a> 
           </cfif>

              
            <!--- display each page number --->
            <cfloop index="pix" from="1" to="#iNbrPages#">
                   <cfif pix eq iPageNbr>
                          #pix# 
                   <cfelse>
                          <a href="member_mnt.cfm?iPageNbr=#pix#" class="rollnu">#pix#</a> 
                   </cfif>
            </cfloop>

                 
            <cfif iPageNbr lt iNbrPages>
                   <cfset variStartRow = myix * iPageSize  + 1>
                   <cfset nextix = myix + 1>
                   <a href="member_mnt.cfm?iPageNbr=#Evaluate(iPageNbr+1)#" class="rolln">next &gt;&gt;</a> 
           </cfif>

               
           </cfoutput>   
    <cfelse>
           There are currently no records to display
    </cfif> 

    Make sure you enable session variable is your application.cfc
       
    <cfset this.sessionmanagement = "yes">
        <cfset this.sessionTimeout = #CreateTimeSpan(0, 0, 30, 0)#>

    I hope this helps.

    Inspiring
    June 16, 2010

    G'day

    You probably want to stretch the TD with the NAME in it over two cells, given the inner loop has two cells per row.

    Also on the inner <cfoutput> you don't want a GROUP attribute at all.

    The docs have an example of this:

    http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7ff6.html

    --

    Adam

    Known Participant
    June 16, 2010

    sorry, my mistakes when i type but i'd like to see how pagination would work with cfoutput group.  any examples?  thanks.

    Inspiring
    June 16, 2010

    You mean other than the example in the docs I pointed you at?

    Or... rather than how to use the GROUP attribute to loop over groupings, do you mean something specific to the concept of pagination?  Web pages don't have a concept of page breaks, so I don't really know what you mean unless you elaborate somewhat.  The example in the docs shows you how to do [something] on each change of the grouped column... ?

    Do you just mean showing n records out of m on a page, and having next/prev links or something (like on a Google search results screen)?  I don't think CFOUTPUT grouping has any relevance to that.  And depending on the nature of your data, the answer will differ.

    Let's start by clarifying what you're actually on about...

    --

    Adam