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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamicly Create A Table 1

Status
Not open for further replies.

mjstark

Programmer
Jun 14, 2001
8
US
I am looking for a script that will allow a table to be dynamicly created. I would like to be able to have a user input the number of rows, columns, cell spacing, cell padding, borders on/off, and border width. Then, they would hit submit and the table would be created for them on the page. I can understand how this can be done using a form, document.write and looping I just don't know how to implement it. If anyone knows where I can find a script to create a table with all or even some of these elements I would appreciate it.
 
this is from
Might be what your looking for

<!-- TWO STEPS TO INSTALL TABLE MAKER:

1. Paste the FRAMESET code into your HTML document
2. Add the next code into the BODY of your table-maker-left.html -->

<!-- STEP ONE: Paste the FRAMESET code into your HTML document -->

<HTML>
<HEAD>
<TITLE>Table Maker</TITLE>

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!-- Original: Andy Augustine -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! -->

<!-- Begin
var myTable = new tableObj('','Default',1,1,1,1,2,'','');

function propObj(string,outstring) {
this.value = string;
this.out = outstring + string;
}
function setValues(form) {
this.alignment.value= form.alignment.value;
this.border.value= form.border.value;
this.cellspacing.value = form.cellspacing.value;
this.cellpadding.value = form.cellpadding.value;
this.rows.value = form.numrows.options[form.numrows.selectedIndex].text;
this.cols.value = form.numcols.options[form.numcols.selectedIndex].text;
this.calign.value = form.calign.value;
this.cvalign.value = form.cvalign.value;
}
function updateProps() {
this.alignment.out= &quot; ALIGN=&quot; + this.alignment.value;
this.border.out= &quot; BORDER=&quot; + this.border.value;
this.cellspacing.out = &quot; CELLSPACING=&quot; + this.cellspacing.value;
this.cellpadding.out = &quot; CELLPADDING=&quot; + this.cellpadding.value;
this.rows.out = &quot; ROWS=&quot; + this.rows.value;
this.cols.out = &quot; COLS=&quot; + this.cols.value;
this.calign.out = &quot; ALIGN=&quot; + this.calign.value;
this.cvalign.out = &quot; VALIGN=&quot; + this.cvalign.value;
}
function tableObj(align,caption,border,cellpadding,cellspacing,rows,cols,calign,cvalign) {
this.alignment = new propObj(align,&quot; ALIGN=&quot;);
this.border = new propObj(border,&quot; BORDER=&quot;);
this.cellspacing = new propObj(cellspacing,&quot; CELLSPACING=&quot;);
this.cellpadding = new propObj(cellpadding,&quot; CELLPADDING=&quot;);
this.rows = new propObj(rows,&quot; ROWS=&quot;);
this.cols = new propObj(cols,&quot; COLS=&quot;);
this.calign = new propObj(calign,&quot; ALIGN=&quot;);
this.cvalign = new propObj(cvalign,&quot; VALIGN=&quot;);
this.setValues = setValues;
this.updateProps = updateProps;
}
function dumpProps(obj,obj_name) {
var result = &quot;&quot;, i =&quot;&quot;;
for (i in obj)
result += obj_name +&quot;.&quot;+ i +&quot; = &quot;+ obj.value +&quot;\n&quot;;
return result;
}
function dumpTags(obj,obj_name) {
var result = &quot;&quot;, i =&quot;&quot;;
for (i in obj)
result += obj_name +&quot;.&quot;+ i +&quot;.out = &quot;+ obj.out +&quot;\n&quot;;
return result;
}
function drawTable(table) {
var tabdoc = this.tableframe.document;
table.updateProps();
tabdoc.open();
tabdoc.write('<HTML><BODY>---Your Table---<br><br><br>')
tabdoc.write('<TABLE ')
tabdoc.write(table.alignment.out)
tabdoc.write(table.cellspacing.out)
tabdoc.write(table.cellpadding.out)
tabdoc.write(table.border.out)
tabdoc.write(' >')
for (var i=1; i <= table.rows.value; i++) {
tabdoc.write('<TR' +table.cvalign.out+ '>')
for (var j = 1; j <= table.cols.value; j++) {
tabdoc.write('<TD ' +table.calign.out+ '>' +(j*i)+ '</TD>')
}
tabdoc.write('</TR>')
}
tabdoc.write('</TABLE></HTML></BODY>')
tabdoc.close()
}
function showSource() {
var opts = 'width=350,height=350,scrollbars=yes,directories=no,status=yes,location=no,toolbar=no';
var ltag = '<'
var gtag = '>'
var sourcewin = window.open('','source',opts)
sourcewin= window.open('','source')
sourcewin.status=&quot;Cut and paste this info into your web page&quot;
sourcewin.document.write('<HTML><BODY BGCOLOR=&quot;#FFFFFF&quot; <PRE><XMP>')
sourcewin.document.writeln(ltag + 'TABLE ')
if (myTable.alignment.out != '')
sourcewin.document.writeln(myTable.alignment.out)
sourcewin.document.writeln(myTable.border.out)
sourcewin.document.writeln(myTable.cellspacing.out)
sourcewin.document.writeln(myTable.cellpadding.out + gtag)
for (var i=1; i <= myTable.rows.value; i++) {
sourcewin.document.writeln(ltag+ 'TR' +myTable.cvalign.out+ ' ' +myTable.calign.out+ gtag)
for (var j = 1; j <= myTable.cols.value; j++) {
sourcewin.document.writeln(' ' +ltag+ 'TD' +gtag +(j*i)+ ltag+ '/TD' +gtag)
}
sourcewin.document.writeln(ltag+ '/TR' +gtag)
}
sourcewin.document.writeln(ltag+ '/TABLE' +gtag)
sourcewin.document.write('</XMP></PRE><CENTER>')
sourcewin.document.write('<FORM NAME=menu><INPUT TYPE=&quot;button&quot; ');
sourcewin.document.write('VALUE=&quot;Close&quot; onClick=&quot;self.document.clear();self.close()&quot;>')
sourcewin.document.write('<P><P><P><P><P><P></FORM>')
sourcewin.document.write('</CENTER></BODY></HTML>')
sourcewin.document.close()
}
// End -->
</SCRIPT>
</HEAD>

<frameset cols=&quot;50%,50%&quot; onLoad=&quot;parent.myTable.setValues(this.info.tform);parent.drawTable(parent.myTable)&quot;>

<frame src=&quot;table-maker-left.html&quot; name=&quot;info&quot;>
<frame src=&quot;javascript:void(0)&quot; name=&quot;tableframe&quot; marginwidth=&quot;4&quot;>

</frameset>
</html>



<!-- STEP TWO: Add to the BODY of your table-maker-left.html -->

<html>
<head>

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!-- Begin
function init() {
with (document) {
tform.alignment.value=&quot;left&quot;
tform.align.value=&quot;left&quot;
tform.calign.value=&quot;left&quot;
tform.cvalign.value=&quot;center&quot;
}
}
function clearit(form) {
form.alignment.value = &quot;left&quot;
form.align.value=&quot;left&quot;
form.border.value = 1
form.cellspacing.value = 1
form.cellpadding.value = 1
form.numrows.selectedIndex = 0
form.numcols.selectedIndex = 1
form.calign.value = ''
form.cvalign.value = ''
}
function isNum(entry) {
var str = entry.value;
for (var i = 0; i < str.length; i++) {
var ch = str.substring(i, i + 1)
if ((ch < &quot;0&quot; || &quot;9&quot; < ch) && ch != '.') {
alert(&quot;You must enter a number.&quot;)
entry.value=0
entry.focus()
entry.select()
}
}
}
// End -->
</SCRIPT>
</head>

<body onLoad=&quot;init()&quot; bgcolor=&quot;#ffffff&quot;>

<center><table border=1><form name=&quot;tform&quot;>
<tr align=center>
<td><input type=&quot;reset&quot; name=&quot;clear&quot; value=&quot;Reset to Defaults&quot; onClick=&quot;clearit(this.form)&quot;>
<td>Alignment:<br>
<input type=&quot;radio&quot; name=&quot;align&quot; value=&quot;left&quot; onClick=&quot;document.tform.alignment.value=this.value&quot; checked>Left
<input type=&quot;radio&quot; name=&quot;align&quot; value=&quot;center&quot; onClick=&quot;document.tform.alignment.value=this.value&quot;>Center
<input type=&quot;radio&quot; name=&quot;align&quot; value=&quot;right&quot; onClick=&quot;document.tform.alignment.value=this.value&quot;>Right
<input type=&quot;hidden&quot; name=&quot;alignment&quot; value=&quot;left&quot;>
<tr align=center>
<td colspan=2>Border Width (in Pixels):
<input name=&quot;border&quot; value=&quot;1&quot; size=&quot;3&quot; onChange=&quot;isNum(this)&quot; value=1>
<br>
<input type=&quot;radio&quot; name=&quot;br&quot; value=&quot;0&quot; onClick=&quot;tform.border.value=0&quot;>0
<input type=&quot;radio&quot; name=&quot;br&quot; value=&quot;1&quot; onClick=&quot;tform.border.value=1&quot; checked>1
<input type=&quot;radio&quot; name=&quot;br&quot; value=&quot;2&quot; onClick=&quot;tform.border.value=2&quot;>2
<input type=&quot;radio&quot; name=&quot;br&quot; value=&quot;5&quot; onClick=&quot;tform.border.value=5&quot;>5
<input type=&quot;radio&quot; name=&quot;br&quot; value=&quot;10&quot; onClick=&quot;tform.border.value=10&quot;>10
<input type=&quot;radio&quot; name=&quot;br&quot; value=&quot;15&quot; onClick=&quot;tform.border.value=15&quot;>15
<tr>
<td>Cellpadding:<input name=&quot;cellpadding&quot; size=3 value=1 onChange=&quot;isNum(this)&quot;>
<td>Cellspacing:<input name=&quot;cellspacing&quot; size=3 value=1 onChange=&quot;isNum(this)&quot;>
<tr align=center>
<td>Rows:<br><select name=&quot;numrows&quot;>
<option selected>1 <option>2 <option>3 <option>4 <option>5
</select>
<td>Cols:<br><select name=&quot;numcols&quot;>
<option>1 <option selected>2 <option>3 <option>4 <option>5
</select>
<tr>
<td>HORIZONTAL ALIGNMENT (cells):<br>
<input type=&quot;radio&quot; name=&quot;calign&quot; value=&quot;left&quot; onClick=&quot;document.tform.calign.value=this.value&quot; checked>Left
<input type=&quot;radio&quot; name=&quot;calign&quot; value=&quot;center&quot;onClick=&quot;document.tform.calign.value=this.value&quot;>Center
<input type=&quot;radio&quot; name=&quot;calign&quot; value=&quot;right&quot; onClick=&quot;document.tform.calign.value=this.value&quot;>Right
<td>VERTICAL ALIGNMENT (cells):<BR>
<input type=&quot;radio&quot; name=&quot;cvalign&quot; value=&quot;top&quot; onClick=&quot;document.tform.cvalign.value=this.value&quot;>Top
<input type=&quot;radio&quot; name=&quot;cvalign&quot; value=&quot;center&quot; onClick=&quot;document.tform.cvalign.value=this.value&quot; checked>Center
<input type=&quot;radio&quot; name=&quot;cvalign&quot; value=&quot;bottom&quot; onClick=&quot;document.tform.cvalign.value=this.value&quot;>Bottom
<tr align=center>
<td colspan=2><input type=&quot;button&quot; name=&quot;drawtable&quot; value=&quot;Draw this table!&quot; onClick=&quot;parent.myTable.setValues(this.form);parent.drawTable(parent.myTable)&quot;>
</tr>
<tr>
<td colspan=2>The following features correspond to bottom frame:</TD>
</tr>
<tr align=center>
<td colspan=2><input type=&quot;button&quot; value=&quot;See HTML code for table!&quot; onClick=&quot;parent.showSource()&quot;>
</tr>
<tr align=center>
<td colspan=2><input type=&quot;button&quot; value=&quot;View table values&quot; onClick=&quot;alert(parent.dumpProps(parent.myTable,'myTable'))&quot;>
</tr>
</table>
</form></table></center>
</body>
</html>

<p><center>
<font face=&quot;arial, helvetica&quot; size=&quot;-2&quot;>Free JavaScripts provided<br>
by <a href=&quot; JavaScript Source</a></font>
</center><p>

<!-- Script Size: 8.35 KB --> I help at your own risk, if I brake it sorry! But if I fixed it, let me know with a
star.gif
[thumbsup2]
admin@onpntwebdesigns.com
 
I posted once a script similar to what you need.

Read this: thread216-202334

You can use the same idea to make yours or adjust that one to your needs.

 
I've been bit by the i, had to happen sooner or later[lol]
I help at your own risk, if I brake it sorry! But if I fixed it, let me know with a
star.gif
[thumbsup2]
admin@onpntwebdesigns.com
 
onpnt thanks that is exactly what I was looking for.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top