使用javascript函数打印出dymanic html表(jquery datatable插件)

时间:2013-12-08 14:26:16

标签: javascript jquery html mysql database

好的,所以我已经讨论了这个问题很长一段时间了,并对它进行了彻底的研究。首先,我使用的是Spring Tool Suite - MVC Project。在.jsp页面上工作

我正在尝试找出动态创建和编辑HTML表的最佳/适当/有效方法,该表将根据来自MySql数据库的数据链接到dataTables(jQuery插件)表。

我希望用户能够根据不同的用户输入....按钮,价格范围输入等更新表格。

到目前为止,我研究的最好的方法是将表的创建包装在js函数中,然后创建其他函数,这些函数将改变表,添加它,更新它,或者我想做的任何其他事情。我没有运气使用函数创建数据表,因为现在我能做的最好是使用java连接到我的数据库并创建一个SINLGE表,成功加载并显示数据库中的数据,问题是我无法改变页面加载后的表,基于新的SQL查询刷新表的每次尝试都失败了。

Datatables有一个非常大的api,我可以使用它来向表中添加一行,但其他看起来很有用的函数,比如更新表也不行。

这让我想到我的方法是否应该完全重新思考。我应该采取什么方法?

如果需要,我会发布代码

提前谢谢

1 个答案:

答案 0 :(得分:0)

 <%
 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>

所以在这里我正在建立与db的连接并在jsp中执行查询 - 我知道这是不好的做法,但我无法想出任何其他方式

在标题中我有我的js

$(function() {
     $("#example").dataTable({
       "sScrollY": "400px",
       "bPaginate": false,
       "bScrollCollapse": true 
     });
}

我认为正确的方法是在我的控制器中使用JDBCTemplate - 但我不确定如何在我的.jsp文件中引用该数据。

我的目标是再次拥有一个表(datatables jquery插件),并根据用户与jsp页面,按钮,文本输入等的交互来更新表。

有一种方法无法使用数据表api - fnDraw(),它应该重绘表格,但我不知道在哪里放置函数。

谢谢