Go Back  HTML Forums - Free Webmaster Forums and Help Forums > WEBSITE DEVELOPMENT > Client Side Scripting
User Name:
Password:
 

Reply
Thread Tools   Display Modes
  View First Unread
 
Old 04-09-2010, 10:57 AM
  #1
msg2han
Novice (Level 1)
 
Join Date: Aug 2007
Posts: 2
iTrader: (0)
msg2han is an unknown quantity at this point
How to assign java arraylist or array to JAVASCRIPT Array

Hi gurus,

I need to convert java arraylist to javascript array. When i tried..

i have an arraylist called famousTodayList in Java which contains value as --> string1, string2, string3 . It is displaying whole java Arraylist directly.

eg : [string1, string2, string3]


i have used syntax as

var myArray2= new Array('<s:property value="%{arr}"/>');


any solutions ?

The main intention is to get the values as myArray2[0], myArray2[1], myArray2[2]


Thanks in advance.
kite.
msg2han is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 04-11-2010, 02:02 PM
  #2
DamianWarS
SuperHero (Level 14)
 
Join Date: Jan 2007
Posts: 279
iTrader: (0)
DamianWarS is an unknown quantity at this point
well this is how you make an array in javascript

Code:
var  myArray = new Array("string1","string2","string3");
this will make an array with 3 indices (0,1,2). Notice how each string I have is surrounded by quotations and is followed by a comma. the quotations mark the boundaries of the string you are putting into the array and the comma marks that it is adding another string. Both quotations and comma are needed in order to build an array like this.

from you sample code
var myArray2= new Array('<s:property value="%{arr}"/>');

it doesn't seem like this is happening. I don't know what "<s:property value="%{arr}"/>" is but at best this will only produce an array with 1 index and that doesn't seem to be the result you want.

if you have an existing array in some server side language and you want to build a client-side array in javascript you will need to use a loop to do so. the loop will loop through the elements of the array on the server side to populate the code for the client side. To build an array like the above code you will have to follow something like this:

Code:
var myArray = new Array("
	<start server side code>
		start loop
			output array value here surrounded by quotations
			if its not the last index of the array add a comma
		end loop
	</end server side code>
");
something like that is going to get the results of something like this...
Code:
var myArray = new Array("value1","value2","value3","value4","value5");
...and it will build your array.

there are other methods to make javascript arrays too. for example you can declare the array then add values at the index you want.

Code:
var myArray = new Array();
myArray[0] = "value1";
myArray[1] = "value2";
myArray[2] = "value3";
myArray[3] = "value4";
myArray[4] = "value5";
or you can use the array push method to throw values and it will dynamically grow the array like this:

Code:
var myArray = new Array();
myArray.push("value1");
myArray.push("value2");
myArray.push("value3");
myArray.push("value4");
myArray.push("value5");
im sure there are other creative ways as well to make an array and you can use which ever method you find easiest to work with when looping through a server side language to make client-side syntax.

javascript will only understand javascript code so you need to output what it understands to make what you want work.
DamianWarS is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Reply
KEEP TABS
SPONSORS
 
Boxedart

 
 


 
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
  
 
 
 



 
  POSTING RULES
 
 
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Thread Tools
Display Modes

Forum Jump

 

All times are GMT -5. The time now is 06:33 AM.

   

Mascot team created by Drawshop.com | Web Hosting

Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

Server Monitoring by ENIACmonitor 0.01
HTMLforums.com © Big Resources, Inc. Web Design by BoxedArt.com
vRewrite 1.5 beta SEOed URLs completed by Tech Help Forum and Chalo Na.