• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 857
  • Last Modified:

Requery chained select box after parent insert.

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
MFredin
Asked:
MFredin
  • 6
  • 6
1 Solution
 
azadisaryevCommented:
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
 
azadisaryevCommented:
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
 
MFredinAuthor Commented:
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
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.

 
azadisaryevCommented:
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
 
MFredinAuthor Commented:
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
 
azadisaryevCommented:
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
 
MFredinAuthor Commented:
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
 
azadisaryevCommented:
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
 
MFredinAuthor Commented:
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
 
MFredinAuthor Commented:
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
 
azadisaryevCommented:
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
 
MFredinAuthor Commented:
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now