One More reason to love Dojo
So its not exactly magic -- but if you have to do a "check all" checkbox on a list of things, Dojo's query method works quite nicely:
<script language="javascript">
function fsc_select_all(check_all)
{
checks = dojo.query("[name='forms_to_print[]']").forEach(
function(c){c.checked = check_all.checked});
}
</script>
<form method="post" action="/pdf/printchosen" />
<table>
<tr>
<th>
<input type="checkbox" onchange="fsc_select_all(this)" />
</th>
...
</tr>
<? foreach($this->user_formset_job->get_formset()->forms() as $form): ?>
<tr>
<td><input type="checkbox" name="forms_to_print[]" value="<?= $form->identity() ?>" /></td>
...
</tr>
<? endforeach; ?>
Note - a generic function for any number of sets could be made by passing the checkbox group name as a second parameter. The magic here relies on the ability to run a function over each element of the nodelist returned by the query method of dojo. Each node is passed to the embedded function as the first parameter.
This method depends on the checkboxes being changed to have a common (array format) name so that they can be polled. If for some reason this doesn't work with your model, a (meaningless) class can be assigned to the checkboxes to decorate them in some way that dojo's query method recognizes.

Post new comment