2

Experts, I have a situation here. I have ArrayList of String[] (ArrayList of String arrays in Struts2 action class, I am iterating those values into javascript to pass those values as a two dimensional array to google graph by using

var twodarray = ["<s:iterator value='res' status='status'>[<s:property/>]<s:if test='#status.last==false'>,</s:if></s:iterator>"];

then passing that twodarray into google graph using

var data = google.visualization.arrayToDataTable(twodarray);

How google graph accepts the parameter is for example

var data = google.visualization.arrayToDataTable([
     ['Month', 'Electronic', 'Electric'],
     ['2017/03',  400,      290],
     ['2017/04',  450,      275]
  ]);

When I use

var data = google.visualization.arrayToDataTable(twodarray);

Graph is not showing up, but when I display the value of twodarray or alert the twodarray, it gives me the exact data as two dimensional array like below.

var twodarray=[['Month', 'Electronic', 'Electric'],['2017/03',  400,      290],['2017/04',  450,      275]];

I am wondering how the variable twodarray interprets value to pass as an 2D array to javascript function. Is it passing as a string? I am trying to pass it as a 2D array.

6
  • twodarray = ["<s:iterator <-- might it be that you have the " coded into your variable, so you end up with an array of Strings instead of a 2D array in JavaScript? How did you check variable contents? Commented May 25, 2017 at 17:44
  • By javascript alert. Commented May 25, 2017 at 17:55
  • Good catch, Jan! I removed it, it works. I think I have to to get some coffee and come back. Commented May 25, 2017 at 17:59
  • You okay if I add that as answer? Commented May 25, 2017 at 17:59
  • Sure thing... that's the answer... Commented May 25, 2017 at 18:02

2 Answers 2

1

Your variable declaration

var twodarray = ["<s:iterator value='res' status='status'>[<s:property/>]<s:if test='#status.last==false'>,</s:if></s:iterator>"];

contains " that are not required by the iterator - so they probably end up in your javascript-variable making it an array of strings - or an array with one string more exactly.

Removing the " should to the trick:

var twodarray = [<s:iterator value='res' status='status'>[<s:property/>]<s:if test='#status.last==false'>,</s:if></s:iterator>];
Sign up to request clarification or add additional context in comments.

Comments

1

OK with Jan's solution. A suggestion, I would create the JS array in this way:

var twodarray = [];
<s:iterator value='res' status='status'>
    twodarray.push([<s:property/>]);
</s:iterator>

It makes it more readable in your template and in the HTML (no burdening coma conditions and nested brackets)

var twodarray = [];
twodarray.push(['Month', 'Electronic', 'Electric']);
twodarray.push(['2017/03',  400,      290]);
twodarray.push(['2017/04',  450,      275]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.