See Also: http://www.grocerycr...e-for-set-type/

I made another interface for SET type.
Last time, 'multiple select'. This time, 'checkbox'.
For detail, follow 'See Also' link above.
- application/controllers/examples.php
Then change 'special_features's type from 'set' to 'set_checkbox'.
diff -ru --strip-trailing-cr CodeIgniter_2.1.0.orig/application/controllers/examples.php CodeIgniter_2.1.0/application/controllers/examples.php --- CodeIgniter_2.1.0.orig/application/controllers/examples.php 2012-03-15 21:27:32.000000000 +0900 +++ CodeIgniter_2.1.0/application/controllers/examples.php 2012-04-12 15:40:19.000000000 +0900 @@ -130,7 +130,8 @@ $crud->set_table('film'); $crud->set_relation_n_n('actors', 'film_actor', 'actor', 'film_id', 'actor_id', 'fullname','priority'); $crud->set_relation_n_n('category', 'film_category', 'category', 'film_id', 'category_id', 'name'); - $crud->unset_columns('special_features','description'); + $crud->unset_columns('description'); + $crud->change_field_type('special_features', 'set_checkbox'); $crud->fields('title', 'description', 'actors' , 'category' ,'release_year', 'rental_duration', 'rental_rate', 'length', 'replacement_cost', 'rating', 'special_features');
- application/libraries/grocery_crud.php
diff -ru --strip-trailing-cr CodeIgniter_2.1.0.orig/application/libraries/grocery_crud.php CodeIgniter_2.1.0/application/libraries/grocery_crud.php --- CodeIgniter_2.1.0.orig/application/libraries/grocery_crud.php 2012-03-15 21:27:32.000000000 +0900 +++ CodeIgniter_2.1.0/application/libraries/grocery_crud.php 2012-04-12 15:36:00.000000000 +0900 @@ -198,6 +198,12 @@ case 'enum': $field_info->input = $this->get_enum_input($field_info,$value); break; + case 'set': + $field_info->input = $this->get_set_input($field_info,$value); + break; + case 'set_checkbox': + $field_info->input = $this->get_set_checkbox_input($field_info,$value); + break; case 'relation': $field_info->input = $this->get_relation_input($field_info,$value); break; @@ -267,6 +273,12 @@ case 'enum': $value = $this->character_limiter($value,20,"..."); break; + case 'set': + $value = $this->character_limiter($value,20,"..."); + break; + case 'set_checkbox': + $value = $this->character_limiter($value,20,"..."); + break; case 'relation_n_n': $value = implode(', ' ,$this->get_relation_n_n_selection_array( $value, $this->relation_n_n[$field_info->name] )); $value = $this->character_limiter($value,30,"..."); @@ -359,6 +371,18 @@ else $type = 'enum'; break; + case 'set': + if($db_type->db_type != 'set') + $type = 'string'; + else + $type = 'set'; + break; + case 'set_checkbox': + if($db_type->db_type != 'set') + $type = 'string'; + else + $type = 'set_checkbox'; + break; case '252': case 'blob': case 'text': @@ -1667,6 +1691,49 @@ return $input; } + protected function get_set_input($field_info,$value) + { + $selected_options = array(); + if ( ! empty($value)) + { + foreach (explode(',', $value) as $v) $selected_options[$v] = TRUE; + } + + $input = "<select multiple onchange=\"var v = ''; for (var i in this.options) if (this.options[i].selected) v += (v == '' ? '' : ',') + this.options[i].value; this.nextSibling.value = v;\">"; + + $options_array = explode("','",substr($field_info->db_max_length,1,-1)); + foreach($options_array as $option) + { + $selected = !empty($value) && isset($selected_options[$option]) ? "selected='selected'" : ''; + $input .= "<option value='$option' $selected >$option</option>"; + } + + $input .= "</select>"; + $input .= "<input type='hidden' name='{$field_info->name}' value='$value' />"; + return $input; + } + + protected function get_set_checkbox_input($field_info,$value) + { + $selected_options = array(); + if ( ! empty($value)) + { + foreach (explode(',', $value) as $v) $selected_options[$v] = TRUE; + } + + $input = '<div>'; + $options_array = explode("','",substr($field_info->db_max_length,1,-1)); + foreach($options_array as $option) + { + $selected = !empty($value) && isset($selected_options[$option]) ? "checked='checked'" : ''; + $input .= "<input type=\"checkbox\" value=\"$option\" $selected onchange=\"var v = ''; var c = this.parentNode.childNodes; for (var i in c) if (c[i].value !== undefined && c[i].checked) v += (v == '' ? '' : ',') + c[i].value; this.parentNode.nextSibling.value = v;\" />$option "; + } + + $input .= "</div>"; + $input .= "<input type='hidden' name='{$field_info->name}' value='$value' />"; + return $input; + } + protected function get_relation_input($field_info,$value) { $this->set_css('assets/grocery_crud/css/jquery_plugins/chosen/chosen.css');