Solved

Requery chained select box after parent insert.

Posted on 2009-04-01
12
844 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
ID: 24039526
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
ID: 24039577
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
ID: 24039722
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 27

Expert Comment

by:azadisaryev
ID: 24039951
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
ID: 24040272
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
ID: 24040296
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
 

Author Comment

by:MFredin
ID: 24040536
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
ID: 24040713
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
ID: 24040875
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
ID: 24040909
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
ID: 24041335
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
ID: 24041395
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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…
CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 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