Home|Sitemap|Contact

How can you dynamically add rows and fields to a table?


Header


<!--METADATA TYPE="typelib" uuid="00000205-0000-0010-8000-00AA006D2EA4" -->
<%
Option Explicit
Session.CodePage=65001
Response.Charset="UTF-8"

' no browser caching of this page !! to be used on all pages
Response.Expires=-1
Response.ExpiresAbsolute = Now() - 1

' do not allow proxy servers to cache this page !! to be used on all pages
Response.CacheControl="private"
Response.CacheControl="no-cache"
Response.CacheControl="no-store"

Dim cmd, conn, RS, cmdText, param, numAffected

Set cmd = Server.CreateObject("ADODB.Command")
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open Application("examples")

cmdText = "SELECT code, description FROM states"
cmd.commandText = cmdText
Set cmd.ActiveConnection = conn

Set rs = cmd.Execute(numAffected,,adCmdText)
if NOT (rs.bof AND rs.eof) then
  Dim tmpStr
  tmpStr = "<scr" & "ipt type=" & chr(34) & "text/javascr" & "ipt" & chr(34) & ">" & vbLF

  ' declare a client side array
  tmpStr = tmpStr & " var csArray=new Array();" & vbLF & vbLF

  Dim idx, item
  idx = 0 ' JavaSccript arrays are zero based.

  ' fill the array from our recordset
  While NOT rs.eof
    tmpStr = tmpStr & " csArray["& idx &"]=(['"
    for each item in rs.fields
      tmpStr = tmpStr & item & "','"
    next

    ' backoff the trailing ,' and terminate the line with ]);
    tmpStr = Left(tmpStr, Len(tmpStr)-2) & "]);" & vbLF

    ' increment our array index
    idx = idx + 1
    rs.movenext
  Wend

  tmpStr = tmpStr & "</scr" & "ipt>" & vbLF
end if
rs.close
Set rs = nothing
Set cmd=nothing
conn.close
Set conn=nothing

Dim posted, numRows

posted = ""

' if we have a post - process it
if request.servervariables("HTTP_METHOD")="POST" then
  if CInt(request.form("numRows")) <> 0 then
    ' how many rows were posted?
    numRows = CInt(request.form("numRows"))

    ' loop through the rows and associated the checkboxes
    ' to the selected values in the select lists
    for idx=0 to numRows
      if request.form("cb"&idx) = "" then
        posted = posted & "Row " & idx & " Not Checked "
      else
        posted = posted & "Row " & idx & " Checked "
      end if
      if request.form("sel"&idx) = "" then
        posted = posted & " Value=Empty<br>"
      else
        posted = posted & " Value=" & request.form("sel"&idx) & "<br>"
      end if
    next
  end if
end if
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="author" content="Roderick Divilbiss">
<meta name="copyright" content="© 2005, 2006 Roderick Divilbiss">
<title>Adding Dynamic Rows To Form</title>
<!--write our database results as an array-->
<% response.write tmpStr %>
<script type="text/javascript">
<!--
var numberOfRows = 0;

function insertRow(tblId) {
    var tbl = document.getElementById(tblId);
    var iteration = tbl.tBodies[0].rows.length;
    newRow = tbl.tBodies[0].insertRow(-1);
    var newCell = newRow.insertCell(0);
    var newCell1 = newRow.insertCell(1);
    var el = document.createElement('input');
    el.type = 'checkbox';
    el.name = 'cb' + iteration;
    el.id = 'cb' + iteration;
    newCell1.appendChild(el);

    var newCell2 = newRow.insertCell(1);
    var newCell1 = newRow.insertCell(1);
    var sel = document.createElement('select');
    sel.name = 'sel' + iteration;
    sel.id = 'sel' + iteration;
    // build the select from the retrieved database rows
    // we stored in a JS array.
    for (var idx=0;idx<csArray.length;idx++) {
        sel.options[idx] = new Option(csArray[idx][0], csArray[idx][1]);
    }
    newCell2.appendChild(sel);
    // update hidden rows count field
    document.getElementById('numRows').value=iteration;
}


function deleteAllRows(tblId) {
    var tbl = document.getElementById(tblId);
    for (var i=tbl.tBodies[0].rows.length-1; i>=0; i--) {
        tbl.tBodies[0].deleteRow(i);
    }
}
//-->
</script>
</head>

<body>
<!--posted results if any-->
<%=posted%>

<form action="dynamicRows.asp" method="post">
<p>
<input value="Add" onclick="insertRow('tblInsertRow');" type="button">
<input value="Reset" onclick="deleteAllRows('tblInsertRow');" type="button">
<input value="Submit" type="submit">
</p>
<table id="tblInsertRow" border="0" cellspacing="0">
  <thead>
    <tr>
      <th colspan="2">Header</th>
    </tr>
  </thead>
  <tbody></tbody>
</table>
<input id="numRows" name="numRows" type="hidden" value="0">
</form>
</body>
</html>