CFLib.org – Common Function Library Project

arrayGroupsOf(arrObj, intGroup [, padding])

Last updated February 4, 2010

Version: 1 | Requires: ColdFusion MX | Library: DataManipulationLib

 
Rated 1 time(s). Average Rating: 5.0

Description:
Splits or iterates over the array in number of groups, padding any remaining slots with custom strings unless it is empty. It's a big helper for the presentation layer where sometimes it's necessary to display data broken down into columns.

Return Values:
Returns an array.

Example:

view plain print about
<cfset ArrFruits = ["Orange""Apple""Peach""Blueberry"
                    "Blackberry""Strawberry""Grape""Mango"
                    "Clementine""Cherry""Plum""Guava"
                    "Cranberry"]
>


<table border="1">
    <cfoutput>
        <cfloop array="#arrayGroupsOf(ArrFruits, 5, 'not set')#" index="arrFruitsIX">
          <tr>
            <cfloop array="#arrFruitsIX#" index="arrFruit">
                <td>#arrFruit#</td>
            </cfloop>
          </tr>
        </cfloop>
    </cfoutput>
</table>

Parameters:

Name Description Required
arrObj Array to split up in groups. Yes
intGroup Number of items allowed on each group. Yes
padding What should it be filled with in case there's empty slots. No

Full UDF Source:

view plain print about
<!---
 Splits or iterates over the array in number of groups.
 
 @param arrObj      Array to split up in groups. (Required)
 @param intGroup      Number of items allowed on each group. (Required)
 @param padding       What should it be filled with in case there's empty slots. (Optional)
 @return Returns an array. 
 @author Marcos Placona (marcos.placona@gmail.com) 
 @version 1, February 4, 2010 
--->

<cffunction name="arrayGroupsOf" access="public" output="false" returntype="array">
    <cfargument name="arrObj" type="array" required="true" hint="An array object that will be split up in groups">
    <cfargument name="intGroup" type="numeric" required="true" hint="Number of items on each group">
    <cfargument name="padding" type="string" required="false" default=" " hint="What should it be filled with in case there's empty slots">
    
    <cfset var resArray = createObject("java""java.util.ArrayList").Init(arguments.arrObj) />
    <cfset var arrGroup = arrayNew(1) />
    <cfset var arrObjGroup = arrayNew(1) />
    <cfset var arrObjSize = resArray.size()>
    <cfset var subStart = 0>
    <cfset var subEnd = arguments.intGroup>
    <cfset var ii = "">
    <cfset var difference = "">
    <cfset var jj = "">
    
    <cfset arrGroupSize = ceiling(arrObjSize / arguments.intGroup)>
    <cfset arrArrayGroupSize = arrGroupSize * arguments.intGroup>
    
    <cfif arrArrayGroupSize GT arrObjSize>
        <cfset difference = arrArrayGroupSize - arrObjSize>
        <cfloop from="1" to="#difference#" index="ii">
            <cfset resArray.add(arguments.padding) />
        </cfloop>
    </cfif>
    
    <cfloop from="1" to="#arrGroupSize#" index="jj">            
        <cfset arrGroup = resArray.subList(subStart, subEnd)>        
        <cfset arrayAppend(arrObjGroup, arrGroup)>
        
        <cfset subStart = subStart + arguments.intGroup>
        <cfset subEnd = subEnd  + arguments.intGroup>
        <cfset arrGroup = arrayNew(1) />
    </cfloop>
    
    <cfreturn arrObjGroup>

</cffunction>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
4 day(s) ago

Will Belden Will Belden added
longTime
9 day(s) ago

James Sleeman James Sleeman added
quickSort
19 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
22 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson