Link to home
Start Free TrialLog in
Avatar of MFredin
MFredinFlag for United States of America

asked on

Requery chained select box after parent insert.

This question is a continuation of this question... https://www.experts-exchange.com/questions/24278868/Get-the-current-value-of-a-select-box.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

Avatar of azadisaryev
azadisaryev
Flag of Hong Kong image

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
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
Avatar of MFredin

ASKER

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.  
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
Avatar of MFredin

ASKER

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>

ASKER CERTIFIED SOLUTION
Avatar of azadisaryev
azadisaryev
Flag of Hong Kong image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MFredin

ASKER

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

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
Avatar of MFredin

ASKER

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.  
Avatar of MFredin

ASKER

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?
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
Avatar of MFredin

ASKER

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.