Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi, I have page with a table, an

Status
Not open for further replies.

oraclec

Programmer
Dec 8, 2002
16
0
0
SG
Hi,

I have page with a table, and a button to add rows to this table dynamically.

This is a very basic concept I have of this program. It adds a row at a time:

<%
Function addrow()
%>
<tr>
<td>value1</td>
<td>value2</td>
</tr>
<%
End Function
%>

<form>
<input type=&quot;Submit&quot; value=&quot;Add Row&quot; onClick=&quot;addrow()&quot;>
</form>

How do I enclose the HTML tags in the function so that it actually adds my row? Or is my concept too basic?

Thanks for any help!

|~*Oraclec*~|
 
You cant call ASP function from client side.
You need to use client side scripting to add a row.
Code:
<html>
<script language=javascript>
function addRow()
{
	var tableObj=document.all.table1;
	var whereToInsert=tableObj.rows.length;
	var newRow=tableObj.insertRow(whereToInsert-1);
	var newCell=newRow.insertCell();
	newCell.innerText=whereToInsert-1;
}
</script>
<body>
<table name=table1 border=1 id=table1>
<tr>
<td>
	This table will add rows
</td>
</tr>

<tr>
<td align=center>
	<input type=button value=&quot;Add row&quot; onclick=&quot;addRow()&quot;>
</td>
</tr>
</table>
</body>
</html>

________
George, M
 
Thanks for the example! I added more columns to each of my rows by multiplying the following line of code--

var newCell=newRow.insertCell();

2 further questions:
(1) Is it possible to have text boxes or select lists for user input in the cells in each column?

(2) How can individual cells in each row be identified so that calculations on the values in the cells can be done, with the result showing in a different cell on the same row?

e.g. My Table has 3 columns: the 1st column is a text box, the 2nd, a drop-down list and the 3rd, a calculation field (when link in the cell is click, the calculated result appears in the same cell.)

Thanks again.

|~*Oraclec*~|
 
Try out this..

<html>
<script language=javascript>
var counter=0;
function addRow()
{
var tableObj=document.all.table1;
var whereToInsert=tableObj.rows.length;
var newRow=tableObj.insertRow(whereToInsert-1);
var newCell=newRow.insertCell();

var a=&quot;<INPUT type=text value='text' name=t&quot;+ counter++ + &quot;>&quot;;
var b=&quot;<INPUT type=checkbox value='text' name=c&quot;+ counter++ + &quot;>&quot;;
var c=&quot;<select name=sel&quot;+ counter++ + &quot;><option>1</option>&quot;;

newCell.insertAdjacentHTML(&quot;AfterBegin&quot;,c);
newCell.insertAdjacentHTML(&quot;AfterBegin&quot;,b);
newCell.insertAdjacentHTML(&quot;AfterBegin&quot;,a);

}
</script>

<body>
<table name=table1 border=1 id=table1>
<tr>
<td>
This table will add rows
</td>
</tr>

<tr>
<td align=center>
<input type=button value=&quot;Add row&quot; onclick=&quot;addRow()&quot;>
</td>
</tr>
</table>
</body>
</html>

 
LakshmiKiran has right, you can add HTML code inside using innerHtml property

________
George, M
 
Hi!

Thanks for all your help. There were some table layout problems though when the number of columns increased. But I've worked around the problem by adding and specifying one cell at a time. Below is a short abstract of my codes:

I actually need to capture the user input in each row and calculate speed from distance and time on the same row.

(1)
Does anyone know how to retrieve the values from the function within the script? The HTML text fields are enclosed with a string variable. Where do I put the <form> </form> tags so I can get the user inputs and do the calculation within the same page?

(2)
Also, I want to save everything, (after adding rows, having user inputs in each row, and do the calculations for all rows) in a database with my SAVE button beside the ADD ROW button.


I am grateful for any help. Below are my codes:

<HTML>

<HEAD>
<script language=javascript>

var counter=0;

function addRow()
{
var tableObj=document.all.DLAtable;
var whereToInsert=tableObj.rows.length;
var newRow=tableObj.insertRow(whereToInsert-0);

var newCell=newRow.insertCell();

var cell_dateDay=&quot;<select name=sel_dateDay&quot;+ counter++ + &quot;><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option><option>6</option><option>7</option><option>8</option><option>9</option><option>10</option><option>11</option><option>12</option><option>13</option><option>14</option><option>15</option><option>16</option><option>17</option><option>18</option><option>19</option><option>20</option><option>21</option><option>22</option><option>23</option><option>24</option><option>25</option><option>26</option><option>27</option><option>28</option><option>29</option><option>30</option><option>31</option>&quot;;
newCell.insertAdjacentHTML(&quot;AfterBegin&quot;,cell_dateDay);


var newCell=newRow.insertCell();

var cell_dateMth=&quot;<select name=sel_dateMth&quot;+ counter++ + &quot;><option>January</option><option>February</option><option>March</option><option>April</option><option>May</option><option>June</option><option>July</option><option>August</option><option>September</option><option>October</option><option>November</option><option>December</option>&quot;;
newCell.insertAdjacentHTML(&quot;AfterBegin&quot;,cell_dateMth);


var newCell=newRow.insertCell();

var cell_dateYr=&quot;<select name=sel_dateYr&quot;+ counter++ + &quot;><option>2003</option><option>2004</option><option>2005</option><option>2006</option><option>2007</option>&quot;;
newCell.insertAdjacentHTML(&quot;AfterBegin&quot;,cell_dateYr);

var newCell=newRow.insertCell();
var cell_dailydist=&quot;<INPUT type=text value='' size=5 name=txt_dailydist&quot;+ counter++ + &quot;>&quot;;
newCell.insertAdjacentHTML(&quot;AfterBegin&quot;,cell_dailydist);


var newCell=newRow.insertCell();
var cell_dailytimeHr=&quot;<INPUT type=text value='' size=3 name=txt_dailytimeHr&quot;+ counter++ + &quot;>&quot;;
newCell.insertAdjacentHTML(&quot;AfterBegin&quot;,cell_dailytimeHr);

var newCell=newRow.insertCell();
var cell_dailytimeMin=&quot;<INPUT type=text value='' size=2 name=txt_dailytimeMin&quot;+ counter++ + &quot;>&quot;;
newCell.insertAdjacentHTML(&quot;AfterBegin&quot;,cell_dailytimeMin);

var newCell=newRow.insertCell();
var cell_dailyspd=&quot;<INPUT type=text value='' size=4 name=txt_dailyspd&quot;+ counter++ + &quot;><a href='#'>[calculate]</a>&quot;;
newCell.insertAdjacentHTML(&quot;AfterBegin&quot;,cell_dailyspd);
}
</script>
</HEAD>



<BODY>

<table width=&quot;100%&quot; bgcolor=&quot;#E8E8E8&quot; border=&quot;1&quot; name=&quot;DLAtable&quot; id=&quot;DLAtable&quot;>

<tr>
<td align=&quot;center&quot;><input type=button value=&quot;Add row&quot; onclick=&quot;addRow()&quot;>
</td>


<!-- A save changes button to submit form values here -->
<td><input type=&quot;submit&quot; value=&quot;Save Changes&quot;><img src=&quot;images/save.gif&quot;>
</td>

</tr>

<tr>
<td>Date</td>
<td>Month</td>
<td>Year</td>
<td>Daily Distance</td>
<td>Daily Time (Hrs)</td>
<td>Daily Time (Mins)</td>
<td>Daily Avg Speed</td>
</tr>

</table>

</BODY>
</HTML>



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top