EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

Can't get EasyAdminAutocompleteType to work in a collection

Open mikeelltt opened this issue 9 years ago • 9 comments

I have a form type that utilises the EasyAdminAutocompleteType which gets rendered as a plain select instead of select2.

The "Artist" field should be autocomplete:

screen shot 2017-02-11 at 13 26 56

Here's the configuration:

# /app/config/config.yml
easy_admin:
    entities:
        Event:
            class: ApiBundle\Entity\Event
            form:
                fields:
                    - property: 'performances'
                      type: 'collection'
                      by_reference: false
                      type_options:
                        entry_type: 'AppBundle\Form\PerformanceType'
                        by_reference: false

and the form type:

// /src/AppBundle/Form/PerformanceType.php
// ...
class PerformanceType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('artist', EasyAdminAutocompleteType::class, ['class' => Artist::class]);
        $builder->add('songs_description', TextareaType::class);
    }
    // ...
}

I tried fixing it listening for "easyadmin.collection.item-added" event and applying select2 manually on that select element but autocompletion doesn't fetch values in this case.

mikeelltt avatar Feb 11 '17 10:02 mikeelltt

Used versions: EasyAdmin: 1.16.5 Symfony: 3.1.10

mikeelltt avatar Feb 11 '17 11:02 mikeelltt

Same issue 🤓

baptadn avatar Mar 07 '17 15:03 baptadn

@mikeelltt, @shinework could you show us the code where you're initialize select2 manually on that select?

yceruto avatar Mar 07 '17 15:03 yceruto

Hi @yceruto, I used this code:


$('.field-collection').on('easyadmin.collection.item-added', function() {
    var $parent = $(this);
    var $field = $parent.find('.field-easyadmin_autocomplete');

    if (!$field.length) {
        return;
    }

    var $input = $field.find('select').select2({
        theme: 'bootstrap'
    });
});

mikeelltt avatar Mar 07 '17 18:03 mikeelltt

You can use createAutoCompleteFields() function instead? this solve your problem?

$('.field-collection').on('easyadmin.collection.item-added', function() {
    createAutoCompleteFields();
});

I know that it does not make the best approach, but we would change this function for better reusability/runtime initialization of dynamic autocomplete fields (i.e. createAutoCompleteFields($input)).

yceruto avatar Mar 07 '17 19:03 yceruto

@yceruto Thanks it works ✅

baptadn avatar Mar 09 '17 10:03 baptadn

Thanks @yceruto, works correctly.

mikeelltt avatar Mar 09 '17 10:03 mikeelltt

https://github.com/javiereguiluz/EasyAdminBundle/pull/1536 improving this initialization...

yceruto avatar Mar 09 '17 14:03 yceruto

@yceruto, sounds good! Thank you 😀

mikeelltt avatar Mar 09 '17 15:03 mikeelltt