Link to home
Start Free TrialLog in
Avatar of Westside2004
Westside2004Flag for United States of America

asked on

Coldfusion and JavaScript question

Hi,

I wanted to know.

Is it possible to have a query that for example returns 5 firstnames and 5 lastnames

Like:

<cfquery name="getNames" datasource="#ds#">
select firstname, lastname
from contacts
</cfquery>

The results would be:
Martin Short
Bob Beep
Joe Doe
Mary Lou
Rick Smith

---

I then want to pass these names to javascript in the form of an array or structure or something.  I have this drop down, where a user chooses a company name, based on that name i want to display a list of firstname/lastname's associated with that company, however I am trying to do this without trips to the server.  The query that gets the names is run once, and it loads all the companys and contacts associated with each company, so I have all these available.  I want the user to people to choose a new company in the drop down and then have some "onChange" function that would pull the contacts from each company and display them on the page without querying the db again.

How can I do this.. I hope i made sense.

-ws
Avatar of dcodez
dcodez

ws,

The following code is an example of how you can create 2 simple select lists and update them based on the options that are selected.

Based on the info you provided above, I assume you have a seperate db table that stores companies and that the contacts table has a companyID to link the contacts to a company.  If that's not the case, I'm sure you can easily adapt the following code to your db schema and make it work.



<cfquery name="getInfo" DataSource="#Request.DataSource#" username="#Request.DataSourceLogin#" password="#Request.DataSourcePass#">
      select *
      from contacts cons inner join testcompanies tc on cons.companyID = cons.companyID            
      order by cons.companyID      
</cfquery>
<script language="JavaScript"><!--
       ArrayOfCompanies = new Array();
      
       
 <cfoutput query="getInfo" group="companyID">
      tmpCompany = new Object();
      tmpCompany.cname =  '#companyname#';
       tmpCompany.empList = new Array();
      <cfoutput>
            tmpCompany.empList[tmpCompany.empList.length]='#name#';
      </cfoutput>
      ArrayOfCompanies[ArrayOfCompanies.length] = tmpCompany;
</cfoutput>  
function updateCompany(x){
       if(x >= 0){
            testform.empSelect.disabled = false;
            testform.empSelect.options.length = 0;
            empList = ArrayOfCompanies[x].empList;
            for(i=0;i<empList.length;i++){
                  testform.empSelect.options[i] = new Option(empList[i],empList[i]);  
            }
      }  
      else{
            testform.empSelect.disabled = true;      
            testform.empSelect.options.length = 0;
            testform.empSelect.options[i] = new Option('Select Company Above','Select Company Above');  
            
      }
}

--></script>

<form name="testform">
<select name="compSelect" onchange="updateCompany(this.selectedIndex -1)">
      <option>Select Company</option>
      <cfoutput query="getInfo" group="companyID">
            <option>#companyname#</option>
      </cfoutput>
</select>
<br>
<select name="empSelect" disabled>
      <option>Select Company Above</option>
</select>

</form>
ws,

I had a couple of typos in that last code example.  I've pasted an updated version below.   You can also see the code in action here: http://www.cnsusa.com/untitled.cfm





<cfquery name="getInfo" DataSource="#Request.DataSource#" username="#Request.DataSourceLogin#" password="#Request.DataSourcePass#">
      select *
      from contacts cons inner join testcompanies tc on tc.companyID = cons.companyID            
      order by cons.companyID      
</cfquery>
<script language="JavaScript"><!--
       ArrayOfCompanies = new Array();
      
       
 <cfoutput query="getInfo" group="companyID">
      tmpCompany = new Object();
      tmpCompany.cname =  '#companyname#';
       tmpCompany.empList = new Array();
      <cfoutput>
            tmpCompany.empList[tmpCompany.empList.length]='#name#';
      </cfoutput>
      ArrayOfCompanies[ArrayOfCompanies.length] = tmpCompany;
</cfoutput>  
function updateCompany(x){
       if(x >= 0){
            testform.empSelect.disabled = false;
            testform.empSelect.options.length = 0;
            empList = ArrayOfCompanies[x].empList;
            for(i=0;i<empList.length;i++){
                  testform.empSelect.options[i] = new Option(empList[i],empList[i]);  
            }
      }  
      else{
            testform.empSelect.disabled = true;      
            testform.empSelect.options.length = 0;
            testform.empSelect.options[0] = new Option('Select Company Above','Select Company Above');  
            
      }
}

--></script>

<form name="testform">
<select name="compSelect" onchange="updateCompany(this.selectedIndex -1)">
      <option>Select Company</option>
      <cfoutput query="getInfo" group="companyID">
            <option>#companyname#</option>
      </cfoutput>
</select>
<br>
<select name="empSelect" disabled>
      <option>Select Company Above</option>
</select>

</form>
Avatar of Westside2004

ASKER

Hi,

This does make sense, however I want to have the name displays as hyperlinks as opposed to a drop down.

The companies should be in a drop down as you have it, but the individual contacts within each company, should be listed as hyperlinks if possible.

Then when you click the hyperlink (which is the user's firstname and lastname) it will drill down and show the individual record details.  I can do the drilling down, etc, but I wanted to have the ability to choose a different company and have the firstname lastname come up without requerying the database each time.

Please let me know..

in the meantime I will try to modify your code (the 2nd drop down) and convert it to a list of hyperlinks...

-ws
ws,

yes this is possible, I'll post a code snippet soon modified with hyperlinks.
ASKER CERTIFIED SOLUTION
Avatar of dcodez
dcodez

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
oops, i left out.  but it should make sense.


if(x >= 0){
...}
else{
 document.all.emps.innerHTML = "";
}
Hey..

That worked.. thanks a bunch..

So to recap...

Basically you pull all the data you need once, and then build an array with it.  Than you manipulated the array?

thx

-Ws
Hi,

I did have one question, regarding the way the links are outputted.

How can I output the "contactID" for example.. in the query I pull a contactID

For my drill down pages I will need this contact ID

function updateCompany(x){
      if(x >= 0){
          empList = ArrayOfCompanies[x].empList;
          docstring = '';
          for(i=0;i<empList.length;i++){  
          docstring = docstring +     '<a href="">'+empList[i]+'</a><br>';
          }
           document.all.emps.innerHTML = docstring;
     }  
     
}

I see the <a href=""> and I tried wrapping that function in a <cfoutput> and then using queryname.contactID, but I ended up getting multiple names in my list.  Any idea on how I can add a contactID to the <a href> so when each firstname, lastname is display, when you roll over it, you will see the contactID as part of the URL..

Thanks

-ws