Welcome Guest, Not a member yet? Register   Sign In
Updated form_checkbox - pass array and create select all buttons/checkbox!
#1

[eluser]spheroid[/eluser]
I've been passing an array of data to create drop-downs but wanted the same for checkboxes. I've modified the form_checkbox function in system/helpers/form_helper.php to the following. This also contains code to create a set of check all/uncheck all buttons OR a toggle checkbox to do the same, your choice! I hope this helps someone!

Note: Below you'll need to manually add the opening and closing javascript tags where the "[removed]" items appear below.

Code:
if ( ! function_exists('form_checkbox'))
{    
    function form_checkbox($data = '', $options = array(), $selected = array(), $extra = '', $select_all = array())
    {
        $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data.'[]' : $data['name'].'[]'));
        
        $name = ( ! is_array($data) ? $data : $data['name']);
        
        if ( ! is_array($selected))
        {
            $selected = array($selected);
        }
        
        if ($extra != '') $extra = ' '.$extra;
        
        $form = "";
        $tmpform = "";
        $select_all_html = "";
        $select_all_js = "";

        if ($select_all != NULL)
        {
            if (is_array($select_all) AND array_key_exists('type', $select_all))
            {
                $select_all_type = $select_all['type'];
            }
            else
            {
                $select_all_type = "button";
            }
                        
            if (is_array($select_all) AND array_key_exists('location', $select_all))
            {
                $select_all_location = $select_all['location'];
            }
            else
            {
                $select_all_location = "bottom";
            }
            
            switch($select_all_type)
            {
                case 'checkbox':
                    $select_all_html .= '&lt;input type="checkbox" name="'.$name.'_show_all" id="'.$name.'"&gt;&nbsp;Select all '.$name."s<br />";
                    break;
                
                case 'button':
                default:
                    $select_all_html .= '&lt;input type="button" name="'.$name.'_show_all" id="'.$name.'" value="Select all '.$name.'s"&gt;';
            
                    $select_all_html .= '&lt;input type="button" name="'.$name.'_show_all" id="'.$name.'" value="Uncheck all '.$name.'s"&gt;&lt;br />';
                    break;
            }
        }
        
        foreach ($options as $key)
        {
            $key = (string) $key;
        
            $sel = (in_array($key, $selected))?' checked':'';
            
            $tmpform .= '&lt;input '._parse_form_attributes($data, $defaults).' value="'.$key.'"'.$sel."&gt; " . $key . "\n";
        }
        
        $select_all_js .= ('
[removed]

var '.$name.'_checkboxes = document.getElementsByName("'.$name.'[]");
                
function checkboxToggleCheckboxes_'.$name.'('.$name.') {
  if (document.getElementById('.$name.').checked==false) {
    for(var '.$name.'_counter = 0; '.$name.'_counter < '.$name.'_checkboxes.length; '.$name.'_counter++) {
      '.$name.'_checkboxes['.$name.'_counter].checked = false;
    }
  } else {
    for(var '.$name.'_counter = 0; '.$name.'_counter < '.$name.'_checkboxes.length; '.$name.'_counter++) {
      '.$name.'_checkboxes['.$name.'_counter].checked = true;
    }
  }
}

function buttonToggleCheckboxesOn_'.$name.'('.$name.') {
  for(var '.$name.'_counter = 0; '.$name.'_counter < '.$name.'_checkboxes.length; '.$name.'_counter++) {
    '.$name.'_checkboxes['.$name.'_counter].checked = true;
  }
}

function buttonToggleCheckboxesOff_'.$name.'('.$name.') {
  for(var '.$name.'_counter = 0; '.$name.'_counter < '.$name.'_checkboxes.length; '.$name.'_counter++) {
    '.$name.'_checkboxes['.$name.'_counter].checked = false;
  }
}

[removed]
');

        switch($select_all_location)
        {
            case 'top':
                $form .= $select_all_html . "<br />" .$tmpform . $select_all_js;
                break;
                
            case 'bottom':
                $form .= $tmpform . "<br />" . $select_all_html . $select_all_js;
                break;
        }
        
        return $form;        
    }
}

To test, in your controller try:
Code:
$data['skill_array'] = array(
                    'PHP', 'ASP', 'ASP.NET', 'Ruby On Rails'
                    );
                    
                $data['language_array'] = array(
                    'English', 'Spanish', 'French', 'Swahili', 'Kirundi', 'Russian', 'Tagalog', 'American Sign Language'
                    );

And in your view add:
Code:
<tr valign=top>
        <td width=15%>Skills</td>
        <td>
            &lt;?php echo $this->validation->error('skill'); ?&gt;
            &lt;?php echo form_checkbox('skill', $skill_array, ($this->input->post('skill') ? $this->input->post('skill') : NULL), '', array('type'=>'button')); ?&gt;
        </td>
    </tr>
    
    <tr valign=top>
        <td width=15%>Language</td>
        <td>
            &lt;?php echo $this->validation->error('language'); ?&gt;
            &lt;?php echo form_checkbox('language', $language_array, ($this->input->post('language') ? $this->input->post('language') : NULL), '', array('type'=>'checkbox')); ?&gt;
        </td>
    </tr>
#2

[eluser]SamCris[/eluser]
is this an official update? does version 1.7 have this feature included? ill try to test this...
#3

[eluser]spheroid[/eluser]
I put this together for 1.6.3...not sure, about to test 1.7 now.




Theme © iAndrew 2016 - Forum software by © MyBB