DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Data
  4. Multi-selects Related with CFAjaxProxy

Multi-selects Related with CFAjaxProxy

Brian Rinaldi user avatar by
Brian Rinaldi
·
Mar. 04, 08 · Interview
Like (0)
Save
Tweet
Share
6.80K Views

Join the DZone community and get the full member experience.

Join For Free

About a year ago I wrote a post about how to do Multi-selects Related with AjaxCFC. Recently someone was saying they found it useful, and I mentioned that if I were doing it today I would likely just use ColdFusion 8's new <cfajaxproxy> tag. Therefore I have decided to show how the identical example could be done in ColdFusion 8. In the end, I think it is easier to understand and to implement.

Compared to the prior example , the component code is nearly identical, simply removing the extends attribute of the Ajax.cfc component. It also specifies the method as remote, whereby with AjaxCFC we could set it to private:

<cfcomponent>
<cffunction name="getFillQuery" output="no" access="remote" returntype="query">
<cfargument name="itemID" required="true" type="numeric" />

<!--- I am faking the query for test purposes --->
<cfset var selectQuery = queryNew("display,id") />
<!--- this would be where you do your real query --->
<cfset var i = 0 />
<cfloop from="1" to="5" index="i">
<cfset queryAddRow(selectQuery) />
<cfset querySetCell(selectQuery,"display","selected #arguments.itemID# option #i#") />
<cfset querySetCell(selectQuery,"id",i) />
</cfloop>

<cfreturn selectQuery />
</cffunction>
</cfcomponent>

As before, the component is very basic. I have a function that returns a query that, if this were real, would be filtered based upon an ID (or whatever criteria you want to send). Now, let's look at the form:

<cfajaxproxy cfc="selectBoxes" jsclassname="SelectBoxes">
<html>
<head>
<script language="javascript">
var formItemToFill;
function callFill(thisSelect,target) {
formItemToFill = target;
selectBoxes = new SelectBoxes();
selectBoxes.setCallbackHandler(fill);
selectBoxes.getFillQuery(thisSelect.options[thisSelect.selectedIndex].value);
}
function fill(r) {
var i = 0;
var foo2 = document.forms['myForm'].foo2;
foo2.options.length = 0;
for (i in r.DATA) {
foo2.options[i] = new Option(r.DATA[i][0],r.DATA[i][1]);
}
}
</script>
</head>
<body onload="callFill(document.forms['myForm'].foo1,'foo2');">
<form name="myForm">
<select name="foo1" onchange="callFill(this,'foo2');">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<br />
<select name="foo2" id="foo2"></select>
</form>
</body>
</html>

The initial <cfajaxproxy> tag establishes the component that I will be using and makes all the remote methods available (in case only the one) via JavaScript under the variable class name I specify (i.e. SelectBoxes). The callFill() function is called when a new option is selected in my foo1 select box or when the page loads to allow you to pre-select an option. The callFill() function simply creates an instance of our SelectBoxes proxy, establishes the callback method (fill()) and then calls the getFillQuery() function within the class. You may notice that I have set a global JavaScript variable here to allow you to easily set the target select box you would like to fill.

Once the data is returned, we remove all the existing options from foo2 and loop through the DATA array adding each result as a new option in the select box. Even without the built-in AjaxCFC utility functions, this is a very simple task.

As you can see, this is extremely easy once you are familiar with the new <cfajaxproxy> tag. You could also easily expand this to add additional related select boxes as necessary.

Data (computing) JavaScript remote Task (computing) Form (document) POST (HTTP) Database Attribute (computing) Data structure

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • A Beginner's Guide to Infrastructure as Code
  • What To Know Before Implementing IIoT
  • DevOps vs Agile: Which Approach Will Win the Battle for Efficiency?
  • Secure APIs: Best Practices and Measures

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: