maketree(query, unique, parent)
Last updated February 17, 2005
Version: 1 | Requires: ColdFusion 5 | 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:
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:
<cfscript>
/**
* 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;
}
</cfscript>
Search CFLib.org
Latest Additions
Shawn Porter added
DeMoronize
3 hour(s) ago
Chris Carey added
readPropertiesFi...
1 day(s) ago
Randy Johnson added
lastDayofWeek
3 day(s) ago
Frank Marion added
sitemapPing
7 day(s) ago
Top Rated
QuickSort
Rated 5.0, 3 time(s)
indentXml
Rated 5.0, 3 time(s)
queryColumnsToSt...
Rated 5.0, 3 time(s)
generateSsccAsn
Rated 5.0, 3 time(s)