0

For several years, I have used dropdowns to determine the requirements for a SQL query and have never needed to use dropdowns with ‘related selects’ until now. I found a sample on how to do this and I can create related selects (with static data) which works well, see below using my data relating to vehicles and makes.

enter image description here

However, when I come to apply this to my environment, and use dynamic SQL query data, I cannot get it to work. I am confident that all the pieces of code are working together (as it works perfectly for the static data), but if I try and build the data automatically and then create the string to pass to the queryNew function, it fails.

This is the working Content.cfc file with STATIC query strings/data array

<cfcomponent>

    <cfset tblMake = queryNew("name,id", "varchar,varchar", [{name:'RENAULT',id:'RENAULT'},{name:'RENAULT',id:'RENAULT'},{name:'RENAULT',id:'RENAULT'}])>
    <cfset tblModel = queryNew("name,code,continent_id", "varchar,varchar,varchar", [{name:"TSERIES",code:"TSERIES",continent_id:"RENAULT"},{name:"MASTER",code:"MASTER",continent_id:"RENAULT"}])>
    <cfset tblVoltage = queryNew("name,code", "varchar,varchar", [{name:"24 volt",code:"MASTER"},{name:"12 volt",code:"TSERIES"}])>

   <cffunction name="getContent" access="remote" returntype="query" output="true">
      <cfargument name="strTableName" type="string" required="true">
      <cfargument name="strID" type="string" required="true">
      <cfargument name="strName" type="string" required="true">
      <cfargument name="intDistinct" type="numeric" required="false" default="0">
      <cfargument name="selectedCol" type="string" required="false" default="0">
      <cfargument name="selectedID" type="string" required="false" default="0">
      <cfquery name="qryContent" dbtype="query">
         select
         <cfif arguments.intDistinct eq 1>distinct</cfif>
         #arguments.strID# as theID,
         #arguments.strName# as theValue
         from #arguments.strTableName#
         <cfif arguments.selectedID neq 0>
            where #arguments.selectedCol# = '#arguments.selectedID#'
         </cfif>
         order by #arguments.strName#
      </cfquery>
      <cfreturn qryContent />
   </cffunction>
</cfcomponent>

When I try and create the query using this method, nothing appears to work. I am looping through the database (from a query) and then creating a string using the required structure and the passing it to the function.

<cfcomponent>

    <cfset XSTATIC = ""/>
    <cfquery name="NOXVehicleModels" datasource="EBSNOX" >
        SELECT DISTINCT VehicleMake  
        FROM [dbo].[NOX-Master]
        WHERE VehicleMake IS NOT NULL
    </cfquery>
    <cfloop query="NOXVehicleModels">
      <cfset XSTATIC = XSTATIC & "{name:'" & #Trim(NOXVehicleModels.VehicleMake)# & "',id:'"& #Trim(NOXVehicleModels.VehicleMake)# & "'},"/>
    </cfloop>
    <cfset XLEN=LEN(#XSTATIC#)/>
    <cfset XSTATIC = MID(XSTATIC,1,XLEN-1)/>
    <cfoutput>#XSTATIC#</cfoutput> 

    <cfset tblMake = queryNew("name,id", "varchar,varchar", [#XSTATIC#])>

I have created a separate cfoutput to test the string for structure etc and it appears to be correct, but it is just not passing to the querynew function. This is what the output looks like:-

{name:'CUMMINS',id:'CUMMINS'},{name:'DAF',id:'DAF'},{name:'IVECO',id:'IVECO'},{name:'MAN',id:'MAN'},{name:'MERCEDES',id:'MERCEDES'},{name:'RENAULT',id:'RENAULT'},{name:'SCANIA',id:'SCANIA'},{name:'VOLVO',id:'VOLVO'} {name:'RENAULT',id:'RENAULT'},{name:'RENAULT',id:'RENAULT'},{name:'RENAULT',id:'RENAULT'}

What I have done so far:-

  1. I have checked that the quotes are not important i.e. single/double.
  2. I have tried to build the query outside of the tag.

Again, all appears (?) to be in order and I cannot understand that could be the problem. Possibly the structure is missing something before parsed to the function?

Any help would be greatly appreciated.

Thanks, Jack

enter image description here

New Dropdown v2

Mr Jackson
  • 57
  • 5
  • 1
    It seems that QueryNew doesn't work in this way. Looking at your code, it's not really clear to me the necessity of taking the step of using QueryNew to load your data into another query with seemingly the same data, but I could be missing something. Assuming it is necessary, I recommend populating this new query with a QueryNew that only defines the fields, and then looping through your original query and using QueryAddRow, QuerySetCell, etc. to load the data dynamically. – The Megatron Man Jan 14 '22 at 05:54
  • Thank you, Megatron Man, I think you are 100% correct - the QueryNew cannot work in this manner, that would have been too simple for me :-(. Will look at your idea as it will be cleaner ultimately - many thanks for your feedback. – Mr Jackson Jan 14 '22 at 07:50
  • 1
    (Edit) The reason the code doesn't work is because it's building a *string*, but `QueryNew` excepts an array of arrays/structures. While you could fix the code by using arrays, it doesn't seem necessary as a (database) cfquery already produces a query object. So why not just use that? – SOS Jan 17 '22 at 00:57
  • Hello SOS, thanks for the note. You are spot on with regards to cfquery already produces a query object, but my concern related to the speed when populating the dropsdowns especially when on VPN. I was hoping to just build it all into an array once only as the table is static for now. In saying that, I have no problem with the speed itself, it just populates the cfselects slowly and on-screen. I was also hoping to try and supress the screen updating (on cfselect change for example), but cannot locate anything in the documentation or online. – Mr Jackson Jan 18 '22 at 15:01
  • Hello again, SOS - I have attached another image in real-time - related selects pause briefly on VPN when secondary queries run to populate other dropdowns. Simle queries i.e. Select Distinct Vehicle etc. Anyway, will battle on and try some more, but thank you kindly. – Mr Jackson Jan 18 '22 at 15:10
  • (Edit) Sorry, missed the image at the bottom. I don't see any big delay. Just the normal milliseconds it takes to repopulate lists based on the selection. Am I missing something? – SOS Jan 18 '22 at 22:02
  • `I was also hoping to try and supress the screen updating (on cfselect change for example)` That's exactly what a "related" select is supposed to do (: i.e. It's contents should change dynamically based on what you select in *another* list. So ... are you sure you actually want "related" selects? It doesn't sound like it.. – SOS Jan 19 '22 at 23:18
  • Any updates on this? – SOS Feb 27 '22 at 04:03
  • Hello SOS, very sorry for not responding to you. I changed the way I do things on this and have a situation where I have a primary dropdown (vehicle make) and then subsequent dropdowns are built based on the previous dropdown values, left to right based on make then the mode etc. I have attached a new image above and thanks for your feedback. – Mr Jackson Mar 01 '22 at 07:29
  • No worries. So it is working as expected now? – SOS Mar 01 '22 at 15:53
  • Yes, all working as expected - many thanks! – Mr Jackson Mar 09 '22 at 09:38

0 Answers0