⚠ In case you've missed it we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This forum is read-only and soon will be archived. ⚠


SET type interface with checkbox

set enum

  • Please log in to reply
2 replies to this topic

#1 Kenta Saito

Kenta Saito

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts

Posted 12 April 2012 - 06:55 AM

Version: grocery_CRUD_v1.2.zip
See Also: http://www.grocerycr...e-for-set-type/

checkbox.png

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
Un-unset_column 'special_features'.
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
Add function "get_set_checkbox_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-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');


#2 php_lover

php_lover

    Advanced Member

  • Members
  • PipPipPip
  • 41 posts

Posted 23 February 2013 - 06:55 AM

it give error CHAMP may be i mistake somewhere



#3 Felipe Matos

Felipe Matos

    Member

  • Members
  • PipPip
  • 11 posts

Posted 08 November 2013 - 06:42 PM

Hi Kenta Saito, 

 

Firstly, thanks for share your code.
 
But, could explain better how edit the library of Grocery Crud, because it was a bit confused.

 

Sorry my english.







Also tagged with one or more of these keywords: set, enum

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users