HI can anyone help me with a function of javascript ... that calculates the sum of all colums at the very last row and also sum of all rows at the very last coloum....
It wiuld be like a 2-d array sum.. But i am new to javascript so totally lost....
I want the total to be updated as soon as any value changes in any cell.....
Please please help me
18 37279 gits 5,390
Expert Mod 4TB
please show what you have done so far ...
kind regards
Yes Frinavale,
Its the same, but there i tried to show my code which i develpoed thn I came to know tht it is not relevaly enough.
I wanted a simple function which is adding value of all prior rows and coloms (not by using property getelementby Tagname as its not supported by IE). And also wanted to know how can I call tht in my actual table other than defining the table ID.
Hi I have tried one, But i dont know wether it fits into your requriement.
JS: Code - <script type="text/javascript">
-
function getColumnCount()
-
{
-
return document.getElementById('myTable').getElementsByTagName('tr')[0].getElementsByTagName('td').length;
-
}
-
-
function getRowCount()
-
{
-
return document.getElementById('myTable').rows.length;
-
}
-
-
function doAdd(ths)
-
{
-
//alert(ths.parentNode.cellIndex);
-
//alert(getColumnCount());
-
var lastCol = getColumnCount()-1;
-
var lastRow = getRowCount()-1;
-
//for Column Sum
-
var table = document.getElementById("myTable");
-
var row = table.rows[ths.parentNode.parentNode.rowIndex];
-
var colSum=0;
-
for(var i=0;i<lastCol;i++)
-
colSum=eval(colSum) + eval(row.cells[i].childNodes[0].value);
-
row.cells[lastCol].innerHTML = colSum;
-
-
//for Row Sum
-
var cIndex = ths.parentNode.cellIndex;
-
//alert(cIndex);
-
var rowSum = 0;
-
for(var i=0;i<lastRow;i++)
-
rowSum = eval(rowSum) + parseInt(table.rows[i].cells[cIndex].childNodes[0].value);
-
table.rows[lastRow].cells[cIndex].innerHTML = rowSum;
-
-
-
//for the final Value in the last row last column
-
var finSum = 0;
-
for(var i=0;i<lastRow;i++)
-
finSum = eval(finSum) + parseInt(table.rows[i].cells[lastCol].innerHTML);
-
for(var i=0;i<lastCol;i++)
-
finSum=eval(finSum) + eval(table.rows[lastRow].cells[i].innerHTML);
-
table.rows[lastRow].cells[lastCol].innerHTML = finSum;
-
}
-
</script>
HTML Code: - <table cellspacing="0" cellpadding="0" width="100%" border="1" id="myTable">
-
<tr>
-
<td><input type="text" value="0" onchange="doAdd(this)"></td>
-
<td><input type="text" value="0" onchange="doAdd(this)"></td>
-
<td><input type="text" value="0" onchange="doAdd(this)"></td>
-
<td><input type="text" value="0" onchange="doAdd(this)"></td>
-
<td><input type="text" value="0" onchange="doAdd(this)"></td>
-
<td>0</td>
-
</tr>
-
<tr>
-
<td><input type="text" value="0" onchange="doAdd(this)"></td>
-
<td><input type="text" value="0" onchange="doAdd(this)"></td>
-
<td><input type="text" value="0" onchange="doAdd(this)"></td>
-
<td><input type="text" value="0" onchange="doAdd(this)"></td>
-
<td><input type="text" value="0" onchange="doAdd(this)"></td>
-
<td>0</td>
-
</tr>
-
<tr>
-
<td><input type="text" value="0" onchange="doAdd(this)"></td>
-
<td><input type="text" value="0" onchange="doAdd(this)"></td>
-
<td><input type="text" value="0" onchange="doAdd(this)"></td>
-
<td><input type="text" value="0" onchange="doAdd(this)"></td>
-
<td><input type="text" value="0" onchange="doAdd(this)"></td>
-
<td>0</td>
-
</tr>
-
<tr>
-
<td><input type="text" value="0" onchange="doAdd(this)"></td>
-
<td><input type="text" value="0" onchange="doAdd(this)"></td>
-
<td><input type="text" value="0" onchange="doAdd(this)"></td>
-
<td><input type="text" value="0" onchange="doAdd(this)"></td>
-
<td><input type="text" value="0" onchange="doAdd(this)"></td>
-
<td>0</td>
-
</tr>
-
<tr>
-
<td>0</td>
-
<td>0</td>
-
<td>0</td>
-
<td>0</td>
-
<td>0</td>
-
<td>0</td>
-
</tr>
-
</table>
Thanks and Regards
Ramanan Kalirajan
@RamananKalirajan
isn’t that overly complicated? all you need is to loop over every <input> element… - // using some slightly modified HTML
-
function sumTotal()
-
{
-
// "all" = ID of the display cell
-
var total = new Summe("all");
-
-
// Object.applyForEach() similar to Array.forEach()
-
document.getElementsByTagName("input").applyForEach(function() { total.add(this.value); });
-
-
total.show();
-
}
-
-
// going MVC
-
function Summe(output_id)
-
{
-
this.out = output_id;
-
this.sum = 0;
-
}
-
-
Summe.prototype.add = function(x)
-
{
-
var y = parseFloat(x);
-
if (!isNaN(y))
-
this.sum += y;
-
}
-
-
Summe.prototype.show = function()
-
{
-
var td = document.getElementById(this.out)
-
if (td)
-
td.innerHTML = this.sum;
-
}
@tarunkhatri
What version of IE are you using?
I use the getElementsByTagName() method all the time. It works fine in IE7 and IE8. I haven't tested it in IE6...but then again IE6 is 2 versions ago and is old enough for me to justify not supporting any more.
This doesn't have to be complicated. All you have to do is grab all of the rows in the table. Retrieve the number of columns in the table and create an array that will hold all of the sums of the columns (where each element in the array corresponds to a column). Loop through the rows and for each row loop through it's cells (columns) adding their values to the value in the array....
the following is a rough outline of what I was talking about...I haven't tested it and can see some obvious ways to make it crash but here you go: -
var theTable = document.getElementById("numbersTable");
-
var rows = theTable.getElementsByTagName("tr");
-
if(rows.length > 0)
-
{
-
var numColumns = rows[0].getElementsByTagName("td").length;
-
var columnSums = new Array(numColumns);
-
var cols = 0;
-
for(cols = 0; cols < columnSums.length; cols++)
-
{
-
columnSums[cols] = Number(0);
-
}
-
var rowIndex = 0;
-
for(rowIndex = 0; rowIndex < rows.length; rowIndex++)
-
{
-
var cells = rows[rowIndex].getElementsByTagName("td");
-
var cellIndex = 0;
-
for(cellIndex = 0; cellIndex < cells.length; cellIndex++)
-
{
-
var cellValue = Number(cells[cellIndex].innerHTML);
-
columnSums[cellIndex] = Number(columnSums[cellIndex]) + cellValue;
-
}
-
}
-
var i;
-
for(i=0; i<columnSums.length; i++)
-
{
-
alert("column: " + i +" sum: " +columnSums[i]);
-
}
-
}else{
-
alert("no rows");
-
}
HI Ramanan , Dormi & Frina,
The code provided by Ramanan did my job.. you all are such gr8 guys .. thanks very very much... Ramanan you made my lide easier.. God bless u all
Ramanan,
Can u pls explain what is the significance of writing parentNode 2 times ths.parentNode.parentNode.rowIndex
In the Code ths.parentNode.parentNode.rowIndex, the "ths" is your input button object what you clicked. It has a parent Table Cell -> first parentNode and Table Cell has the parent Table Row so the second parentNode which will give you the table row object, from that you can fetch the row Index
Thanks and Regards
Ramanan Kalirajan
Ohh yeah... I understand your complete code now.. It was really very very good. Thank you. .But as all my values are comin from a database table .
With onhange event I also wanted OnLoad event. I did it this way
"<input type="text" value="0" size = "2" onchange="doAdd(this)" onload="doAdd(this)">"
But Onload dosnt work here.
Thanks for al your help.
Kind Regards
Tarun
@tarunkhatri
It works, but not the way you expect it. onload basically says that the code is to be executed when the element finished loding. this does not mean that the full table is ready yet, so the script might miss some important details.
PS. to me this onload event seems futile
I understand your point but I am using this even in every <td> with my onchange event. Not at the table level. So every time a td loads with some value frm databse it should add it to the total . Dont knw wht the problem is.
I
first, you’re not using the event on <td>, but <input>
second, why should you calculate the total while the table (and with it the form elements) is still built, ain’t it enough to do that, once the page has finished?
third, that would spare you (rows * columns – 1)* function calls.
* - if you, for example, have a 11 x 14 table and an approximate function run time of 100 ms, you’d save 15 s of time !!!
Ohh.. U are absolutly right Dormi. what a stupid thing I was about to do.
I think I need some different event which works when the complete table is loaded.
To Dormilich,
Yes The way what you have specified for the final td row sum and column sum is an efficient one, when compared to mine. I will update myself Dormilich. Thanks for the suggestion.
Thanks and Regards
Ramanan Kalirajan
@RamananKalirajan
you can use a similar code for the row sum, you only need to use the <tr> as starting point.
unfortunately, there is nothing similar available for columns, except maybe using class names (where you’d have the same code for rows and columns).
I have edited the above script inorder to work with body onload and changing values in columns
Please let me know incase of any issues..
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Marco |
last post by:
Hello there ;)
I'm trying to build a scrip that would open a page that has a table and then
get all the values as variables so I can put them in a MySQL DB.
The problem is that I don't really...
|
by: xzzy |
last post by:
I have hit a wall with not being able to enumerate the items collection in a
table row.
given:
<table runat=server id=table1>
<tr>
<td id=AAA>HowToReferenceThisValue</td>
etc...
|
by: jonniethecodeprince |
last post by:
Hi all,
I was wondering if anyone could help me display the HTML in a javascript that displays a table of data of prototype cinema listings.
This code brings up a Syntax error: Object...
|
by: veegamca |
last post by:
Hi to All,
Any one help me out, to assign values to HTML Table , which can be exported to Excel Sheet
Thanks and Regards
Veera
|
by: since |
last post by:
I figured I would post my solution to the following.
Resizable column tables.
Search and replace values in a table. (IE only)
Scrollable tables.
Sortable tables.
It is based on a lot...
|
by: Jeff |
last post by:
hey
asp.net 3.5
I'm developing a webportal which need to access a webpage of a external
system. The external webpage contain a table which I want to retrieve the
values from.. it is a html...
|
by: James Kimble |
last post by:
Yeah I'm sure this is a stupid question.
I've got a javascript source file with functions (creates a bar graph)
I want to call from inside an html table cell so that the graph
appears in the...
|
by: sanju |
last post by:
Dear All,
I have html table and this table contains 10 Rows and 2 column, I want
every time this HTML page is called by the user to view the rows
Randomly.
How can I do this from JavaScript?
...
|
by: BaseballGraphs |
last post by:
Hello,
I am trying to filter an HTML table by div value using javascript.
Here is my table in HTML:
<table>
<tr>
<td><div align="center">1</div></td>
<td><div align="left"...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |