473,508 Members | 2,392 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

LONG! Document Object Model - table instead of form?? - slow processing in large form

The below code builds 2 tables 4 rows by 4 cols. All cells have
checkboxes. When checked, the checkboxes in the first column
automatically check the remainder of the check boxes in the same row.

This is working fine for tables of this size. Unfortunately, my app
produces lots of considerably bigger tables and the 'toggle' can take
over a minute to complete!!

I assume this is because for each toggle, the all form elements are
searched through to see if they match.

Is it possible to change this so that a toggle only has to search the
elements of the table it appears in - rather than the whole form?

A document.table_id DOM object would seem to be the answer, but I can
find a reference to a table object.

Your assistance is appreciated!

Edward

CODE:

<form name=taskform action=check_submit.php method=post>

<script language="JavaScript"> <!--

var row_1Monday = new Array("Monday0", "Monday4", "Monday8")
function check_boxes1Monday() {var state = false;if
(document.taskform.checkall_row1Monday.checked) state = true; for
(y=0; y < row_1Monday.length; y++) { for (i = 0; i <
document.taskform.elements.length; i++) { if
(document.taskform.elements[i].name == row_1Monday[y])
document.taskform.elements[i].checked = state; }; }}

var row_2Monday = new Array("Monday1", "Monday5", "Monday9")
function check_boxes2Monday() {var state = false;if
(document.taskform.checkall_row2Monday.checked) state = true; for
(y=0; y < row_2Monday.length; y++) { for (i = 0; i <
document.taskform.elements.length; i++) { if
(document.taskform.elements[i].name == row_2Monday[y])
document.taskform.elements[i].checked = state; }; }}

var row_3Monday = new Array("Monday2", "Monday6", "Monday10")
function check_boxes3Monday() {var state = false;if
(document.taskform.checkall_row3Monday.checked) state = true; for
(y=0; y < row_3Monday.length; y++) { for (i = 0; i <
document.taskform.elements.length; i++) { if
(document.taskform.elements[i].name == row_3Monday[y])
document.taskform.elements[i].checked = state; }; }}

var row_4Monday = new Array("Monday3", "Monday7", "Monday11")
function check_boxes4Monday() {var state = false;if
(document.taskform.checkall_row4Monday.checked) state = true; for
(y=0; y < row_4Monday.length; y++) { for (i = 0; i <
document.taskform.elements.length; i++) { if
(document.taskform.elements[i].name == row_4Monday[y])
document.taskform.elements[i].checked = state; }; }}
--> </script>

<table border=1>
<tr><caption>Monday</caption></tr>
<tr><td>toggle</td><td>0</td><td>1</td><td l>2</td></tr>
<tr><td><input type=checkbox name="checkall_row1Monday"
onClick="check_boxes1Monday(true)"></td>
<td><input type=checkbox name=Monday0></td><td><input type=checkbox
name=Monday4></td>
<td><input type=checkbox name=Monday8></td></tr>
<tr><td><input type=checkbox name="checkall_row2Monday"
onClick="check_boxes2Monday(true)"></td>
<td><input type=checkbox name=Monday1></td><td><input type=checkbox
name=Monday5></td>
<td><input type=checkbox name=Monday9></td></tr>
<tr><td><input type=checkbox name="checkall_row3Monday"
onClick="check_boxes3Monday(true)"></td>
<td><input type=checkbox name=Monday2></td><td><input type=checkbox
name=Monday6></td>
<td><input type=checkbox name=Monday10></td></tr>
<tr><td><input type=checkbox name="checkall_row4Monday"
onClick="check_boxes4Monday(true)"></td>
<td><input type=checkbox name=Monday3></td><td><input type=checkbox
name=Monday7></td>
<td><input type=checkbox name=Monday11></td></tr>
</table>
<script language="JavaScript"> <!--

var row_1Tuesday = new Array("Tuesday0", "Tuesday4", "Tuesday8")
function check_boxes1Tuesday() {var state = false;if
(document.taskform.checkall_row1Tuesday.checked) state = true; for
(y=0; y < row_1Tuesday.length; y++) { for (i = 0; i <
document.taskform.elements.length; i++) { if
(document.taskform.elements[i].name == row_1Tuesday[y])
document.taskform.elements[i].checked = state; }; }}

var row_2Tuesday = new Array("Tuesday1", "Tuesday5", "Tuesday9")
function check_boxes2Tuesday() {var state = false;if
(document.taskform.checkall_row2Tuesday.checked) state = true; for
(y=0; y < row_2Tuesday.length; y++) { for (i = 0; i <
document.taskform.elements.length; i++) { if
(document.taskform.elements[i].name == row_2Tuesday[y])
document.taskform.elements[i].checked = state; }; }}

var row_3Tuesday = new Array("Tuesday2", "Tuesday6", "Tuesday10")
function check_boxes3Tuesday() {var state = false;if
(document.taskform.checkall_row3Tuesday.checked) state = true; for
(y=0; y < row_3Tuesday.length; y++) { for (i = 0; i <
document.taskform.elements.length; i++) { if
(document.taskform.elements[i].name == row_3Tuesday[y])
document.taskform.elements[i].checked = state; }; }}

var row_4Tuesday = new Array("Tuesday3", "Tuesday7", "Tuesday11")
function check_boxes4Tuesday() {var state = false;if
(document.taskform.checkall_row4Tuesday.checked) state = true; for
(y=0; y < row_4Tuesday.length; y++) { for (i = 0; i <
document.taskform.elements.length; i++) { if
(document.taskform.elements[i].name == row_4Tuesday[y])
document.taskform.elements[i].checked = state; }; }}
--> </script>

<table border=1>
<tr><caption>Tuesday</caption></tr>
<tr><td>toggle</td><td>0</td><td>1</td><td l>2</td></tr>
<tr><td><input type=checkbox name="checkall_row1Tuesday"
onClick="check_boxes1Tuesday(true)"></td>
<td><input type=checkbox name=Tuesday0></td><td><input type=checkbox
name=Tuesday4></td>
<td><input type=checkbox name=Tuesday8></td></tr>
<tr><td><input type=checkbox name="checkall_row2Tuesday"
onClick="check_boxes2Tuesday(true)"></td>
<td><input type=checkbox name=Tuesday1></td><td><input type=checkbox
name=Tuesday5></td>
<td><input type=checkbox name=Tuesday9></td></tr>
<tr><td><input type=checkbox name="checkall_row3Tuesday"
onClick="check_boxes3Tuesday(true)"></td>
<td><input type=checkbox name=Tuesday2></td><td><input type=checkbox
name=Tuesday6></td>
<td><input type=checkbox name=Tuesday10></td></tr>
<tr><td><input type=checkbox name="checkall_row4Tuesday"
onClick="check_boxes4Tuesday(true)"></td>
<td><input type=checkbox name=Tuesday3></td><td><input type=checkbox
name=Tuesday7></td>
<td><input type=checkbox name=Tuesday11></td></tr>
</table>
</form>
Jul 23 '05 #1
2 1666
Edward wrote:
The below code builds 2 tables 4 rows by 4 cols. All cells have
checkboxes. When checked, the checkboxes in the first column
automatically check the remainder of the check boxes in the same row.

This is working fine for tables of this size. Unfortunately, my app
produces lots of considerably bigger tables and the 'toggle' can take
over a minute to complete!!

I assume this is because for each toggle, the all form elements are
searched through to see if they match.
Worse than that you are re-resolving the reference to the form's -
elements - collection for each and ever property access used.
Is it possible to change this so that a toggle only has to search the
elements of the table it appears in - rather than the whole form?

<snip>

Yes, but as you have a consistent naming convention there is no need.

Overall, you would be hard pressed to create a worse implementation of
this script than you did. Not only is it horrendously inefficient but it
is also so evidently repetitive that it should have been obvious that
considerable simplification could be achieved by appropriate function
design.

Contrast your efforts with this version, for code size and speed:-

<html>
<head>
<title></title>
<script type="text/javascript">
function toggleCheckRow(inpt, name, startNum){
var formEls = inpt.form.elements;
var state = inpt.checked;
for(var c = (startNum+8);c >= startNum;c -= 4){
formEls[name+c].checked = state;
}
}
</script>
</head>
<body>
<form name=taskform action="check_submit.php" method=post>
<table border=1>
<tr><caption>Monday</caption></tr>
<tr><td>toggle</td><td>0</td><td>1</td><td l>2</td></tr>
<tr>
<td><input type=checkbox name="checkall_row1Monday"
onClick="toggleCheckRow(this, 'Monday', 0);"></td>
<td><input type=checkbox name=Monday0></td>
<td><input type=checkbox name=Monday4></td>
<td><input type=checkbox name=Monday8></td>
</tr>
<tr>
<td><input type=checkbox name="checkall_row2Monday"
onClick="toggleCheckRow(this, 'Monday', 1);"></td>
<td><input type=checkbox name=Monday1></td>
<td><input type=checkbox name=Monday5></td>
<td><input type=checkbox name=Monday9></td>
</tr>
<tr>
<td><input type=checkbox name="checkall_row3Monday"
onClick="toggleCheckRow(this, 'Monday', 2);"></td>
<td><input type=checkbox name=Monday2></td>
<td><input type=checkbox name=Monday6></td>
<td><input type=checkbox name=Monday10></td>
</tr>
<tr>
<td><input type=checkbox name="checkall_row4Monday"
onClick="toggleCheckRow(this, 'Monday', 3);"></td>
<td><input type=checkbox name=Monday3></td>
<td><input type=checkbox name=Monday7></td>
<td><input type=checkbox name=Monday11></td>
</tr>
</table>
<table border=1>
<tr><caption>Tuesday</caption></tr>
<tr><td>toggle</td><td>0</td><td>1</td><td l>2</td></tr>
<tr>
<td><input type=checkbox name="checkall_row1Tuesday"
onClick="toggleCheckRow(this, 'Tuesday', 0);"></td>
<td><input type=checkbox name=Tuesday0></td>
<td><input type=checkbox name=Tuesday4></td>
<td><input type=checkbox name=Tuesday8></td>
</tr>
<tr>
<td><input type=checkbox name="checkall_row2Tuesday"
onClick="toggleCheckRow(this, 'Tuesday', 1);"></td>
<td><input type=checkbox name=Tuesday1></td>
<td><input type=checkbox name=Tuesday5></td>
<td><input type=checkbox name=Tuesday9></td>
</tr>
<tr>
<td><input type=checkbox name="checkall_row3Tuesday"
onClick="toggleCheckRow(this, 'Tuesday', 2);"></td>
<td><input type=checkbox name=Tuesday2></td>
<td><input type=checkbox name=Tuesday6></td>
<td><input type=checkbox name=Tuesday10></td></tr>
<tr>
<td><input type=checkbox name="checkall_row4Tuesday"
onClick="toggleCheckRow(this, 'Tuesday', 3);"></td>
<td><input type=checkbox name=Tuesday3></td>
<td><input type=checkbox name=Tuesday7></td>
<td><input type=checkbox name=Tuesday11></td>
</tr>
</table>
</form>
</body>
</html>

Richard.
Jul 23 '05 #2
Edward wrote:
This is working fine for tables of this size. Unfortunately, my app
produces lots of considerably bigger tables and the 'toggle' can take
over a minute to complete!!

<snip>

I used this approach, it works on my
Win98 IE 6, Netscape 7.1, Firefox 0.8:

- use a single generic function
- use a single 2 dimensional array
- send the 1st dimension of the array to the function
(rename checkboxes starting with 0 to match array base element)
- loop through the 2nd dimension of the array only <<<
- reduce lookup time, use: docelements = document.taskform.elements;

Mike
<form name=taskform action=check_submit.php method=post>
<script language="JavaScript">
var row_Monday = new Array(3);
row_Monday[0] = ["Monday0", "Monday4", "Monday8"];
row_Monday[1] = ["Monday1", "Monday5", "Monday9"];
row_Monday[2] = ["Monday2", "Monday6", "Monday10"];
row_Monday[3] = ["Monday3", "Monday7", "Monday11"];

function check_boxes(rownum) {
var docelements = document.taskform.elements;
var state = docelements['checkall_row'+rownum+'Monday'].checked;
for (var i=0; i<3; i++)
docelements[row_Monday[rownum][i]].checked=state;
}
</script>

<table border=1>
<tr><caption>Monday</caption></tr>
<tr>
<td>toggle</td>
<td>0</td>
<td>1</td>
<td l>2</td>
</tr>
<tr>
<td><input type=checkbox name="checkall_row0Monday"
onClick="check_boxes(0)"></td>
<td><input type=checkbox name=Monday0></td>
<td><input type=checkbox name=Monday4></td>
<td><input type=checkbox name=Monday8></td>
</tr>
<tr>
<td><input type=checkbox name="checkall_row1Monday"
onClick="check_boxes(1)"></td>
<td><input type=checkbox name=Monday1></td>
<td><input type=checkbox name=Monday5></td>
<td><input type=checkbox name=Monday9></td>
</tr>
<tr>
<td><input type=checkbox name="checkall_row2Monday"
onClick="check_boxes(2)"></td>
<td><input type=checkbox name=Monday2></td>
<td><input type=checkbox name=Monday6></td>
<td><input type=checkbox name=Monday10></td>
</tr>
<tr>
<td><input type=checkbox name="checkall_row3Monday"
onClick="check_boxes(3)"></td>
<td><input type=checkbox name=Monday3></td>
<td><input type=checkbox name=Monday7></td>
<td><input type=checkbox name=Monday11></td>
</tr>
</table>

Jul 23 '05 #3

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

Similar topics

8
100228
by: Tim Clacy | last post by:
How is a 64 bit type defined in strict C++? It seems C has support for 'long long' since C99, but not so for C++? Looking through one compiler vendor's standard library headers has clouded the...
7
3581
by: William Payne | last post by:
Hello, I have a variable of type unsigned long. It has a number of bits set (with set I mean they equal one). I need to determine those bits and their position and create new numbers from them. For...
5
4784
by: Mark Shelor | last post by:
Problem: find a portable way to determine whether a compiler supports the "long long" type of C99. I thought I had this one solved with the following code: #include <limits.h> #ifdef...
29
19556
by: Richard A. Huebner | last post by:
Is the unsigned long long primitive data type supported in ANSI standard C? I've tried using it a couple of times in standard C, but to no avail. I'm using both MS VIsual C++ 6, as well as the...
4
8636
by: Lingyun Yang | last post by:
*** post for FREE via your newsreader at post.newsfeed.com *** Dear all, I have a file it's binary data viewed in UltraEdit is EF BB BF 0D 0A 3C ....... I want to read them into a int or long...
9
3921
by: luke | last post by:
Hi everybody, please, can someone explain me this behaviour. I have the following piece of code: long long ll; unsigned int i = 2; ll = -1 * i; printf("%lld\n", ll);
21
2780
by: Charles Sullivan | last post by:
I maintain/enhance some inherited FOSS software in C which has compiler options for quite a few different Unix-like operating systems, many of which I've never even heard of. It would be...
2
7253
by: PengYu.UT | last post by:
Hi, In python, triple quote (""") can be used to quote a paragraph (multiple lines). I'm wondering if there is any equivalent in C++. For the following code, I could write the long string in a...
10
3797
by: ratcharit | last post by:
Currently using cosine function in math.h Currently I get: 1 = cos(1e^-7) Is there another way for cos to return value of high accuracy say: 0.999999 = cos(1e^-7)
15
2717
by: Oliver Graeser | last post by:
I need a >49 bit integer type. tried sizeof(long long), says 8. 8 byte = 64 bit right? but when I try to assign a value with more than 32 bit, it fails. To illustrate: for (i=0; i<64; i++){...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7120
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
7380
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...
0
7494
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
5626
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,...
1
5050
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4706
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
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.