FIFA oneterahertz FIFA oneterahertz - 5 months ago 13
jQuery Question

How to get a jsp string based array to a JavaScript variable

I don't know whether what am I asking is possible or not, but I have to ask because i need a clarification on this. I have a database table which is as followsenter image description here

I have a JavaScript function for dynamically adding rows which is as follows

function addRow() {
var table = document.getElementById('my_table'); //html table
var rowCount = table.rows.length; //no. of rows in table
var columnCount = table.rows[0].cells.length; //no. of columns in table
var row = table.insertRow(rowCount); //insert a row

var cell1 = row.insertCell(0); //create a new cell
var element1 = document.createElement("input"); //create a new element
element1.type = "checkbox"; //set the element type
element1.setAttribute('id', 'newCheckbox'); //set the id attribute
element1.setAttribute('checked',true); //set checkbox by default checked
cell1.appendChild(element1); //append element to cell

var cell2 = row.insertCell(1);
var element2 = document.createElement("input");
element2.type = "text";
element2.setAttribute('id', 'newInput'); //set the id attribute
element2.setAttribute('name', 'sl'+rowCount);
element2.setAttribute('style', 'width: 50px');
cell2.appendChild(element2);

var cell3 = row.insertCell(2);
var element3 = document.createElement("input");
element3.type = "textarea";
element3.setAttribute('rows', '4');
element3.setAttribute('cols','40');
element3.setAttribute('id', 'newInput'); //set the id attribute
element3.setAttribute('name', 'discription'+rowCount);
cell3.appendChild(element3);

var cell4 = row.insertCell(3);
var element4 = document.createElement("input");
element4.type = "text";
element4.setAttribute('id', 'newInput'); //set the id attribute
element4.setAttribute('name', 'quantity'+rowCount);
cell4.appendChild(element4);


var cell5 = row.insertCell(4);
var element5 = document.createElement("input");
element5.type = "text";
element5.setAttribute('id', 'newInput'); //set the id attribute
element5.setAttribute('name', 'price'+rowCount);
cell5.appendChild(element5);



var cell6 = row.insertCell(5);
var element6 = document.createElement("input");
element6.type = "text";
element6.setAttribute('id', 'newInput'); //set the id attribute
element6.setAttribute('name', 'CST'+rowCount);
element6.setAttribute('style', 'width: 50px');
cell6.appendChild(element6);


var cell7 = row.insertCell(6);
var element7 = document.createElement("select");
element7.setAttribute('multiple', '');
var optarr = ['vat1','vat2','vat3','vat4','vat5','vat6','vat7','vat8','vat9'];
for(var i = 0;i<optarr.length;i++)
{
var opt = document.createElement("option");
opt.text = optarr[i];
opt.value = optarr[i];
opt.className = optarr[i];
element7.appendChild(opt);
}


element7.setAttribute('id', 'vat5'); //set the id attribute
element7.setAttribute('name','tax'+rowCount);
element7.setAttribute('value','vat5');

cell7.appendChild(element7);
}


And now i have written code to get the database values and store in array.

<%
Connection con = null;
ResultSet rs = null;
ArrayList<String> ar=new ArrayList<String>();
String taxgroup;

try {
DBConnect db = new DBConnect();
con = db.getCon();
String sql = "select distinct TaxGroup from marketing_database.tax_info";
PreparedStatement smt = con.prepareStatement(sql);
rs = smt.executeQuery();

while(rs.next())
{
taxgroup=rs.getString(1);

ar.add(taxgroup);
}
int size = ar.size();
String[] prodValues = (String[])ar.toArray(new String[ar.size()] );
System.out.println("size is" +size);
System.out.println("Names are" +Arrays.toString(prodValues));
%>
<%
con.close();
smt.close();
rs.close();

}
catch(SQLException ex){
ex.printStackTrace();
}

%>


Now I need to pass the array and its values to the JavaScript variable which is

var optarr = ['vat1','vat2','vat3','vat4','vat5','vat6','vat7','vat8','vat9'];


I have to replace
['Vat1','vat2','vat3','vat4','vat5','vat6','vat7','vat8','vat9']
with the array I wrote in the database code

Answer

Simple way to do that is, create hidden field and assign value to it by jsp for example

<input type='hidden' name='' id='data' value=' <%= someExpressionHere  %>' />

Now is Js

document.getElementById('data').value;

In this way you can transfer value from jsp to the JavaScript. In your case you want to pass array, so you can create multiple field.

var optarr =  ['vat1','vat2','vat3','vat4','vat5','vat6','vat7','vat8','vat9'];

As it have fixed column, you can create finite number of hidden field.

Comments