PDA

View Full Version : [DHTML] Check/Uncheck columns script



jscheuer1
09-10-2006, 08:37 AM
1) CODE TITLE: Check/Uncheck columns script

2) AUTHOR NAME/NOTES: jscheuer1

3) DESCRIPTION: I wrote this code in response to a post in the javascript section:

http://www.dynamicdrive.com/forums/showthread.php?t=12978

It could be very useful for anyone faced with a similar situation. It will initialize checkboxes in table columns so that a 'check all' box at the top of the column can check all boxes and uncheck all boxes in the column. Once checked, if one box in the column is deselected, the 'check all' box will be deselected too. If it happens that all boxes in the column get selected except for the 'check all' box, it will automatically get selected. Degrades well in non-javascript enabled browsers if optional styles and configuration are used.

4) URL TO CODE: http://home.comcast.net/~jscheuer1/side/table_check.htm

or, ATTACHED BELOW (see #3 in guidelines below):

mwinter
09-10-2006, 09:41 PM
Degrades well in non-javascript enabled browsers if optional styles and configuration are used.

But it errors out in older browsers due to the lack of feature detection. You've gone over-the-top with regular expression usage where simple string comparison (and no toLowerCase/toUpperCase) would do, and you'll invoke IE's memory leak due to the DOM-involved closures you create.

Another issue is the handling of the "check all" checkbox, both in that it always exists (the control and label should be generated), and that you hide the cell contents. The latter case isn't helped because the original request didn't really state what exactly was needed so you had to guess, but it doesn't seem reasonable for the cell contents to be mutually exclusive. The original contents must somehow be useful otherwise they shouldn't exist in the first place.

Most of the time, there will only be a single set of checkboxes that the user will want to control as a group. As a result, the controls for that group can be placed anywhere (within reason), rather than being limited to somewhere in the table (or whatever container is in use). It could simply be a checkbox next to the column heading, or in combination with an explicit label.

I don't mind suggesting an alternative, but a more specific (and realistic) specification is needed.

Mike

jscheuer1
09-14-2006, 07:59 AM
I really wasn't all that concerned with legacy browsers when I wrote the code (focusing on minimal markup hooks like id's and classes, mainly) but, I usually am. So, point well taken. I think I am just beginning to really get the hang of regular expressions, having struggled along with them off and on for quite some time. I did get carried away. The idea of the alternate text was just so that an empty or display none tr wouldn't have to be used as, they look odd. This alternate text could be used even as part of the initialized content though, that whole bit is at the designer's discretion (configurable in the script and/or by leaving 'displayed under all circumstances text' with the td as the immediate parent element). I'm not attached to this being exclusively my code or even my code at all. Feel free to write an update or a completely different version using your ideas. If I feel motivated to do so I may update this one myself as well, or we could as I say, work it as a joint credit.

mwinter
09-17-2006, 09:25 PM
The idea of the alternate text was just so that an empty or display none tr wouldn't have to be used as, they look odd.

I can appreciate that, but the columns of a table should normally have headings anyway, so the checkboxes could be generated dynamically and inserted there. Similarly, an extra row could be created using the DOM at run-time. It would look stranger, I should think, if the alternate text was just some filler (as it was in your example).



I'm not attached to this being exclusively my code or even my code at all. Feel free to write an update or a completely different version using your ideas.

As I wrote previously, I'd prefer a better idea of what the intention really is, first.

Consider that a set of checkboxes that will have a common control should have something in common. In markup, that relationship is usually established by giving them the same control name. If that were the case, there's no need to traverse the table at all, but simply obtain a collection of those elements from the form. Life becomes a whole lot easier.

Mike