3

How can I add my java array contents into empty javascript array? I tried but its not working.

Here is my code in file.jsp.

<%@page import="javax.portlet.PortletPreferences" %>

   PortletPreferences prefs = renderRequest.getPreferences(); // here i am reading values from preferences in Liferay and rendering values

 <select id="dropdown1" onchange="changeAction(this.value);">
                        <option value="" selected="selected" >Select</option>
                        <option value="">value1</option>
                        <option value="">value2</option>
  </select>

      <label>Device</label>
      <%

       String device1 = prefs.getValue("device1","");
       String[] array1 = device1.split("\n"); // String[] array=["books","pens","cars"];
     %>

      <select id="dropdown2">
                <option value="select" selected="selected">Select</option>
      </select>

below is my Java script code, both jsp code and javascripts are inside same jsp file.

<script>
     function changeAction(optionVal){
     var tempArrayLabel = [];

     jQuery("#dropdown2").empty().append('<option value="-1" >Select</option>');

    if(optionVal == "selected val in first drop down") {

tempArrayLabel = "add values from array1 or device1"; // update the drop down list


}

for (var i=0;i<tempArrayLabel.length;i++){
    jQuery('<option/>').html(tempArrayLabel[i]).appendTo('#dropdown2');
}

}

 </script>

Thanks for any kind of suggestion.

user3534759
  • 221
  • 2
  • 12
  • String[] array=["books","pens","cars"]; is not a document element. document elements are html tags not java codes. You can write a div which contains the values comma seperated and name the div arrayDiv and get values from it. – sgpalit Mar 12 '15 at 12:12
  • @sgpalit.. In my case I need to change java script code only but can not change String[] array=["books","pens","cars"]; since this array is getting values dynamically but for reference purpose I mentioned like this. suggest me javascript such that i add values into "tempArrayValue" – user3534759 Mar 12 '15 at 12:20
  • ok, so why do you need to clear the select options and then add them again via javascript, since the options remain the same? My (current) answer will not work since the code is in two files and not one. If you need to just clear the selected option there is no need to remove the optins and re-add them in the select box – Nikos M. Mar 12 '15 at 13:18
  • @ Nikos M , both jsp code and java scripts are inside same jsp file. your answer is very near but some where its missing. please update separately your java 8 answer also.. – user3534759 Mar 12 '15 at 13:25
  • @NikosM. your code worked fine!! good effort. Thanks. – user3534759 Mar 13 '15 at 06:24

1 Answers1

2

Update

Try this (Java 8, see here for more ways to join an array in java)

<%  
  String[] array=["books","pens","cars"];
  String array_joined = '["' + String.join('","', array) + '"];';
%>

or use this instead to join the array in case the String.join menthod is not available

<%  
  String[] array=["books","pens","cars"];
  String array_joined = '"' + array[0] + '"';
  for (int i=1; i<array.length; i++) array_joined += ',' + '"' + array[i] + '"';
  array_joined = '[' + array_joined + '];';
%>

<script>
     var javaArray= <%= array_joined %> // renders ["books","pens","cars"];

     var tempArrayValue = [];

     function myFunction(){

     for(var i=0,j=0;i<javaArray.length;i++,j++){

     tempArrayValue[j]=javaArray[i]; //trying to store ["books","pens","cars"] values into "tempArrayValue"

     }

  }

</script>

Note i dont remember the jsp tags, so i may have used incorrect tags to render a jsp expresion above, use accordingly

found use <%= %> expression tag

Community
  • 1
  • 1
Nikos M.
  • 8,033
  • 4
  • 36
  • 43
  • var javaArray= ["<% String.join('", "', array); %>"]; this line, I am getting error as "invalid character constant" – user3534759 Mar 12 '15 at 12:26
  • @user3534759, actually this is supposed to be a jsp render expression tag, so i may have used incorrect tags <% %>, have mentioned this, i dont remember the tag syntax right now, looking for it – Nikos M. Mar 12 '15 at 12:29
  • @user3534759, ok [found it](http://en.wikipedia.org/wiki/JavaServer_Pages), use the <%= %> expression tag – Nikos M. Mar 12 '15 at 12:31
  • Nikos, var javaArray= ["<%=String.join('", "', array); %>"]; still giving error like this.Multiple annotations found at this line: - Type mismatch: cannot convert from String[] to any - Invalid character constant – user3534759 Mar 12 '15 at 12:36
  • @user3534759, is the code on same page as the rest code (where the array ["books","pens","cars"] is defined)? if yes, do you have Java 8 or other version? The String.join will work with Java 8 (as per the link on the answer). Giving alternative method, updating answer – Nikos M. Mar 12 '15 at 12:41
  • i am sorry, i am using java 7. can't I do it with Java 7? since all our development machines are installed with java 7. so – user3534759 Mar 12 '15 at 12:43
  • Does these changes support in Java 7 ?? var javaArray= [<%= array_joined; %>]; still saying as syntax error on token ";" . if i delete this still its showing error as "array_joined" can not be resolved to a variable. – user3534759 Mar 12 '15 at 12:54
  • @user3534759, oops sorry remove the ";", after array_joined, updating – Nikos M. Mar 12 '15 at 12:56
  • @user3534759, if array_joined cannot be resolved to a variable, it means the code is not on same page as the script code, ro not accessible – Nikos M. Mar 12 '15 at 12:58
  • @user3534759, please show whole code so i know where each part is placed – Nikos M. Mar 12 '15 at 12:59
  • @ Nikos M, I have edit my question please have look into that and update you code for java 8 as well thats great help may be in future. – user3534759 Mar 12 '15 at 13:21
  • @user3534759, ok updated answer, compute the array_joined from your data array (array1) and plug it in the javascript part where you need it, i.e `tempArrayLabel = "add values from array1 or device1";`. If this does not answer your question, there is no need to accept, as someone else may give better answer. – Nikos M. Mar 12 '15 at 13:33