Click to See Complete Forum and Search --> : JS arrays


mili
12-19-2003, 01:52 PM
Hi
I defined a new array called "arrStgy" which gets all strategy names from xml doc. This is a one dimensional array with strategy name.
But I want to populate my select box with a two dimensional array with both values being same for option text and option value.
My code does not work now because I have a one dimensional array.
How can i do this?
arrStgy = new Array();
var i = 0;

<xsl:for-each select="//Class/Master/Strategies">
arrStgy[i] = '<xsl:value-of select="name"/>'
i = i + 1;
</xsl:for-each>


function getStgy()
{
ccySelected = document.forms[0].Id_ccy[document.forms[0].Id_ccy.selectedIndex].text;
n = new Array();

strategy = new Array();
var count = 0;
for(var j=0; j &lt; arrStgy.length; j++)
{
var strStgy = arrStgy[j];

var strCurrency = strStgy.substring(0,strStgy.indexOf("#"));

var strStgyCcy = strStgy.substring(strStgy.indexOf("#")+1);

if(ccySelected == strCurrency)
{
strategy[count] =strStgyCcy;
count++;
ccyStgy = (strStgy);
fill( form.selectbox, ccyStgy );
}
}
}

function fill( sel, ar )
{
var ix;
for ( ix = sel.options.length-1; ix >= 0; --ix )
sel.options[ix] = null;
for ( ix = 0; ix &lt; ar.length; ++ix )
sel.options[ix] = new Option( ar[ix][0], ar[ix][1] );
}

Khalid Ali
12-19-2003, 07:10 PM
the first thing u must verify that if array is being created by XSL

mili
12-19-2003, 07:47 PM
Khalid,

Yes, at first I loop thro the xml and store all the values in a array.The values in array I have are the format
"USD#blahblah", "EUR#blahblah", "TKY#blahblah"


I have a static drop down with option values USD,TKY etc.This drop down is named Id_ccy.I want to populate another dropdown box called selectbox, based on the value selected from Id_ccy.

Now using JS i'm looping thro the array,trying to split the data before and after #.This gives me a list of all values befor and after #.

If my Id_ccy value selected = the value before #, I want to populate the corresponding data in selectbox.

I hope I'm clear.

Thanks

mili
12-19-2003, 07:50 PM
If both my option value and option text are same, I guess I could say
sel.options[ix] = new Option( ar[ix], ar[ix]); in fill();
But this doesnt seem to work as well.

Khalid Ali
12-19-2003, 07:52 PM
May be This link will shed some light (http://www.webapplikations.com/pages/html_js/forms/DropDownSelectCountryShowCities.html) on how to populate the second list box

mili
12-19-2003, 08:07 PM
Khalid, that for the link.I went thro' the code.
But I dont understand something.....
I'm trying to parse my array value to separate values before & after the delimeter #
My guess was that, in the function, fill, I try to treat that second argument as an array. it doesn't work, because it isn't an array, at all.
So I changed my fill() to
sel.options[ix] = new Option(ar,ar);
This time, it loads the selectbox, but only the last value from my array list.
I dont know where I'm going wrong.

Khalid Ali
12-19-2003, 08:27 PM
I hope this will help

<script type="text/javascript">
<!--
function Process(){
var arrStrgy = new Array("USD#blahblah", "EUR#blahblah", "TKY#blahblah");//assuming you have correctly created array
//now you want to split the array by # mark
//we will do that and create 2 arrays one with the value b4 the # and one with value after
var firstValArr = new Array();
var secValArr = new Array();
for(var x=0;x<arrStrgy.length;x++){
var temp = arrStrgy[x].split("#");
firstValArr[x] = temp[0]; //value before #
secValArr[x] = temp[1]; //value after #
}

alert("BEFORE ["+firstValArr+"]\nAFTER ["+secValArr+"]")
}
//-->
</script>
</head>
<body>
<form id="form1" action="" onsubmit="">
<input type="button" value="process" onclick="Process()"/>
</form>

mili
12-19-2003, 08:57 PM
Thanks,khalid. That worked. I could see the values in my alert.I didnt know that i could use "split".
How can I check if ccyselected == all the values firstValArr[x]
and load only the related secValArr[x] in my selectbox.
In your example in the link you sent me, you have if selection==locArr[x][0]. How does it work for me?

mili
12-19-2003, 09:43 PM
I'm not able to get to the last step. This time, I'm getting the right value into the selectbox based on the selected id_ccy value, but it is loading only one value.where am i going wrong?

function getStgy()
{
ccySelected = document.forms[0].Id_ccy[document.forms[0].Id_ccy.selectedIndex].text;
var ccy = new Array();
var stgy = new Array();
strategy = new Array();
var count = 0;

for(var x=0;x&lt;arrStgy.length;x++)
{
var temp = arrStgy[x].split("#");
ccy[x] = temp[0]; //value before #
stgy[x] = temp[1]; //value after #

if(ccySelected == ccy[x])
{
//alert("matching done");
var stgyccy = new Array();
stgyccy = stgy[x];
}
}

//alert("BEFORE ["+ccy+"]\nAFTER ["+stgy+"]")
fillStgy( form.selectbox, stgyccy);
}

Khalid Ali
12-19-2003, 10:11 PM
my guess is that u are creating new array each time and pass it to the fill function,if I ma correct you should be getting the last array value filled in the list box

mili
12-19-2003, 10:18 PM
If I get the last array value filled in the list box, them I'm not filtering my list.I'm populating the entire array.

mili
12-20-2003, 09:33 AM
Khalid, I'm still stuck with this problem.
Should I build an array again, within my if condition?

Thanks

mili
12-20-2003, 02:10 PM
Many thanks for your help, khalid.
I resolved the issue
for(var x=0;x&lt;arrStgy.length;x++)
{
var temp = arrStgy[x].split("#");
ccy[x] = temp[0];
if(ccySelected == ccy[x])
{
var temp = arrStgy[x].split("#");
stgy[x] = temp[1];
stgy.sort();
}
}