Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2008
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Dynamically Adding rows into the Table For the data retrieved from the database

    Here iam fetching the table from an Oracle database, but i need to add text boxes dynamically in to table based on the no. of columns retrieved from the data base. I'am unable to get the textboxes to be dynamically generated on pressing the AddRow button.

    This is pg1.jsp

    <%@page import="java.util.*"%>
    <%@page import="java.sql.*"%>
    <html>
    <head>
    <title></title>


    </head>
    <body>
    <%
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:mydsn","scott","tiger");
    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery("select * from tab");
    %>
    <form method="POST" action="pg2.jsp">
    <select name="s1">
    <%
    while(rs.next())
    {
    %>
    <option><%=rs.getString("TName")%></option>
    <%
    }
    %>
    </select>
    <input type="submit" value="OK"></form>
    <%
    con.close();
    %>

    </body>
    </html>

    This is pg2.jsp

    <%@page import="java.util.*"%>
    <%@page import="java.sql.*"%>
    <html>
    <head>
    <%
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:mydsn","scott","tiger");
    Statement st = con.createStatement();
    String s=request.getParameter("s1");
    ResultSet rs = st.executeQuery("select * from "+s+"");
    ResultSetMetaData m=rs.getMetaData();
    //String m2=m.getColumnName();
    int cols=m.getColumnCount();
    //Count Of No. Of Columns In The Table
    out.println("No of Cols"+cols);
    out.println(s);
    %>
    <script type="javascript">


    function AddRow() {
    var oTBody = document.getElementById('tablebody');
    // add row
    //var oTR = document.createElement('TR');
    //oTBody.appendChild(oTR);
    var CellsInRow=cols;
    alert(CellsInRow);
    for(var i=0; i<CellsInRow; i++) { // number of cells in row
    // create input
    var oInput = document.createElement('input');
    //oInput.setAttribute('name', 'cell'+(num++));
    oInput.setAttribute('type', 'text');
    oInput.setAttribute('size', '10');
    // create cell containing input
    var oTD = document.createElement('TD');
    oTD.appendChild(oInput);
    // add to row
    oTR.appendChild(oTD);
    }
    // add to table
    oTBody.appendChild(oTR);
    }

    </script>

    </head>

    <body>
    <table border="1"><tr>

    <td><%
    for(int i=1;i<=cols;i++)
    out.println(m.getColumnName(i));
    %></td></tr>
    <tr> <td id="tablebody"></td></tr>

    </table>
    <input type="button" value="AddRow" onclick="AddRow()">
    <%
    con.close();
    %>
    </body>
    </html>

    I'am posting in this forum as i am not sure whether it is JSP coding needed for the table or Javascript.

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    its most likely a javascript problem...so, post the generated html in the javascript forum
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •