?
Solved

Building cfcase tags by looping within cfswitch.

Posted on 2011-04-26
6
Medium Priority
?
820 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

0
Comment
Question by:brianmfalls
  • 3
  • 2
6 Comments
 
LVL 39

Accepted Solution

by:
gdemaria earned 1600 total points
ID: 35469593
I don't think you need the switch because the code in each cfcase is the same, you just need to switch it to variables instead of hard coding it...


<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, "|")>

	    <cfif local.indentLevel gt 1 and local.parent NEQ (local.indentLevel - 1)>
	    	<ul style=" list-style: none; margin-left: 14px; ">
	    </cfif>
	    <cfloop from="#local.indentLevel#" to="#local.parent#" index="i">
	        </ul>
     	</cfloop>
	    <cfset local.parent = local.indentLevel - 1>

Open in new window

0
 
LVL 39

Expert Comment

by:gdemaria
ID: 35469610
> 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..
0
 
LVL 1

Author Comment

by:brianmfalls
ID: 35470326
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.
0
How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

 
LVL 39

Expert Comment

by:gdemaria
ID: 35470411

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?


0
 
LVL 52

Assisted Solution

by:_agx_
_agx_ earned 400 total points
ID: 35470598
cfswitch/cfcase over cfif due to performance gains

IMO efficiency has more to do with *what* the code is comparing and how many times, rather than whether cfif or cfswitch is used.  With your typical page even hundreds of comparisons are barely noticeable.  But once you start doing anything that involves thousands of loops it's time to re-evaluate the approach.  Looping will kill performance far quicker than any minuscule diff cfif vs. cfswitch ;-)
0
 
LVL 1

Author Comment

by:brianmfalls
ID: 35470653
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.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Screencast - Getting to Know the Pipeline
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question