Link to home
Start Free TrialLog in
Avatar of brianmfalls
brianmfallsFlag for United States of America

asked on

Building cfcase tags by looping within cfswitch.

I am not terribly pleased with this bit of code.  If I could use a cfloop in a cfswitch tag, I could consolidate it, and regulate the depth dynamically by using a variable to set the loop depth.  But since ColdFusion doesn't allow us to use anything but cfcase tags within cfswithch statements, I can't.  How can I consolidate this bit and improve performance?  

<!------------------------- 
	the index 'thisList' is a bar delemited list
	the list is ordered as follows: (positions 1-9) 
		1: etListID (exact target id) | 2: listName | 3: listModified (date/time) | 4: listCount | 
			5: listID (our ID) | 6: listType (depricated) | 7: seletIndivDefaultChecked | 8: indentLevel | 9: listInfoQueryID
				 --------------------->

<ul id="recipientListsTree">

	<cfloop list="#exactTargetLists#" index="thisList">

		<!--- local settings --->
		<cfset local.id = listGetAt(thisList, 1, "|")>
		<cfset local.label = listGetAt(thisList, 2, "|")>
		<cfset local.total = listGetAt(thisList, 4, "|")>
		<cfset local.listID = listGetAt(thisList, 5, "|")>
		<cfset local.indentLevel = listGetAt(thisList, 8, "|")>

		<cfoutput>

			<cfswitch expression="#local.indentLevel#">

				<cfcase value="1">
					<cfloop from="1" to="#local.parent#" index="i">
						</ul>
					</cfloop>
					<cfset local.parent = 0>
				</cfcase>

				<cfcase value="2">
					<cfif NOT local.parent>
						<ul style=" list-style: none; margin-left: 14px; ">
					</cfif>
					<cfloop from="2" to="#local.parent#" index="i">
						</ul>
					</cfloop>
					<cfset local.parent = 1>
				</cfcase>

				<cfcase value="3">
					<cfif local.parent NEQ 2>
						<ul style=" list-style: none; margin-left: 14px; ">
					</cfif>
					<cfloop from="3" to="#local.parent#" index="i">
						</ul>
					</cfloop>
					<cfset local.parent = 2>
				</cfcase>

				<cfcase value="4">
					<cfif local.parent NEQ 3>
						<ul style=" list-style: none; margin-left: 14px; ">
					</cfif>
					<cfloop from="4" to="#local.parent#" index="i">
						</ul>
					</cfloop>
					<cfset local.parent = 3>
				</cfcase>

				<cfcase value="5">
					<cfif local.parent NEQ 4>
						<ul style=" list-style: none; margin-left: 14px; ">
					</cfif>
					<cfloop from="5" to="#local.parent#" index="i">
						</ul>
					</cfloop>
					<cfset local.parent = 4>
				</cfcase>

				<cfcase value="6">
					<cfif local.parent NEQ 5>
						<ul style=" list-style: none; margin-left: 14px; ">
					</cfif>
					<cfloop from="6" to="#local.parent#" index="i">
						</ul>
					</cfloop>
					<cfset local.parent = 5>
				</cfcase>

				<cfcase value="7">
					<cfif local.parent NEQ 6>
						<ul style=" list-style: none; margin-left: 14px; ">
					</cfif>
					<cfloop from="7" to="#local.parent#" index="i">
						</ul>
					</cfloop>
					<cfset local.parent = 6>
				</cfcase>

				<cfcase value="8">
					<cfif local.parent NEQ 7>
						<ul style=" list-style: none; margin-left: 14px; ">
					</cfif>
					<cfloop from="8" to="#local.parent#" index="i">
						</ul>
					</cfloop>
					<cfset local.parent = 7>
				</cfcase>

				<cfcase value="9">
					<cfif local.parent NEQ 8>
						<ul style=" list-style: none; margin-left: 14px; ">
					</cfif>
					<cfloop from="9" to="#local.parent#" index="i">
						</ul>
					</cfloop>
					<cfset local.parent = 8>
				</cfcase>

				<cfcase value="10">
					<cfif local.parent NEQ 9>
						<ul style=" list-style: none; margin-left: 14px; ">
					</cfif>
					<cfloop from="9" to="#local.parent#" index="i">
						</ul>
					</cfloop>
					<cfset local.parent = 10>
				</cfcase>

			</cfswitch>
			
			<li>
				<div class="floatRight">#local.total# Total</div>
				<input id="#local.id#" name="#local.label#" type="checkbox" value="#local.listID#"
					onmouseover="Tip('Check this box to include this group of recipients.');" onmouseout="UnTip();"
					<cfif listFindNoCase(getEmailContent.selectedExistingLists_idList, local.listID, "|")> checked</cfif>
					>
					#local.label#
		</cfoutput>
		
	</cfloop>

</ul>

Open in new window


I tried the following, which works well, but I had hoped to avoid using cfif/cfelse where possible in order to keep performance up.  Thoughts?
<cfif NOT local.indentLevel>
	<cfloop from="1" to="#local.parent#" index="i">
		</ul>
	</cfloop>
	<cfset local.parent = 0>
</cfif>

<cfloop from="1" to="7" index="hierarchy">

	<cfif local.indentLevel EQ hierarchy>
	
		<cfif local.parent>
			<ul style=" list-style: none; margin-left: 14px; ">
		</cfif>
		
		<cfloop from="#hierarchy#" to="#local.parent#" index="i">
			</ul>
		</cfloop>
		
		<cfset local.parent = hierarchy>
		
	</cfif>
	
</cfloop>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of gdemaria
gdemaria
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> I tried the following, which works well, but I had hoped to avoid using cfif/cfelse where possible in order to keep performance up.  

CFIF statements are not a performance hit..
Avatar of brianmfalls

ASKER

Are you sure?  I read an article years back which touted cfswitch/cfcase over cfif due to performance gains.  The loop you see will at best loop over a hundred times, and at worst, it will loop several thousand times, which is why I tried with the case statement the first time.

Your try at it, while it did not throw an exception, failed to close the lists properly, there by nesting the lists inappropriately.   It's a good idea though.  I am going to play around with it here in a bit and see if I can make it work.

I agree that cfswitch is probably better than CFIF/CFELSIF if there are a certain number of conditions, but in this case, it seems you only have one CFIF condition or two..


The code probably just needs some tweaking to make it line-up correctly.  


If it will load several thousand, I wonder if there is another approach over loading thousands of options on a page; perhaps autosuggest or something?


SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Agreed, to both of your comments.  In a perfect world, we could look at another approach.  But then legacy applications are one of the world's imperfections.  This loop will not loop thousands of records on a regular basis.  The average is about two hundred...    Thank you both for your input.