⚠ 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

change_field_type "enum"



khashabawy
  • profile picture
  • Member

Posted 31 May 2012 - 12:44 PM

about the Version 1.2.3

the only way to use enum is



$crud->change_field_type('lang', 'enum', array('en','ru'));


how can i use it like this


$this->crud->change_field_type('lang', 'enum', array( 'English' => 'en', 'Russian' => 'ru' ));


and appears like:


<select>
<option value="en">English</option>
<option value="ru">Russian</option>
</select>



Instead of


<select>
<option value="en">en</option>
<option value="ru">ru</option>
</select>

noskov.biz
  • profile picture
  • Member

Posted 01 June 2012 - 05:26 AM

Hi, don't know if there is the best way, but for the similar situation I have used callback_field function. Something like this:

...
$crud->callback_field('lang', array($this, 'lang_enum'));
...


and lang_enum function:

function lang_enum($value = '', $primary_key = null)
{
if ($value == 'en' or $value == '')
{
return '<select name="lang">
<option value="en" selected="selected">English</option>
<option value="ru">Russian</option>
</select>';
}
elseif ($value == 'ru')
{
return '<select name="lang">
<option value="ru" selected="selected">Russian</option>
<option value="en">English</option>
</select>';
}
}

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

Posted 03 June 2012 - 09:55 AM

Hello [member='khashabawy'] and sorry for the delayed answer. Well for now the new version will support only:

$crud->change_field_type('lang', 'enum', array('en','ru'));

this kind of enum. If you want to make it with another way you have to use the callback_field for this as also [member='noskov.biz'] mentioned.

In the future I will probably have a type named 'dropdown' that it will have similar functionality as enum with the only difference that the values will be like this:

$crud->change_field_type('lang', 'dropdown', array('en' => 'English','ru' => 'Russian','gr' => 'Greek', ....));

khashabawy
  • profile picture
  • Member

Posted 04 June 2012 - 09:57 AM

thanks noskov.biz

thnaks web-johnny
waiting for this type coz iam working in a big project :)

Евгений Румянцев
  • profile picture
  • Member

Posted 01 August 2012 - 18:44 PM

last half hour trying to resolve this problem...
result:

find this

function change_list_value(...)


add after

case 'enum':
$value = $this->character_limiter($value,30,"...");
break;

this

case 'dropdown':
$value =
( !empty( $value ) && isset($field_info->extras[$value]) )
?
$this->character_limiter($field_info->extras[$value], 30, "...")
:
$this->character_limiter($value,30,"...");
break;


then

add after:

protected function get_enum_input(...){...}

this

protected function get_dropdown_input($field_info,$value)
{
$input = "<select id='field-{$field_info->name}' name='{$field_info->name}'>";
if ( $field_info->extras !== false && is_array($field_info->extras) )
{
$options_array = $field_info->extras;
foreach($options_array as $key => $option)
{
$selected = !empty($value) && $value == $key ? "selected='selected'" : '';
$input .= "<option value='$key' $selected >$option</option>";
}
}
else
{
$options_array = explode("','",substr($field_info->db_max_length,1,-1));
foreach($options_array as $option)
{
$selected = !empty($value) && $value == $option ? "selected='selected'" : '';
$input .= "<option value='$option' $selected >$option</option>";
}
}
$input .= "</select>";
return $input;
}


then

find

protected function get_field_input(...)


add after:

case 'enum':
$field_info->input = $this->get_enum_input($field_info,$value);
break;


this


case 'dropdown':
$field_info->input = $this->get_dropdown_input($field_info,$value);
break;


example:

$crud->change_field_type ( 'visible', 'dropdown', array ('yes' => 'Да', 'no'=> 'Нет') );

j-gun
  • profile picture
  • Member

Posted 01 October 2012 - 06:20 AM

hi everyone,

i have ambigous field name in my database, so i use alias name in my query like below:

SELECT name, classes.name AS classname, .....


next step is i want to use classname fieldtype using enum. When i use this code

$crud->change_field_type('classes.name','enum',array('XA','XB'));
or
$crud->change_field_type('classname','enum',array('XA','XB'));


it won't work at all. Any idea?
thanks

j-gun
  • profile picture
  • Member

Posted 02 October 2012 - 00:06 AM

@web johnny,

is grocery CRUD could make an enum field from the relation table? could you show me the way of this?
FYI, i'd build my relation using custom model.

Big thanks for your kindly reply

[quote name='web-johnny' timestamp='1338717357' post='2137']
Hello [member='khashabawy'] and sorry for the delayed answer. Well for now the new version will support only:

$crud->change_field_type('lang', 'enum', array('en','ru'));

this kind of enum. If you want to make it with another way you have to use the callback_field for this as also [member='noskov.biz'] mentioned.

In the future I will probably have a type named 'dropdown' that it will have similar functionality as enum with the only difference that the values will be like this:

$crud->change_field_type('lang', 'dropdown', array('en' => 'English','ru' => 'Russian','gr' => 'Greek', ....));

[/quote]

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

Posted 02 October 2012 - 07:04 AM

[quote name='j-gun' timestamp='1349136410' post='3729']
@web johnny,

is grocery CRUD could make an enum field from the relation table? could you show me the way of this?
FYI, i'd build my relation using custom model.

Big thanks for your kindly reply
[/quote]

You can do this in the new version 1.3.2 . You can use the field_type (this is the method that it is replaced for change_field_type ) and "dropdown" field type as I told you at :

[quote]
In the future I will probably have a type named 'dropdown' that it will have similar functionality as enum with the only difference that the values will be like this:
$crud->change_field_type('lang', 'dropdown', array('en' => 'English','ru' => 'Russian','gr' => 'Greek', ....));

[/quote]

For more you can check http://www.grocerycr...#dropdown-field

There you can add your custom model for this just make sure to have returned an array like this:


array('en' => 'English','ru' => 'Russian','gr' => 'Greek', ....)