Solved

Requery chained select box after parent insert.

Posted on 2009-04-01
12
845 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

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…
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

791 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