473,472 Members | 2,155 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Generating totals of grid on load

Given the code below how can I get the totals to update when the form
loads not just when a cell is changed? I tried a call to
updateColumn(el[i]) in initTable but this gives a "cell has no value"
error.

Thanks in advance.

---------

<form action="" name="ArrayOfInputs">
<table>
<tr>
<td><input type="text" name="D-0-0" value="1"></td>
<td><input type="text" name="D-1-0" value="1"></td>
<td><input type="text" name="D-2-0" value="1"></td>
</tr>
<tr>
<td><input type="text" name="D-0-1" value="2"></td>
<td><input type="text" name="D-1-1" value="2"></td>
<td><input type="text" name="D-2-1" value="2"></td>
</tr>
<tr>
<td><input type="text" name="D-0-2" value="0"></td>
<td><input type="text" name="D-1-2" value="0"></td>
<td><input type="text" name="D-2-2" value="0"></td>
</tr>
<tr>
<td><input type="text" name="D-0-t" value="0" readonly></td>
<td><input type="text" name="D-1-t" value="0" readonly></td>
<td><input type="text" name="D-2-t" value="0" readonly></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="reset"></td>
<td><input type="submit"></td>
</tr>
</table>
</form>
<script type="text/javascript">
function initTable(){
// Attach the event to all cells in the table
// except the ones with 't' in their name
var f = document.forms['ArrayOfInputs'];
var el = f.elements;
for (var i=0, eLen=el.length; i<eLen; i++){
if (el[i].type.toLowerCase() == 'text'
&& !el[i].name.match('t') { //don't attach to totals
el[i].onchange = updateColumn;
//updateColumn(el[i]);
}
}

}

function updateColumn(e){
// Get the cell that fired the event
var e = e || window.event;
var cell = e.target || e.srcElement;
var f = cell.form;
var n = cell.name.split('-');
var j = 0;
var t;
var sum = 0;
while ( (t = f.elements[n[0]+'-'+n[1]+'-'+j]) ){

// Check content, if OK add, else prompt
if ( testNum(t.value )) {
sum -= -t.value; // Using '-' forces t to be a number
} else {
while (!testNum(t.value)) {
t.value = prompt('Entries must be a number.'
+ '\nPlease enter a new value: ');
}
}
j++;
}
f.elements[n[0]+'-'+n[1]+'-'+'t'].value = sum;

}

function testNum(x) {
return /^(\+|-)?\d+(\.)?(\d+)?$/.test(x);
}

window.onload = initTable;

</script>
_______
Trade your DVDs, movies and CDs for Free!
http://www.dvdtrades.net
Nov 23 '05 #1
3 1522
> how can I get the totals to update when the form loads not just when a cell is
changed?


You are passing the form element to updateColumn but updateColumn is
treating it as an event object. Make update column set cell to e if e
is a form element.

Nov 23 '05 #2
Mike the Canadian wrote:
Given the code below how can I get the totals to update when the form
loads not just when a cell is changed? I tried a call to
updateColumn(el[i]) in initTable but this gives a "cell has no value"
error.

[...]
<script type="text/javascript">
function initTable(){
// Attach the event to all cells in the table
// except the ones with 't' in their name
var f = document.forms['ArrayOfInputs'];
var el = f.elements;
for (var i=0, eLen=el.length; i<eLen; i++){
if (el[i].type.toLowerCase() == 'text'
&& !el[i].name.match('t') { //don't attach to totals
You have a syntax error here ----^

&& !el[i].name.match('t') ) { //don't attach to totals

el[i].onchange = updateColumn;
Change this to:

el[i].onchange = function(){ updateColumn(this); };

//updateColumn(el[i]);
Uncomment the above line:

updateColumn(el[i]);
When you call the function from the onclick, because you have added a
function reference dynamically, the 'event' parameter is passed (in Geko
browsers) and nothing is passed IE, hence your use of target/srcElement.

When you call the function here, you pass a reference to the cell, but
the function isn't expecting that. The simplest solution is to attach
an anonymouse function to the onclick that passes 'this' (see above).

I have asked previously about calling the same function from events
attached dynamically and from other functions, the advice was that the
above is probably the best way. An alternative is to look at what is
passed to the function to determine how it was called and go from there,
but it's a bit messy.

}
}

}

function updateColumn(e){
// Get the cell that fired the event
var e = e || window.event;
var cell = e.target || e.srcElement;
Because you are now passing a reference to the cell, you can replace the
above with:

function updateColumn( cell ){
var f = cell.form;
var n = cell.name.split('-');


You split the name into an array, then keep concatenating it back into a
single string again. Is this still required?

[...]

--
Rob
Nov 23 '05 #3
Rob, thank you for clarifying... halfAsleep == halfAnswers.

Nov 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: kk | last post by:
I have 2 issues, please help ! 1st issue : -------------------------------------------------------- Loading Grid ------------ 1) creating new rows in a datatable and adding data a array...
8
by: Tony Williams | last post by:
I have a database that hold s figures for each quarter. I want to create a query whereby the user can ask for the running total of the data upto the quarter they enter into a prompt. The database...
1
by: Steve | last post by:
Hi I have a db with 2 tables that I want to bind to a grid depending on a selection in a Dropdownlist Also I want to be able to select a row from the gris to fill some textboxes. The databases...
4
by: Gary | last post by:
Is there any way to get FooterText to show at the top of the grid instead of the bottom? We have hundreds of pages of grids that are almost always too tall to show on a page. Users are constantly...
3
by: mark | last post by:
im keeping running totals of certain columns in a datagrid, id like to put these totals in the footer - how do i do this ? or how can i put a label into the footer ? (im displaying results using...
4
by: blackhawk | last post by:
I need to build a web page that has to potentially display a large amount of data in two grids on the same page. The HTML file with all of the formatting is about 7MB in size. This is too large...
4
by: Coleen | last post by:
Hi All :-) Cor & David, Thank you for your help, sorry I did not reply before now, but for some reason my Newsgroup Reader (Outlook) does not show your last posts. The last one I got from Cor...
6
by: Aravind | last post by:
I have a query, like the following: (Assumption: an ID will not have an entry in both "Meal" and "Daily" on the same date; it can only have an entry in either "meal" or "Daily" per date) ...
0
by: newsaboutgod | last post by:
I am loading a dataset from a XML file. I want to allow the user to display data from the dataset in a grid. Depending on which button they click i want to load the grid with a subset of data...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.