Skip to content

Commit

Permalink
Issue #11826: Use jQuery to handle "select all" checkbox
Browse files Browse the repository at this point in the history
The "select all" checkbox on view_all_bug_page currently uses inline
JavaScript via the onclick event to select all the checkboxes on the
page.

This patch removes the inline JavaScript and instead replaces it with
equivalent (but more portable/reusable) jQuery code.

To implement any "select all" checkbox in the future, all one has to do
is:

1) Ensure the "Select all" checkbox is within the same form as the
checkboxes to check.

2) Ensure that the "Select all" checkbox includes the class "check_all".

3) Name the target checkboxes as "{field_name}[]" e.g., name="bugs[]" or
name="users[]".

4) Name the "Select all" checkbox as "{field_name}_all" e.g.,
name="bugs_all" or name="users_all".
  • Loading branch information
davidhicks committed Aug 19, 2010
1 parent 17a2bc4 commit 111d9b7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
16 changes: 5 additions & 11 deletions javascript/dev/common.js
Expand Up @@ -79,6 +79,11 @@ $(document).ready( function() {

$('input.autofocus:first, select.autofocus:first, textarea.autofocus:first').focus();

$('input[type=checkbox].check_all').click(function() {
var matchingName = $(this).attr('name').replace(/_all$/, '');
$(this).closest('form').find('input[type=checkbox][name=' + matchingName + '\[\]]').attr('checked', this.checked);
});

var stopwatch = {
timerID: null,
elapsedTime: 0,
Expand Down Expand Up @@ -236,17 +241,6 @@ function ToggleDiv( p_div ) {
SetCookie( "collapse_settings", t_cookie );
}

/* Check checkboxes */
function checkall( p_formname, p_state) {
var t_elements = (eval("document." + p_formname + ".elements"));

for (var i = 0; i < t_elements.length; i++) {
if(t_elements[i].type == 'checkbox') {
t_elements[i].checked = p_state;
}
}
}

function setDisplay(idTag, state)
{
if(!document.getElementById(idTag)) alert('SetDisplay(): id '+idTag+' is empty');
Expand Down
2 changes: 1 addition & 1 deletion view_all_inc.php
Expand Up @@ -220,7 +220,7 @@ function write_bug_rows ( $p_rows )
<span class="floatleft">
<?php
if ( $g_checkboxes_exist && ON == config_get( 'use_javascript' ) ) {
echo "<input type=\"checkbox\" name=\"all_bugs\" value=\"all\" onclick=\"checkall('bug_action', this.form.all_bugs.checked)\" /><span class=\"small\">" . lang_get( 'select_all' ) . '</span>';
echo '<input type="checkbox" name="bug_arr_all" value="all" class="check_all" /><span class="small">' . lang_get( 'select_all' ) . '</span>';
}

if ( $g_checkboxes_exist ) {
Expand Down

0 comments on commit 111d9b7

Please sign in to comment.