好的,我已经研究这个问题很长时间了,并且已经对其进行了彻底的研究。首先,我正在使用 Spring Tool Suite - MVC 项目。在 .jsp 页面上工作
我希望用户能够根据不同的用户输入更新表格....按钮、价格范围输入等。
到目前为止,我研究过的最佳方法是将表的创建包装在一个 js 函数中,然后创建其他函数来更改表、添加表、更新表或我想做的任何其他事情。我没有运气使用函数创建数据表,我现在能做的最好的就是使用 java 连接到我的数据库并创建一个成功加载和显示数据库中数据的 SINLGE 表,问题是我无法改变页面加载后的表,每次基于新的 sql 查询刷新表的尝试都失败了。
Datatables 有一个相当大的 api,我可以用它来向表中添加一行,但是其他看起来很有用的功能(例如更新表)不起作用。
这让我开始思考是否应该彻底重新考虑我的方法。我应该采取什么方法?
如果需要我会发布代码
提前致谢
最佳答案
<%
ArrayList<ArrayList<String>> aaList = new ArrayList<ArrayList<String>>();
ResultSet rs;
Statement statement = null;
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://ics321instance.cglh44ydeyny.us-east-1.rds.amazonaws.com/ics321", "user", "password");
statement = conn.createStatement();
//this query should return all items
rs = statement.executeQuery("select * from Product where price < 10;");
int counter=1;
ResultSetMetaData metaData = rs.getMetaData();
%>
<table id = "example">
<thead>
<tr>
<%
for(int q = 1; q<=metaData.getColumnCount();q++)
{%>
<th><%= metaData.getColumnName(q)%></th>
<%
}
%>
</tr>
</thead>
<tbody>
<%
while(rs.next())
{
%>
<tr>
<%
for(int i = 1; i<=metaData.getColumnCount();i++)
{%>
<td><%= rs.getString(i)%></td>
<%
}
%>
</tr>
<%
}
%>
</tbody>
</table>
所以在这里我连接到数据库并在 jsp 中执行查询 - 我知道这是不好的做法但我想不出任何其他方式
在标题中我有我的 js
$(function() {
$("#example").dataTable({
"sScrollY": "400px",
"bPaginate": false,
"bScrollCollapse": true
});
}
我认为正确的方法是在我的 Controller 中使用 JDBCTemplate - 但我不确定如何在我的 .jsp 文件中引用该数据。
同样,我的目标是拥有一个表(datatables jquery 插件)并根据用户与 jsp 页面、按钮、文本输入等的交互更新该表。
数据表 API 中有一个方法 - fnDraw(),它应该重绘表格,但我不知道该函数放在哪里。
谢谢
关于javascript - 使用 javascript 函数打印出动态 html 表(jquery 数据表插件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20454501/