Wow. I have been asked to add a 3rd level to a tree menu for a company. Below is what I found had created about 6 months ago. And while it works, there is no way I am going to add another cfloop hack to it. All my feelings tell me this just bad code and to redo.
I cannot change the databased nor have the results returned in anything else other than a query. now you know the way it has to be, what are my options here???
Each record in the DB has NO relationship, the code below just looks at the name of the widget and works out if it is a parent or not based on the :
examples of widgetName
1= Compliance:
2= Compliance: Document Library:
3= Compliance: Document Library: All Firms
so above says that widget 2 should be 2 levels down. look at the code you will see in a very bad way what has been done….
<cfoutput>
<ul id="root" class="menu">
<cfloop array="#rc.widgets.getPageRecords()#" index="local.widgets">
<cfif listLen(local.widgets.getwidgetName(),":") eq 1>
<li>
<a href='javascript:void(0);' childid = '#local.widgets.getwidgetID()#' class='cat_close category'> </a>
<a href='javascript:void(0);'>#local.widgets.getwidgetName()# - #listLen(local.widgets.getwidgetName(),":")#</li></a>
<cfset wordToFind = local.widgets.getwidgetName()>
<ul id='#local.widgets.getwidgetID()#'>
<cfloop array="#rc.widgets.getPageRecords()#" index="local.widgets">
<cfif listLen(local.widgets.getwidgetName(),":") eq 2 AND #find(wordToFind,local.widgets.getwidgetName())#>
<li>
<a href='javascript:void(0);' childid = '#local.widgets.getwidgetID()#' class='cat_close category'> </a>
<a href='javascript:void(0);'>#local.widgets.getwidgetName()# - #listLen(local.widgets.getwidgetName(),":")#</li></a>
</li>
</cfif>
</cfloop>
</ul>
</cfif>
</cfloop>
</ul>
</cfoutput>
I would use a recursive function to do this, that would give you unlimited future compatibility. I’m not sure what the data you are working with looks like, but it seems as though it should be possible.