We help IT Professionals succeed at work.

please help with question in javascript area

DBrown
DBrown asked
on
Could someone try to help me with this question? I need to populate a javascript array with a query.
I am using CF also. I cannot use the cfscript tag for specific reasons.


http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=javascript&qid=20183445


Thanks

Doug
Comment
Watch Question

There are wddx components that will do this for you.

A not so elegant way to do it

<script language="javascript">
   var arrMyArray = new Array();
   var intIndex   = 0
   <cfoutput query="queryName">
      arrMyArray[i] = new Array('#field1#', '#field2#'...)
      i++
   </cfoutput>
</script>

Commented:
pkinetics solution works. It just looks better to do it this way:

<CFOUTPUT><SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
     var myArray = new Array(#ValueList(MyQuery.Some_Integer_ID)#);
</SCRIPT></CFOUTPUT>

Is it an array of integers or strings?

Author

Commented:
It needs to be a multidimensional array like below. String values

var regionalOffices = new Array()
// Stuff main array entries with objects.
regionalOffices[0] = new officeRecord("New York", "Shirley Smith", 300000)
regionalOffices[1] = new officeRecord("Chicago", "Todd Gaston", 250000)
regionalOffices[2] = new officeRecord("Houston", "Leslie Jones", 350000)
regionalOffices[3] = new officeRecord("Portland", "Harold Zoot", 225000)
I know my suggestion works. I have done it numerous times without a glitch. Matter of fact I cut it right out of a production piece of code.

Author

Commented:
I appreciate it, but I would rellay like to have a few more suggestions. I honestly do not like the way wddx outputs it's array.



Doug
What specifically are you looking for?

My other suggestion will work as you requested in your other post.

<script language="javascript">
  var arrMyArray = new Array();
  var intIndex   = 0
  <cfoutput query="queryName">
     arrMyArray[i] = new Array('#field1#', '#field2#', '#field3#')
     //alert(arrMyArray[i]);
     i++
  </cfoutput>
</script>

If you want, you can also throw an alert statement in there so you can see the output as it is being added to the array.

Author

Commented:
I tried your code. I get a error: missing ). Also this is the output I get. I do not get a index for the array. I copied your exact code and simply replaced the queryName and the coldfusion variable names

script language="javascript">
 var arrMyArray = new Array();
 var intIndex   = 0
 
    arrMyArray[i] = new Array('cpu', 'test-cpu1', 'This is the best out there big bad brudda')
    i++
<script language="javascript">
 var arrMyArray = new Array();
 var intIndex   = 0;
 <cfoutput query="queryName">
    arrMyArray[intIndex] = new Array('#field1#', '#field2#', '#field3#');
    intIndex++;
 </cfoutput>
</script>

try that... I copied too much right from the start. You can limit the output on the CFOUTPUT tag to just five records and that will give you a small dataset to work with, rather than looking at the whole thing.

Also makes it easier to track down javascript error messages.

Author

Commented:
Strange. I still get the same result

Here is my code you gave.

<script language="javascript">
var arrMyArray = new Array();
var intIndex = 0;
<cfoutput query="getCompParts">
   arrMyArray[intIndex] = new Array('#description#', '#partNum#', '#salePrice#');
   intIndex++;
</cfoutput>
</script>

and the result

 arrMyArray[intIndex] = new Array('This is the best out there big bad brudda', 'test-cpu1', '2000.0000');
   intIndex++;


doug
I'm assuming your dataset contains more than 1 record. Otherwise it works fine.

Are you still getting a javascript error message? How many records are there in the dataset?

Does the array data get disjointed? ie text is scattered throughout the page? Usually that is caused by carrage returns in the description field. You might need to run a #replace(description, chr(13), '')# instead of #description#.

Commented:
DBrown,

How about calculating the price as the user changes the configuration? The solution I have in mind is         simpler. You pass the values to Javascript variables using CF (while generating the page) and use Javascript to do the calculation. Let me know if this is a solution you would consider and I'll post a sample code.

Author

Commented:
Yes I would consider that.



DB
Commented:
I think what you want is to do is a client side calculation based on the user's selection. Correct me if I'm wrong, OK?

You'll have a form in where you'll populate it with selections and options for each selection..

<form name="frmName" method="post" action="processFile.cfm">

<select name="processor" onChange="changePrice()">
  <option value="0"></option>
<cfoutput query="getProcessorParts">
  <option value="#typeID#">#model#
</cfoutput>
</select>
<br>
<select name="harddisk" onChange="changePrice()">
  <option value="0"></option>
<cfoutput query="getHarddiskParts">
  <option value="#typeID#">#model#
</cfoutput>
</select>
<br>


<input type="textbox" name="priceBox" value="">
</form>


<script language="Javascript">
function changePrice() {
 var totalPrice = 0;

// calculate price for processor
 var itemPrice = 0;
 if (document.frmName.processor.selectedIndex!=0) {
    <cfloop query="getProcessorParts">
     if (document.frmName.processor[document.frmName.processor.selectedIndex].value == '#typeID#')
        itemPrice = #salePrice#;
    </cfloop>
 }
 totalPrice =+ itemPrice

// calculate price for harddisk
 var itemPrice = 0;
 if (document.frmName.harddisk.selectedIndex!=0) {
    <cfloop query="getHarddiskParts">
     if (document.frmName.harddisk[document.frmName.harddisk.selectedIndex].value == '#typeID#')
        itemPrice = #salePrice#;
    </cfloop>
 }
 totalPrice =+ itemPrice
 
 document.frmName.priceBox.value = totalPrice;
}

</script>


The javascript calculates the corresponding price for the selected computer part. This is just two selections, you can extend it to make calculation based on as many components as you want.




Author

Commented:
Sorry so late.............

Explore More ContentExplore courses, solutions, and other research materials related to this topic.