PDA

View Full Version : complex javascript table


yubnob
09-28-2004, 10:15 PM
I need about 20 rows and 10 columns - each field will be a text form field with a value. When any value is changed some other values may change (totals)... All I can find are simple samples of calculators, etc - anyone have anything this complex?

so, for example 1-10 across and a-j down all have numbers

1a is changed, so the totals in 10a and 1j should change accordingly (one with the total of the row across and one with the total of the column down)

Any guidance toward something this complex would really be loved and appreciated.

thank you!

Willy Duitt
09-29-2004, 10:49 AM
This script (http://www.codingforums.com/showthread.php?t=43421) posted by Basscist in the Post a Javascript forum should provide you with a basis on how to dynamically build a table....

.....Willy

Kor
09-29-2004, 11:22 AM
Try this basic DOM example. You may add as many rows/columns with text fields you want. No need to modify script or give id or names to your text fields, but if you need extra cells in table (with no textfield in them) take care about the arrays increment.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script language="JavaScript" type="text/JavaScript">
function calculate(c){
d=document.getElementById('tab');
totRow=0;
totCol=0;
for(var i=0;i<d.rows.length;i++){
for(var j=0;j<d.rows[i].cells.length;j++){
if(c.parentNode==d.rows[i].cells[j]){
for (var k=0;k<d.rows[i].cells.length-1;k++){
totRow=totRow+Number(d.rows[i].cells[k].firstChild.value);
}
for (var m=0; m<d.rows.length-1;m++){
totCol=totCol+Number(d.rows[m].cells[j].firstChild.value);
}
d.rows[i].cells[d.rows[i].cells.length-1].firstChild.nodeValue=totRow;
d.rows[d.rows.length-1].cells[j].firstChild.nodeValue=totCol;
}
}
}
}
</script>
</head>

<body>
<form>
<table width="400" border="0" cellpadding="4" id="tab">
<tr>
<td><input type="text" onkeyup="calculate(this)"></td>
<td><input type="text" onkeyup="calculate(this)"></td>
<td width="10%" bgcolor="#CCCCCC">&nbsp; </td>
</tr>
<tr>
<td><input type="text" onkeyup="calculate(this)"></td>
<td><input type="text" onkeyup="calculate(this)"></td>
<td bgcolor="#CCCCCC">&nbsp; </td>
</tr>
<tr>
<td><input type="text" onkeyup="calculate(this)"></td>
<td><input type="text" onkeyup="calculate(this)"></td>
<td bgcolor="#CCCCCC">&nbsp; </td>
</tr>
<tr bgcolor="#CCCCCC">
<td>&nbsp; </td>
<td>&nbsp; </td>
<td>&nbsp;</td>
</tr>
</table>

</form>
</body>
</html>

Garadon
09-29-2004, 11:25 AM
or you can try my super fast written MathTable :). which should pretty much do what you want :)

Kor
09-29-2004, 11:29 AM
a small add

d.rows[d.rows.length-1].cells[j].firstChild.nodeValue=totCol;break;

yubnob
09-29-2004, 02:53 PM
The table codes here posted are nice, and the math table is getting me some useful stuff.

Any other samples like the math table?

Here is an applet example, but not really helpful as I need javascript.

http://www.intrepid.com/~robertl/spreadsheet1.html


Thanks for all your help so far!

Garadon
09-29-2004, 03:02 PM
the old table I uploaded should be able to do what you asked for.

But just for you I now made a version 0.3 that can do pretty much any basic math operation and the operation of Math object in js.

yubnob
09-29-2004, 04:04 PM
Thanks again, you CF Addict. I'm actually in pursuit of this because CFGRID isn't customizable enough for my client...

-yub

Garadon
09-29-2004, 04:13 PM
It was fun making.

ijust a warning though it work like excel with the exception you have to type Math infront of the operations

so sqrt(0a) in excel would be Math.sqrt(0a)

and new math to a cell can be set by typing '=' first like in excel.