Solved

Requery chained select box after parent insert.

Posted on 2009-04-01
12
839 Views
Last Modified: 2012-05-06
This question is a continuation of this question... http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/Cold_Fusion_Markup_Language/Q_24278868.html

I've got 2 select boxes.  sale_buyer_id & sale_buyer_yard_id.  sale_buyer_yard_id is chained to sale_buyer_id so when sale_buyer_id changes, sale_buyer_yard_id requerys using the new value in sale_buyer_id.

Here is what the code looks like in the form.

 
    <cfselect name="sale_buyer_id" id="sale_buyer_id" size="1" bind="cfc:addWindow.getSaleBuyers4cfselect()" bindonload="true" display="sale_buyer_Name" value="sale_buyer_id"  />


<cfselect name="sale_buyer_yard_id" id="sale_buyer_yard_id" bind="cfc:addWindow.getSaleBuyerYards4cfselect({sale_buyer_id})" bindonload="false" display="sale_buyer_yard" value="sale_buyer_yard_id" />


I am also using CFWINDOW to insert records on the fly.  Everything is working great except 1 thing.  When I add a new sale_buyer_id and the new value records gets returned to the sale_buyer_id select box, the sale_buyer_yard_id does not requery.  I need it to requery for the new value.  

See code below.  Maybe the getSaleBuyerYards4cfselect function needs to be fired with the onHideAddSaleBuyer function?

I am also using this form to edit records so I need to be able to set an initial value for both select boxes.  How might I do that?



<!---                             CFWINDOW CODE FOR SALE_BUYER_ID INSERT                            --->

 

<!--- create an ajax proxy to the CFC --->

<cfajaxproxy cfc="addWindow" jsclassname="salebuyerProxy">

 

<!--- js functions --->

<script type="text/javascript">

var openWindow = function(name,title,url,options){

 ColdFusion.Window.create(name, title, '', options);

 ColdFusion.navigate(url, name);

};

 

<!--- Functions specific to adding a new SALE BUYER --->

var addSaleBuyer = function(){ //opens add_Salebuyer.cfm page in a cfwindow for adding new auction data

 openWindow('sale_buyer_id_window', 'Add Sale Buyer', 'add_Salebuyer.cfm', {center:true,height:650,width:400,closable:true,dragable:true,resizable:true,modal:true,initshow:true});

};

 

var getSaleBuyers = function(){ //uses ajax proxy to populate cfselect

 var p = new salebuyerProxy();

 var populate = p.getSaleBuyers4cfselect();

 return populate;

};

 

var onHideAddSaleBuyer = function(name){ //refreshes the cfselect

 ColdFusion.Bind.assignValue('sale_buyer_id', 'value', getSaleBuyers());

};

 

 

<!---                           CFC to query the sale_buyer_yard_id select box                              ---->

 

<!--- Sale Buyer Yards CFC to fill the cfselect query --->

<cffunction name="getSaleBuyerYards4cfselect" access="remote" returntype="query" output="false">

<cfargument name="sbid" required="no" default="0">

<cfset var qGetSaleBuyerYards="">

 

<cfquery name="qGetSaleBuyerYards" datasource="#request.dsn#">

SELECT sale_buyer_yard_id, sale_buyer_yard

FROM sale_buyer_yard

<cfif arguments.sbid gt 0>

WHERE sale_buyer_id = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.sbid#">

</cfif>

ORDER BY sale_buyer_yard

</cfquery>

 

<cfreturn qGetSaleBuyerYards />

 

</cffunction>

Open in new window

0
Comment
Question by:MFredin
  • 6
  • 6
12 Comments
 
LVL 27

Expert Comment

by:azadisaryev
Comment Utility
if i remember correctly, you have a onHideAddSaleBuyerYard() js function in there, something like:

var onHideAddSaleBuyerYard = function(name){ //refreshes the cfselect
 var p = new salebuyeryardProxy();
 var sbid = ColdFusion.getElementValue('sale_buyer_id');
 ColdFusion.Bind.assignValue('sale_buyer_yard_id', 'value', p.getSaleBuyerYards4cfselect(sbid));
};


if so, see if changing your onHideAddSaleBuyer js function to the following works (i can't test it right now):

var onHideAddSaleBuyer = function(name){ //refreshes the cfselect
 ColdFusion.Bind.assignValue('sale_buyer_id', 'value', getSaleBuyers());
 onHideAddSaleBuyerYard();
};

Azadi
0
 
LVL 27

Expert Comment

by:azadisaryev
Comment Utility
not directly related to your question, just a suggestion:

why don;t you, like with the onHideAddSaleBuyerYard function, change your onHideAddSaleBuyer to use remote proxy, and get rid of your getSaleBuyers() function altogether?

var onHideAddSaleBuyer = function(name){ //refreshes the cfselect
 var b = new salebuyerProxy();
 ColdFusion.Bind.assignValue('sale_buyer_id', 'value', b.getSaleBuyers4cfselect());
 onHideAddSaleBuyerYard();
};

Azadi
0
 

Author Comment

by:MFredin
Comment Utility
I got rid of the getSaleBuyers() function and added the new onHideAddSaleBuyer code.  For some reason it still isn't refreshing when I add a new record.  
0
 
LVL 27

Expert Comment

by:azadisaryev
Comment Utility
while i am rigging up a test suite, try this:

var onHideAddSaleBuyer = function(name){ //refreshes the cfselect
 var b = new salebuyerProxy();
 ColdFusion.Bind.assignValue('sale_buyer_id', 'value', b.getSaleBuyers4cfselect());
 alert(document.getElementById('sale_buyer_id').value);
};

this should pop up an alert box with current selected value of your sale_buyer_id cfselect. is this the value of the newly added sale buyer?

Azadi
0
 

Author Comment

by:MFredin
Comment Utility
It pops up an alert with the id of the first returned record of the getSaleBuyers4cfselect function.  Then, after I click OK on the alert, sale_buyer_id switches to the newly added record, but doesn't requery the sale_buyer_yard_id.

<cffunction name="getSaleBuyers4cfselect" access="remote" returntype="query" output="false">
<cfset var qGetSaleBuyers="">

<cfquery name="qGetSaleBuyers" datasource="#request.dsn#">
SELECT sale_buyer_id, sale_buyer_name
FROM sale_buyer
ORDER BY sale_buyer_name
</cfquery>

<cfreturn qGetSaleBuyers />
</cffunction>

0
 
LVL 27

Accepted Solution

by:
azadisaryev earned 500 total points
Comment Utility
ok, here's how i made it to work:

var onHideAddSaleBuyer = function(name){ //refreshes the cfselect
 var b = new salebuyerProxy();
 var p = new salebuyeryardProxy();
 ColdFusion.Bind.assignValue('sale_buyer_id', 'value', b.getSaleBuyers4cfselect());
 var sb = document.getElementById('sale_buyer_id');
 sb.options[sb.options.length-1].selected=true;
 ColdFusion.Bind.assignValue('sale_buyer_yard_id', 'value', p.getSaleBuyerYards4cfselect(sb.value));
};

btw, the 2 proxies you use in your code - are they to different CFCs? 'cos if they are for the same cfc then you only need 1 proxy...

+ since you use these proxies in several functions, you may want to init them beforehand and then call them up in the functions. like this:

<script type="text/javascript">
var b = new salebuyerProxy();
var p = new salebuyeryardProxy();

....

var onHideAddSaleBuyer = function(name){ //refreshes the cfselect
 ColdFusion.Bind.assignValue('sale_buyer_id', 'value', b.getSaleBuyers4cfselect());
 var sb = document.getElementById('sale_buyer_id');
 sb.options[sb.options.length-1].selected=true;
 ColdFusion.Bind.assignValue('sale_buyer_yard_id', 'value', p.getSaleBuyerYards4cfselect(sb.value));
};
...
</script>

see how the function does not init the proxies now? that's because js already knows about them since they have been setup outside of any particular function in the script - so now your functions can just use b.this() and p.that() !

Azadi
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:MFredin
Comment Utility
Azadi,

I copied my code below.  Since the proxies use the same CFC, I took out one of them and it still doesn't seem to be working.  Can you verify this is correct?  
<cfajaxproxy cfc="addWindow" jsclassname="salebuyerProxy">
 

<!--- Functions specific to adding a new SALE BUYER --->

var addSaleBuyer = function(){ //opens add_Salebuyer.cfm page in a cfwindow for adding new auction data

 openWindow('sale_buyer_id_window', 'Add Sale Buyer', '../Lots/add_Salebuyer.cfm', {center:true,height:650,width:400,closable:true,dragable:true,resizable:true,modal:true,initshow:true});

};
 

var b = new salebuyerProxy();
 

var onHideAddSaleBuyer = function(name){ //refreshes the cfselect

 ColdFusion.Bind.assignValue('sale_buyer_id', 'value', b.getSaleBuyers4cfselect());

 var sb = document.getElementById('sale_buyer_id');

 sb.options[sb.options.length-1].selected=true;

 ColdFusion.Bind.assignValue('sale_buyer_yard_id', 'value', b.getSaleBuyerYards4cfselect(sb.value));

};

Open in new window

0
 
LVL 27

Expert Comment

by:azadisaryev
Comment Utility
it looks correct, assuming you do have the js code inside <script></script> tags...

i would put the
var b = new salebuyerProxy();
line as the very first line after the opening <script> tag.

and, needless to say, ALL your js needs to be inside ONE block of <script></script>...

what exactly "still doesn't seem to be working"? can you elaborate? does the sale_buyer_id select gets refreshed and has the newly added value selected? any js errors? anything cf complains about?

+ which browser are you testing this in?

Azadi
0
 

Author Comment

by:MFredin
Comment Utility
Ah yes... I just tried in IE7 and its working great!

I was using Google Chrome.  Should have tried in IE.  Is there a quick fix for FF?  If not, its not a huge deal.  
0
 

Author Comment

by:MFredin
Comment Utility
Oh, and the other part to the question is how can I set a initial value for both selects when I am using the form to edit a record?
0
 
LVL 27

Expert Comment

by:azadisaryev
Comment Utility
the code worked in my FF3.0.8 with no problem...
yes, strangely it does not in Chrome... judging by cf ajax debugger, something is triggering another refresh of the second cfselect in chrome, but id does not say what... strange... same in Safari for windows... must be some WebKit js engine thing... will have to look into this...

but FF does work just fine.

as for your other question: yes, it is a bit tricky to pre-select a value in a cfselect with a binding to a cfc. but possible! just requires about 10 more lines of javascript code.
here are some excellent links on the topic:

http://www.coldfusionjedi.com/index.cfm/2007/8/7/Selecting-default-items-using-ColdFusion-8s-AJAX-Controls
http://cfsilence.com/blog/client/index.cfm/2007/8/7/Selecting-Multiple-Default-Items-With-ColdFusion-8-Ajax-Controls

Azadi
0
 

Author Comment

by:MFredin
Comment Utility
Thanks so much for all your help Azadi!! I really appreciate it.  Please don't worry about looking into the Chrome problem... at least not for me.  You've done much more than enough to help me.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now