CFLib.org – Common Function Library Project

maketree(query, unique, parent)

Last updated February 17, 2005

author

Qasim Rasheed

Version: 1 | Requires: CF5 | Library: DataManipulationLib

Description:
This function will take a query that is in a LEGAL parent/child relationship and sort it. The entire query will be sorted and an additional field called "sortlevel" will be added to specify the level of a particular item.

Return Values:
Returns a query.

Example:

<cfscript>
	q = querynew( 'id,text,parent' );
	queryaddrow( q );
	querysetcell( q, 'id', 1 );
	querysetcell( q, 'text', 'One' );
	querysetcell( q, 'parent', 0 );
	queryaddrow( q );
	querysetcell( q, 'id', 2 );
	querysetcell( q, 'text', 'Two' );
	querysetcell( q, 'parent', 1 );
	queryaddrow( q );
	querysetcell( q, 'id', 3 );
	querysetcell( q, 'text', 'Three' );
	querysetcell( q, 'parent', 0 );
</cfscript>
<cfset temp = maketree( q, 'id', 'parent' )>
<cfdump var="#temp#">

Parameters:

Name Description Required
query Query to be sorted. Yes
unique Name of the column containing the primary key. Yes
parent Name of the column containing the parent. Yes

Full UDF Source:

/**
 * This function is a UDF for maketree custom tag developed by Michael Dinowitz.
 * 
 * @param query 	 Query to be sorted. (Required)
 * @param unique 	 Name of the column containing the primary key. (Required)
 * @param parent 	 Name of the column containing the parent. (Required)
 * @return Returns a query. 
 * @author Qasim Rasheed (qasimrasheed@hotmail.com) 
 * @version 1, February 17, 2005 
 */
function maketree( query, unique, parent ){
	var current = 0;
	var path = 0;
	var i = 0;
	var j = 0;
	var items = "";
	var parents = "";
	var position = "";
	var column = "";
	var retQuery = querynew( query.columnlist & ',sortlevel' );
	for (i=1;i lte query.recordcount;i=i+1)
		items = listappend( items, query[unique][i] );
	for (i=1;i lte query.recordcount;i=i+1)
		parents = listappend( parents, query[parent][i] );
	
	for (i=1;i lte query.recordcount;i=i+1){
		queryaddrow( retQuery );
		position = listfind( parents, current );
		while (not position){
			path= listrest( path );
			current = listfirst( path );
			position = listfind( parents, current );
		}
		for (j=1;j lte listlen( query.columnlist ); j=j+1){
			column = listgetat( query.columnlist, j );
			querysetcell( retQuery, column, evaluate( 'query.'&column&'[position]') );
		}
		querysetcell( retQuery, 'sortlevel', listlen( path ) );
		current = listgetat( items, position );
		parents = listsetat( parents, position, '-' );
		path = listprepend( path, current);
	}
	return retQuery;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson