如何使用 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 数据库中获取数据库值,并且必须将这些值保留在下拉框中.我能够获取数据库值并存储它在一个数组列表中.但我面临的问题是将该数组列表导出到 javascript 函数.我听说我们可以通过 JSON 将数组列表值传递给脚本函数.所以我尝试解析并将它们发送到 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();
}
%>
那么如何让上述 arralist 使用 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屋!