Search code examples
oracle-databasecoldfusioncoldfusion-9

Dynamic rowspan in a Table Coldfusion


Just to give some background. We have users in different parts of the region. Our application sends out reports in emails which can be accessed via a URL. This way we keep a track on who accessed the report and various other attributes.

Now, as part of the statistics, I am trying to display the attributes in a HTML table. I have the query which contains the details about the "Region Name", "UserID", "ReportName", "AccessCount", "ViewDate" etc. The requirement is to span the "Region Name" across all its rows.

For ex. 10 different users from Melbourne region have access a report XYZ. My table should have Melbourne with rowspan = "10" and each row having each users' details. I don't want Melbourne to repeat 10 times in the table.

I've tried using the <cfoutput group="RegionName" tag along with the HTML table but the table is not well-formed.

How can I achieve this?


Solution

  • You should be able to achieve this and you were along the right direction by looking at the groupby attribute (actually the attribute is group=""). The cfml won't look very pretty though (I prefer cfscript and likely would do the following with a few functions). Something like the following should render a well-formed table with the RegionName cell spanning multiple rows in a well-formed manner, just adjust with classes / formatting etc. as you see fit!

    <!---
        Make sure that myQuery is ordered by
        RegionName ASC before anything else to
        ensure the group by works as intended
    --->
    <table>
        <thead>
            <tr>
                <th>Region</th>
                <th>User</th>
            </tr>
        </thead>
        <tbody>
            <cfoutput query="myQuery" group="RegionName">
                <!--- set up an array to hold the users for this region --->
                <cfset arrOfUsers = ArrayNew(1)>
                <cfoutput>
                    <cfset ArrayAppend(arrOfUsers,'<td>'&myQuery.UserName&'</td>)>
                </cfoutput>
                <!--- render time, use the array just generated so we know how many users are in this group --->
                <cfloop from="1" to="#ArrayLen(arrOfUsers)#" index="i">
                    <tr>
                        <cfif i EQ 1>
                            <td rowspan="#ArrayLen(arrOfUsers)#">#myQuery.RegionName#</td>
                        </cfif>
                        #arrOfUsers[i]#
                    </tr>
                </cfloop>
            </cfoutput>
        </tbody>
    </table>