See Also: http://www.grocerycr...tion-n-n-field/
[attachment=94:set.png]
grocery CRUD "examples" has a "SET" type field in "film" table.
(`special_features` set('Trailers','Commentaries','Deleted Scenes','Behind the Scenes') DEFAULT NULL)
I made an interface for SET type. (for Add and Edit action)[list]
[*]application/controllers/examples.php
[/list]
In default, the field 'special_features' is in unset_columns.
So, allow to display this field first. (and searching works too)
[list]
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-07 10:40:28.000000000 +0900
@@ -130,7 +130,7 @@
$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->fields('title', 'description', 'actors' , 'category' ,'release_year', 'rental_duration', 'rental_rate', 'length', 'replacement_cost', 'rating', 'special_features');
[*]application/libraries/grocery_crud.php
[/list]
Add function "get_set_input()" to the library (and some peripherals)
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-07 10:38:30.000000000 +0900
@@ -197,6 +197,8 @@
break;
case 'enum':
$field_info->input = $this->get_enum_input($field_info,$value);
+ case 'set':
+ $field_info->input = $this->get_set_input($field_info,$value);
break;
case 'relation':
$field_info->input = $this->get_relation_input($field_info,$value);
@@ -266,6 +268,8 @@
break;
case 'enum':
$value = $this->character_limiter($value,20,"...");
+ case 'set':
+ $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] ));
@@ -358,6 +362,11 @@
$type = 'string';
else
$type = 'enum';
+ case 'set':
+ if($db_type->db_type != 'set')
+ $type = 'string';
+ else
+ $type = 'set';
break;
case '252':
case 'blob':
@@ -1667,6 +1676,28 @@
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}' />";
+ return $input;
+ }
+
protected function get_relation_input($field_info,$value)
{
$this->set_css('assets/grocery_crud/css/jquery_plugins/chosen/chosen.css');