⚠ 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

Combobox



maxmax
  • profile picture
  • Member

Posted 03 April 2015 - 13:58 PM

I was looking to something like dojo combobox, but field_type enum, set, dropdown and multiselect do not allow to add values apart from the ones on the list of options. Did I miss something ?

If there is anything similar on Grocery crud I think, I'll implement it using this nice article...

:ph34r:


maxmax
  • profile picture
  • Member

Posted 10 April 2015 - 11:58 AM

Well after all I've decided to adopt an autocomplete function...

That's my solution, but i still need to write some code on /libraries/grocery_CRUD.php.

I don't like to edit this, i hope there's a way to make a plugin.

 

However

 

1st) I added this simple function at the end of the file:

    protected function get_autocomplete_input($field_info,$value)
    {
        $this->set_css($this->default_css_path.'/ui/simple/'.grocery_CRUD::JQUERY_UI_CSS);
        $this->set_js_lib($this->default_javascript_path.'/jquery_plugins/ui/'.grocery_CRUD::JQUERY_UI_JS);
        $value = !is_string($value) ? '' : str_replace('"',""",$value);

        $extra_attributes = '';
        if(!empty($field_info->db_max_length))
            $extra_attributes .= "maxlength='{$field_info->db_max_length}'";
        $input = "<input id='field-{$field_info->name}' class='form-control' name='{$field_info->name}' type='text' value=\"$value\" $extra_attributes />";

        $input .= '<script> $(function() {  var availableTags = [';
        
        $options = $field_info->extras;

        for($i = 0; $i < count($options); ++$i) {
            $input .= '"'.$options[$i].'"';    
            if ($i != count($options)-1) $input .= ', ';    
        }        
        
        $input .= "]; $( '#field-{$field_info->name}' ).autocomplete({ source: availableTags, minLength: 0 }); });</script>";
 
        return $input;
    }

2nd) Then, in the function get_field_input (line 216) I added 'autocomplete' to the array $types_array.

 

3rd) Then I edited the function function change_list_value($field_info, $value = null) (line 257)

 

the original code was:

            case 'dropdown':
                $value = array_key_exists($value,$field_info->extras) ? $field_info->extras[$value] : $value;
            break;

now it is:

            case 'autocomplete':
            case 'dropdown':
                $value = array_key_exists($value,$field_info->extras) ? $field_info->extras[$value] : $value;
            break;

You can use it in you controller like this:

$crud->field_type('nome', 'autocomplete', array('0'=>'Pippo','1'=>'Pluto'));

... and that's the result:

 

Immagine.png

 

I hope you enjoy it ;)

 

 

PS: i used minLength: 0 in the function get_autocomplete_input, because I have few values to choose, if you have many, you can use minLength: 1