⚠ 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 is an archived forum. ⚠

  •     

profile picture

An interface for SET type



Kenta Saito

Kenta Saito
  • profile picture
  • Member

Posted 07 April 2012 - 02:05 AM

Version: grocery_CRUD_v1.2.zip
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)


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');
[list]
[*]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');

web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 07 April 2012 - 11:18 AM

Thanks [member='Kenta Saito']. Just added to be part of the new version https://github.com/scoumbourdis/grocery-crud/commit/700046bb7ab202b4c8ef81aaee0461aa4be3f23c

Kenta Saito

Kenta Saito
  • profile picture
  • Member

Posted 07 April 2012 - 11:48 AM

My pleasure, Johnny.

Kenta Saito

Kenta Saito
  • profile picture
  • Member

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.

Kenta Saito

Kenta Saito
  • profile picture
  • Member

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;

Kenta Saito

Kenta Saito
  • profile picture
  • Member

Posted 12 April 2012 - 07:00 AM

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

/topic/301-set-type-interface-with-checkbox/

web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 12 April 2012 - 19: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.

Kenta Saito

Kenta Saito
  • profile picture
  • Member

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" />.

Kenta Saito

Kenta Saito
  • profile picture
  • Member

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

web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

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.

web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 13 April 2012 - 06:48 AM

Just added it to master. Thanks.

Kenta Saito

Kenta Saito
  • profile picture
  • Member

Posted 13 April 2012 - 15:01 PM

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

web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 18 April 2012 - 21:40 PM

[member='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: [attachment=112: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.

Kenta Saito

Kenta Saito
  • profile picture
  • Member

Posted 19 April 2012 - 00: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.

web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

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.

Kenta Saito

Kenta Saito
  • profile picture
  • Member

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 :) .