We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Building cfcase tags by looping within cfswitch.

brianmfalls
brianmfalls asked
on
Medium Priority
899 Views
Last Modified: 2012-06-21
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

Comment
Watch Question

CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
> 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..

Author

Commented:
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.
CERTIFIED EXPERT

Commented:

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?


CERTIFIED EXPERT
Most Valuable Expert 2015
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
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.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.