Link to home
Start Free TrialLog in
Avatar of Panos
PanosFlag for Germany

asked on

ColdFusion Ajax Related Selects

Hello experts
I'm trying to use the tutorial :ColdFusion Ajax Tutorial 2: Related Selects from :http://www.forta.com/blog/index.cfm/2007/5/31/ColdFusion-Ajax-Tutorial-2-Related-Selects.
The problem i have is to set a value for the child select box when the parent id is "".(the default value for -select all-.i did put this in the db table because i could not use it in the cfselect.If that is possible please help me with this too).
On  page load must the parent display be -all-  and the child -all- too.
Here is my code.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-7" />
<title>Untitled Document</title>
</head>
 
<body>
<cfform name="Localiza">
<table>
<tr>
<td width="100">Division:</td>
<td width="150">
<cfselect name="Hersteller_ID" 
          bind="cfc:test.com.getnewsearchmodell.GetHersteller()"
		  display="Hersteller_Text" 
          value="Hersteller_ID" 
          BindOnLoad="true"
          />
        </td>
   </tr>
<tr>
<td width="100">State:</td>
<td width="150">
<cfselect name="Modell_ID" 
          bind="cfc:test.com.getnewsearchmodell.GetMenu({Hersteller_ID})"
		  display="Modell_DE" 
          value="Modell_ID"/>
          </td>
       </tr>
 
</table>
</cfform>
</body>
</html>
 
cfc:
<cfcomponent output="false">
 <cffunction name="GetHersteller" access="remote" returnType="array">
  <cfargument name="vlangid" required="no" type="string" default="de">
      <cfset var data="">
      <cfset var result=ArrayNew(2)>
      <cfset var i=0>
     <cfquery name="data" datasource="#request.dsn#">
     SELECT Hersteller_ID, Hersteller_Text
     FROM Hersteller
     WHERE 
     Mobiles_Subs_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="1">
     OR (Hersteller_ID = <cfqueryparam cfsqltype="cf_sql_integer" value="19999">
     AND LANGID = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.vlangid#"> )
     OR	(Hersteller_NR = <cfqueryparam cfsqltype="cf_sql_integer" value="-1">
     AND LANGID = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.vlangid#"> )
     ORDER BY Hersteller_NR
     </cfquery>
      <cfloop index="i" from="1" to="#data.RecordCount#">
         <cfset result[i][1]=data.Hersteller_ID[i]>
         <cfset result[i][2]=data.Hersteller_Text[i]>
      </cfloop>
  <cfreturn result>
 </cffunction>
 
 <cffunction name="GetMenu" access="remote" returnType="array">
   <cfargument name="Hersteller_ID" type="any" required="true">
      <cfset var data="">
      <cfset var result=ArrayNew(2)>
      <cfset var i=0>
      
      <cfif arguments.hersteller_ID NEQ "">
       <cfquery name="data" datasource="#request.dsn#">
        SELECT Auto_ID,Modell_DE, Modell_ID
        FROM Modell_Auto_S
        WHERE Auto_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#ARGUMENTS.Hersteller_id#">
        ORDER BY Modell_NR
        </cfquery>
        
         <cfloop index="i" from="1" to="#data.RecordCount#">
         <cfset result[i][1]=data.Modell_ID[i]>
         <cfset result[i][2]=data.Modell_DE[i]>
        </cfloop>
    </cfif> 
     <cfreturn result>
    </cffunction>
</cfcomponent>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of erikTsomik
erikTsomik
Flag of United States of America 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
SOLUTION
Avatar of SRIKANTH MADISHETTI
SRIKANTH MADISHETTI
Flag of India 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 Panos

ASKER

Hi erikTsomik.
I did change the value to 0 and put anothet row in the child table - for Auto_id 0 ,a value All for Modell_DE and "" for Modell_ID and it seems that it is working.
The new query is the following .Can it be more simplier?
<cfcomponent output="false">
 <cffunction name="GetHersteller" access="remote" returnType="array">
  <cfargument name="vlangid" required="no" type="string" default="de">
      <cfset var data="">
      <cfset var result=ArrayNew(2)>
      <cfset var i=0>
     <cfquery name="data" datasource="#request.dsn#">
     SELECT Hersteller_ID, Hersteller_Text
     FROM Hersteller
     WHERE
     Mobiles_Subs_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="1">
     OR (Hersteller_ID = <cfqueryparam cfsqltype="cf_sql_integer" value="19999">
     AND LANGID = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.vlangid#"> )
     OR      (Hersteller_ID = <cfqueryparam cfsqltype="cf_sql_integer" value="0">
     AND LANGID = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.vlangid#"> )
     ORDER BY Hersteller_NR
     </cfquery>
      <cfloop index="i" from="1" to="#data.RecordCount#">
         <cfset result[i][1]=data.Hersteller_ID[i]>
         <cfset result[i][2]=data.Hersteller_Text[i]>
      </cfloop>
  <cfreturn result>
 </cffunction>

 <cffunction name="GetMenu" access="remote" returnType="array">
   <cfargument name="Hersteller_ID" type="any" required="true">
      <cfset var data="">
      <cfset var result=ArrayNew(2)>
      <cfset var i=0>
     
     
       <cfquery name="data" datasource="#request.dsn#">
        SELECT Auto_ID,Modell_DE, Modell_ID
        FROM Modell_Auto_S
        WHERE Auto_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#ARGUMENTS.Hersteller_id#">
        ORDER BY Modell_NR
        </cfquery>
       
         <cfloop index="i" from="1" to="#data.RecordCount#">
         <cfset result[i][1]=data.Modell_ID[i]>
         <cfset result[i][2]=data.Modell_DE[i]>
        </cfloop>

     <cfreturn result>
    </cffunction>
</cfcomponent>
wy are you touching the DB inserting rows with 0.

If you have some reports and other stuff .

u need to manage them in code it self
Avatar of Panos

ASKER

Hi srikanthmadishetti:
I did just see your post.I'm trying to test it but it does crash .
I did change the db because i thought that erikTsomik did mean to do this.

Avatar of Panos

ASKER

This does not crash:
 <cffunction name="GetMenu" access="remote" returnType="array">
   <cfargument name="Hersteller_ID" type="any" required="true">
      <cfset var data="">
      <cfset var result=ArrayNew(2)>
      <cfset var i=0>
     
     <cfif arguments.hersteller_ID NEQ "">
       <cfquery name="data" datasource="#request.dsn#">
        SELECT Auto_ID,Modell_DE, Modell_ID
        FROM Modell_Auto_S
       
        WHERE Auto_ID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#ARGUMENTS.Hersteller_id#">
       
        ORDER BY Modell_NR
        </cfquery>
       
         <cfloop index="i" from="1" to="#data.RecordCount#">
         <cfset result[i][1]=data.Modell_ID[i]>
         <cfset result[i][2]=data.Modell_DE[i]>
        </cfloop>
       <cfelse>
        <cfset result[1][1]="">
        <cfset result[1][2]="Please select Menu">
     </cfif>
     <cfreturn result>
    </cffunction>
you can say in the query that <cfif name neq 0>
then do sigle item
</cfif>

btw, you do not need to convert your query to an array for a cfselect - it works just fine with a query.

Azadi
Avatar of Panos

ASKER

Can you  please give me more details azadi?
Avatar of Panos

ASKER

azadi i'm trying to use another way for related selects instead of the jsmx library i did use because i do need another way for simultaneous ajax requests.(i had the problem here:https://www.experts-exchange.com/questions/24478424/JSMX-and-number-of-simultaneous-ajax-requests.html)
Avatar of Panos

ASKER

Thank you all for your help
regards
panos