views:

363

answers:

2

I'm trying to use a select all checkbox to select a column of checkboxes.

So I gave all the ones I want checked off at once the same name ABoxesElement so that I can easily check them all on in javascript.

However when I use firebug in firefox I can see that the checkboxes did not keep the name I gave them but pre-pended the component name and appended an auto-incrementing number to my checkboxes.

Since I cannot use regex in getElementByName how can I go about setting the same value in multiple checkboxes. (ID is used for something else).. here is my code:

Select All Checkbox

<dmf:checkbox
name="ABoxes"
onclick = 'selectAllACheckBoxes'
id="allABoxes"  
runatclient="true"/>

Example of one of the checkboxes that I want checked

<dmf:checkbox  
name="ABoxesElement" 
id="<%=...%>" 
runatclient="true"/>

**Example of Javascript **

function selectAllCheckBoxes(source) {
var checked = source.checked 
var cbName = source.name + 'Element';  
var col = document.getElementsByName(cbName);

for (var i=0; i<col.length;i++) 
{
col[i].checked = checked;
col[i].disabled = !checked;          
} 
}

When the page renders however I notice that the name of the individual checkboxes is not ABoxesElement but something like component_nameAboxesElement_2 so the script won't work unless getElementsByName can accept regexes.

Any ideas on how to go around this?

the tld for checkbox has another attribute

  <attribute>
     <name>datafield</name>
     <required>false</required>
     <rtexprvalue>true</rtexprvalue>   </attribute>

can use that to store the "name" and then access the element through it?

EDIT HTML OUTPUT for an individual checkbox

<input type="hidden" value="true" id="a1ON_hidden" name="mform_content_AboxesElement_hidden_2"/>
    <input type="checkbox" onclick="setKeys(event);safeCall(selectAllCheckBoxes,this);" value="" title="Click here" id="a1ON" name="form_content_AboxesElement_2"/>

EDIT 2 HTML OUTPUT for a selectall checkbox

<input type="hidden" name="mform_content_ABoxes_hidden_1" id="allABoxes_hidden" value="true"/>
<input type="checkbox" name="mform_content_ABoxes_1" id="marketsAll" title="Click to select" value="" onclick="setKeys(event);safeCall(selectAllCheckBoxes,this);"/>
A: 

The datafield attribute is used to provide an object property(such as r_object_id,r_version_label etc.) as data to the control, which wont help much in this case.

A not so perfect solution could be to set runatclient="false" in your dmf tag and reference the checkboxes by different names in the corresponding component; then instead of javascript you can use a java method in the component class to get a handle on the checkboxes and modify their values. The WDK generated name attribute for the checkboxes depends on the component name and probably their order in the JSP, so I am not sure if its a good idea to assume that WDK will always generate the same names for the checkboxes. However processing in the component class will always be slower than a javascript function.

Shantanu
can I just use regular html input checkboxes instead of the dmf if I don't really use them elsewhere? this way it will not change the name
Ayrad
AFAIK you should be able to use plain HTML controls in your jsp, but I am not too sure on how to get a handle on their values if you are going to need the checkbox values in your component class.
Shantanu
+1  A: 

Here's a kickoff:

function selectAllCheckboxes(checkboxElement) {
    var allFormElements = checkboxElement.form.elements;
    for (var i = 0; i < allFormElements.length; i++) {
        var formElement = allFormElements[i];
        if (formElement.name.indexOf('mform_content_ABoxes_') == 0) { // Check if its name starts with particular string.
            formElement.checked = checkboxElement.checked;
        }
    }
}

You could eventually pass the 'mform_content_ABoxes_' in as another function argument, or even extract it from checkboxElement.name if you know the patterns used in generating the name attribute.

BalusC
ended up using something like this but it feels inefficient to loop through all the checkboxes for each of my categories but I guess I can't do much since the name is automatically named and auto-incremented.. it's still fast enough though so that's ok
Ayrad
It's only inefficient if you've more than 10,000 fields or so (which would maybe last one second) You can't get it faster.
BalusC