...

View Full Version : Dynamically Adding rows into the Table For the data retrieved from the database



venkatgfo
01-29-2008, 02:49 PM
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.

shyam
01-30-2008, 08:16 AM
its most likely a javascript problem...so, post the generated html in the javascript forum (http://www.codingforums.com/forumdisplay.php?f=2)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum