⚠ 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



  • 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...



  • 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.




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

    protected function get_autocomplete_input($field_info,$value)
        $value = !is_string($value) ? '' : str_replace('"',""",$value);

        $extra_attributes = '';
            $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;

now it is:

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

You can use it in you controller like this:

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

... and that's the result:




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