Jump to content


An interface for SET type

enum set

  • Please log in to reply
15 replies to this topic

#1 Kenta Saito

Kenta Saito

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts
  • LocationTokyo JAPAN

Posted 07 April 2012 - 02:05 AM

Version: grocery_CRUD_v1.2.zip
See Also: http://www.grocerycr...tion-n-n-field/

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)
  • application/controllers/examples.php
In default, the field 'special_features' is in unset_columns.
So, allow to display this field first. (and searching works too)

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
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');


Edit Signature TEST

#2 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 1,101 posts
  • LocationLondon

Posted 07 April 2012 - 11:18 AM

Thanks Kenta Saito. Just added to be part of the new version https://github.com/scoumbourdis/grocery-crud/commit/700046bb7ab202b4c8ef81aaee0461aa4be3f23c
Posted Image

#3 Kenta Saito

Kenta Saito

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts
  • LocationTokyo JAPAN

Posted 07 April 2012 - 11:48 AM

My pleasure, Johnny.

Edit Signature TEST

#4 Kenta Saito

Kenta Saito

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts
  • LocationTokyo JAPAN

Posted 11 April 2012 - 06:05 AM

I had a terrible mistake.
I forgot a "brake"s at diff-source above.

GitHub source is fixed about these mistake.
Thank you for care Johnny.

Edit Signature TEST

#5 Kenta Saito

Kenta Saito

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts
  • LocationTokyo JAPAN

Posted 12 April 2012 - 05:53 AM

I found another mistake, and it requires fix for GitHub repository.

Symptom:
1. Open 'Edit'.
2. Correct values are selected in multiple SELECT element.
3. 'Save' *** without *** change.
4. 'Go back to list'.
5. Values will be empty.

Cause:
I forgot value attribute for <input type="hidden" />.


Please apply a change below. I'm sorry for this mistake.

   	 $input .= "</select>";
-   	 $input .= "<input type='hidden' name='{$field_info->name}' />";
+   	 $input .= "<input type='hidden' name='{$field_info->name}' value='$value' />";
		 return $input;


Edit Signature TEST

#6 Kenta Saito

Kenta Saito

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts
  • LocationTokyo JAPAN

Posted 12 April 2012 - 07:00 AM

I made another interface for SET type.
Last time, 'multiple select'. This time, 'checkbox'.

http://www.grocerycr...-with-checkbox/

Edit Signature TEST

#7 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 1,101 posts
  • LocationLondon

Posted 12 April 2012 - 07:41 PM

Kenta Saito I make the changes but I have another problem. It doesn't work correctly at google chrome browser :huh: . When I edit the value and I have actually change the selection, the field appears to be empty I will try to fix it. Thanks for the contribution.
Posted Image

#8 Kenta Saito

Kenta Saito

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts
  • LocationTokyo JAPAN

Posted 13 April 2012 - 01:05 AM

Oh. I will check that with Chrome this week end too.

and, thank you for adding
value attribute for <input type="hidden" />.

Edit Signature TEST

#9 Kenta Saito

Kenta Saito

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts
  • LocationTokyo JAPAN

Posted 13 April 2012 - 03:13 AM

I fixed Chrome problem.
I changed 'for in' into 'for'. (JavaScript)

--- grocery_crud.php.orig	2012-04-13 11:44:38.000000000 +0900
+++ grocery_crud.php	2012-04-13 11:57:13.000000000 +0900
@@ -1699,7 +1699,7 @@
			 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;\">";
+		$input = "<select multiple onchange=\"var v = ''; for (var i = 0; i < this.options.length; i++) 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)

It worked on Firefox Chrome and IE.
Also, 'checkbox' type (I posted above) interface too. (without any change for 'checkbox' type)

P.S.
The thumbnail you attached was not for this, just for
http://www.grocerycr...-dropdown-list/
doesn't it? :P

Edit Signature TEST

#10 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 1,101 posts
  • LocationLondon

Posted 13 April 2012 - 06:32 AM

Sorry man it was really late at night. I hope at least the code is right! :P

Thank you for the fix I will add this as soon as possible.
Posted Image

#11 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 1,101 posts
  • LocationLondon

Posted 13 April 2012 - 06:48 AM

Just added it to master. Thanks.
Posted Image

#12 Kenta Saito

Kenta Saito

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts
  • LocationTokyo JAPAN

Posted 13 April 2012 - 03:01 PM

Thank you for manage, Johnny.
I will learn Git and GitHub.

Edit Signature TEST

#13 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 1,101 posts
  • LocationLondon

Posted 18 April 2012 - 09:40 PM

Kenta Saito I had some problems with your script ( see post http://www.grocerycr...ge__gopid__1314 ) and I just make a new release (grocery CRUD version 1.2.1.1 https://github.com/d...UD_v1.2.1.1.zip ) of grocery CRUD with a new interface of the set type and it will look like this: set-type.png .

Really thank you for the contribution, though I couldn't handled your javascript and I created a new one. I hope you don't mind.
Posted Image

#14 Kenta Saito

Kenta Saito

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts
  • LocationTokyo JAPAN

Posted 19 April 2012 - 12:58 AM

Johnny, I am sorry for my missing testing.
I was tested only in grocery_CRUD_v1.2.zip, but not in newest GitHub repositry.

A new set type interface you made looks good than my multiple select.
Thank you for rapid fix, Johnny.

Edit Signature TEST

#15 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 1,101 posts
  • LocationLondon

Posted 19 April 2012 - 06:17 AM

It OK, it was actually my fault to not tested properly. The quick fix it was that it is very important for me to save properly the data. So as it is fixed everything is alright now :) .
You are of course always welcome to contribute some code as you already helped me a lot.
Posted Image

#16 Kenta Saito

Kenta Saito

    Advanced Member

  • Members
  • PipPipPip
  • 34 posts
  • LocationTokyo JAPAN

Posted 19 April 2012 - 06:25 AM

I was grad to see your reply.
I'll post to this forum again when I'll make some, or get questions :) .

Edit Signature TEST





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

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users