如何使用JSON将Java arraylist赋予javascript函数 [英] How to give java arraylist to javascript function using JSON
问题描述
我有一个通过如下所示的javascript函数创建的表
I have a table which is created through javascript function which is as below
现在您可以看到单元格"VAT5.5"为空.我必须从SQL数据库中获取数据库值,并且必须将其保留在下拉框中.我能够获取数据库值并将其存储在arraylist中.但是我面临的问题是将arraylist导出到javascript函数.听说我们可以通过JSON将arraylist值传递给脚本函数,所以我试图解析并将其发送到javascript函数,但我不知道JSON代码正确.所以我遇到了一个错误.
Now you can see the cell "VAT5.5" is empty.there i have to get the database values from SQL database and i have to keep those in a dropdown box.I am able to get the database values and store it in an arraylist.But the problem i am facing is to export that arraylist to the javascript function.I heard we can pass the arraylist values to the script function through JSON.So i tried to parse and sent those to javascript function but i didnt know the JSON code properly.So i am facing an error.
我的代码如下
<script language="javascript">
// Add row to the HTML table
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 cell7 = row.insertCell(6);
var element7 = document.createElement("select");
element7.setAttribute('multiple', '');
var optarr = document.getElementById('data').value;
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);
}
<%
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);
System.out.println("Array is" +ar);
}
int size = ar.size();
System.out.println("size is" +size);
%>
<%
con.close();
smt.close();
rs.close();
}
catch(SQLException ex){
ex.printStackTrace();
}
%>
所以如何将以上讲者介绍给javascript函数.希望我能对您有所帮助
So how to get the above arralist to a javascript function.I hope i will get some help
推荐答案
<%
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);
System.out.println("Array is" +ar);
}
int size = ar.size();
System.out.println("size is" +size);
%>
<%
con.close();
smt.close();
rs.close();
}
catch(SQLException ex){
ex.printStackTrace();
}
%>
var cell7 = row.insertCell(6);
var element7 = document.createElement("select");
element7.setAttribute('multiple', '');
var jsArray = [];
<%for(int i=0;i<ar.size();i++){%>
jsArray.push("<%= ar.get(i)%>");
<%}%>
// var optarr = ["vat1","vat2","vat3","vat4","vat5","vat6","vat7","vat8","vat9"];
alert(jsArray.length);
alert(jsArray);
for(var i = 0;i<jsArray.length;i++)
{
var opt = document.createElement("option");
opt.text = jsArray[i];
opt.value = jsArray[i];
opt.className = jsArray[i];
element7.appendChild(opt);
}
element7.setAttribute('id', 'vat5'); //set the id attribute
element7.setAttribute('name','tax'+rowCount);
element7.setAttribute('value','vat5');
cell7.appendChild(element7);
这篇关于如何使用JSON将Java arraylist赋予javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!