Connecting Tech Pros Worldwide Forums | Help | Site Map

Grouping & Sorting in Coldfusion

Member
 
Join Date: Mar 2007
Posts: 94
#1: Nov 17 '08
Hi there,

I'm replicating the look of an Access form in Coldfusion

In Access I can create group headers to sort my data, so all the jobs under a particular site appear under that site name - which appears only once.

I'm using a cfloop query to get all my results in CF so my site name is repeated constantly. - like so

Site: MYSITE1
Job1

Site:MYSITE1
Job2


How do I get it to appear only once, as a header for all the jobs underneath it? Obviously the loop will need to go on and output the jobs under the next site, with that only having one header as well. like so...

Site: MYSITE1
Job1
Job2
Job3

Site: MYSITE2
Job1
Job2

Thanks for your help!

acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Nov 17 '08

re: Grouping & Sorting in Coldfusion


Can you post the cfloop code.
Member
 
Join Date: Mar 2007
Posts: 94
#3: Nov 18 '08

re: Grouping & Sorting in Coldfusion


Hi Acoder,

I certainly can:

Expand|Select|Wrap|Line Numbers
  1. <tr>
  2.             <cfloop query="ManagerJobsIn">
  3.             <cfoutput>
  4.             <tr>
  5.             <td colspan="14" style="color: red; font-weight: 600;">#ClientFK#</td>
  6.             </td>    
  7.  
  8.             <tr>
  9.             <td colspan="14" style="color: red; font-weight: 600;">#SiteFK#</td>
  10.             </td>    
  11.  
  12.             <tr bgcolor="###iif(currentrow MOD 2,DE('ffffff'),DE('efefef'))#">
  13.             <td width="20%">#AssetClassFK#</td>
  14.             <td>#ManufacturerFK#</td>
  15.             <td>#ModelFK#</td>
  16.             <td>#PowerRating#</td>
  17.             <td>#ClientWONO#</td>
  18.             <td>#WorkshopWONO#</td>
  19.             <td>#dateFormat(WoRecdate,'dd/mm/yyyy')#</td>
  20.             <td>#dateFormat(QuotePDate,'dd/mm/yyyy')#</td>
  21.             <td>#numberFormat(Price, "(______,.00")#</td>
  22.             <td>#dateFormat(QuoteACDate, 'dd/mm/yyyy')#</td>
  23.             <td>#ResponseLevelFK#</td>
  24.             <td>#dateFormat(ActualCompDate, 'dd/mm/yyyy')#</td>
  25.             <td>#AssignedToFK#</td>
  26.             </tr>
  27.  
  28.  
  29.             </cfoutput>
  30.             </cfloop>
  31.             </table>
  32.  
  33.  
Cheers
Neil
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Nov 18 '08

re: Grouping & Sorting in Coldfusion


Assuming the sites are sorted and grouped in the query, e.g.
Expand|Select|Wrap|Line Numbers
  1. Site    Job
  2. 1       1
  3. 1       2
  4. 1       3
  5. 2       1
  6. 2       2
you can use a variable to store the current site, e.g. MYSITE1. Then compare with the SiteFK value. If it's equal, don't add a header. If it's a new site, add a header and set that variable to this new value.
Member
 
Join Date: Mar 2007
Posts: 94
#5: Nov 18 '08

re: Grouping & Sorting in Coldfusion


Acoder,

That's great - I'll give it a go.

Cheers
Neil
Newbie
 
Join Date: Nov 2008
Posts: 5
#6: Nov 20 '08

re: Grouping & Sorting in Coldfusion


You can also use cfoutput's group attribute. Just order your results by "Site" first. Then use nested cfoutput tags and group by "Site" as well.

Expand|Select|Wrap|Line Numbers
  1. <cfquery ..>
  2.        SELECT  Site, Job,  ... other columns
  3.        FROM     Thetable
  4.        ORDER BY Site, Job, ... other columns
  5. </cfquery>
  6.  
  7. <cfoutput query="yourQuery" group="Site">
  8.           #Site#<br>
  9.           <cfoutput>
  10.                #Job#<br>
  11.           </cfoutput>
  12. </cfoutput>
  13.  
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#7: Nov 20 '08

re: Grouping & Sorting in Coldfusion


Nice tip. Thanks for sharing!
Reply