Solved

Coldfusion and JavaScript question

Posted on 2004-11-30
8
162 Views
Last Modified: 2013-12-24
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
0
Comment
Question by:Westside2004
  • 5
  • 3
8 Comments
 
LVL 2

Expert Comment

by:dcodez
Comment Utility
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>
0
 
LVL 2

Expert Comment

by:dcodez
Comment Utility
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>
0
 
LVL 1

Author Comment

by:Westside2004
Comment Utility
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
0
 
LVL 2

Expert Comment

by:dcodez
Comment Utility
ws,

yes this is possible, I'll post a code snippet soon modified with hyperlinks.
0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 2

Accepted Solution

by:
dcodez earned 250 total points
Comment Utility
Here I use a div to fill with hyperlinks that show the name.  The links don't go anywhere since you mentioned that you would handle the drill down, etc.  This should give you what you need to work with.

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){
            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;
      }  
      
}

--></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>


<div ID="emps">

</div>

 
</form>
0
 
LVL 2

Expert Comment

by:dcodez
Comment Utility
oops, i left out.  but it should make sense.


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

Author Comment

by:Westside2004
Comment Utility
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
0
 
LVL 1

Author Comment

by:Westside2004
Comment Utility
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
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
The purpose of this video is to demonstrate how to make a WordPress Site faster and smaller in size by cleaning up the database. This will be demonstrated using a Windows 8 PC. Plugin WP Optimize will be used. Go to your WordPress login page. T…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now