[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

Building or creating dynamic drop down

Experts,

I need to build a dynamic dropdown list based on the selection of a other dropdown. Also I don't want to submit
the form since I have other data entry field on my form and don't want to lose the user entered data.
What is the best way to achive it using CF and/or Java Script. An example would really help to save the time since it is kind of urgent.

My first query is : select state from mydatabse.states
My second query is : select counties from mydatabase.counties where state = #state#

Thanks in Advance
0
Tpaul_10
Asked:
Tpaul_10
  • 4
  • 3
  • 2
  • +2
2 Solutions
 
Tpaul_10Author Commented:
Thanks for the quick reply and example srikanthmadishetti:
Right now I am not using or don't want to use the CFC. So, can I just use the fuction in that example and build my second drop down in the same page? with regular HTML instead of CFSELCT etc.?
0
 
gdemariaCommented:

cf_twoselectsrelated

a CF custom tag that creates the javascript for you..

All you do is write a query that has a join between both items you want to display.  For example,  a query "getProducts" is a list of products and the options available to that product (let's say SIZE : small, medium...)

Then you just fill in the fields :
   Name - the form's field name
   Display - the column that will be show to the user in the select tag
   Value - the column that will be the value of the select tag
   emptytext - what should appear when nothing is selected
   selected - the value of the field that should be shown when loaded
   formName - the name of the form the select tags are put into
   htmlBetween - any HTML that you want to appear between the two select tags, for example, maybe they are columns in a table so you would put </td><td> between the tags



<cf_twoselectsrelated query="getProducts"
   name1="product_id"
   name2="size_id"
   display1="productName"
   display2="optionName"
   value1="company_id"
   value2="annuity_id"
   emptyText1="- Select a Product -"
   emptyText2="- Select a Size"
   selected1="#variables.product_id#"
   selected2="#variables.size_id#"
   htmlBetween="</td><td>"
   formName="myForm"
   >

Open in new window

0
Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

 
Brijesh ChauhanStaff IT EngineerCommented:
>>Right now I am not using or don't want to use the CFC. So, can I just use the fuction in that example and build my second drop down in the same page? with regular HTML instead of CFSELCT etc.?

You can bind it to a CFM.. try below example

your form.cfm

<cfform name="UserForm" method="post" format="html">
  <select name="CategorySearch">
  	<option value="1">Test1</option>
    <option value="2">Test2</option>
   </select> 
   <cfselect width="50" name="SubCategories" bind="url:test.cfm?keyword={CategorySearch}" display="name" value="Aid" />
</cfform>

Open in new window


Create a file test.cfm which will have the query to pull the information for second select

   <cfset Category = queryNew("Aid, name")>
   <cfset queryAddRow(Category,1)>
   <cfset querySetCell(Category,'Aid',1)>
   <cfset querySetCell(Category,'name','Test1')>
   <cfset queryAddRow(Category,1)>
   <cfset querySetCell(Category,'Aid',2)>
   <cfset querySetCell(Category,'name','Test2')>
   

<cfquery name="getCatregory" dbtype="query">
	select Aid, name
    from Category
    where Aid = #url.keyword#
</cfquery>

<cfoutput>
#serializeJSON(getCatregory)#
</cfoutput>

Open in new window

0
 
srikanthmadishettiCommented:
@brijesh Good to see you in EE :)

@Tpaul
>>>Right now I am not using or don't want to use the CFC. So, can I just use the fuction in that example and build my second drop down in the same page? with regular HTML instead of CFSELCT etc.?


Is there any specific reason for not using the cfselect or cfc

First it will be just few lines of code and second cfc will help you to reuse this code any where .

You can use it any form which has the state and country drop downs .

0
 
Gurpreet Singh RandhawaWeb DeveloperCommented:
0
 
Tpaul_10Author Commented:
I have old cold fusion code and the site is going away soon. So, no CFCs and don't want to spend money for buying the code -:(

I may look in to writing a custom tag, but need to find out if the tables are related to have one query.
May be an example would be helpful.

0
 
gdemariaCommented:
I provided you with a custom tag:   cf_twoselectsrelated

 http://projects.nateweiss.com/nwdc/workcode.htm

> but need to find out if the tables are related to have one query

The would have to be related, wouldn't they?   Otherwise, you don't need two related select tags, you could just do two distinctly seperate tags, right?

0
 
Tpaul_10Author Commented:
Sorry about the delay and Thanks for all your inputs.
I was able to created a custom tag successfully with the exaple provided by gdemaria. THANKS.

now the problem is , if I go bak and edit my order, I should be able to pre-select the options which I have selected originally like following.
<option value="#myvalue#" <cfif #ListFind(preselectedValue,query2.value1)#> #SELECTED</cfif>>#myvalue#</option>                   

I am going to try it by adding another parameters to my customtag and use the similar condition like above. But if you have a better solution, please update me.

Thanks
0
 
gdemariaCommented:
the custom tag has two parameters selected1 and selected2 representing the current values of the two select tags
0
 
Tpaul_10Author Commented:
THANKS gdemaria.
Based on my requirements, your answer gave me the complete solution eventhough others have provided with different soluctions
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now