NejcZdovc / ng2-select2

Select 2 wraper for angular2
MIT License
123 stars 93 forks source link

The select2('data') method was called on an element that is not using Select2 #114

Closed gaop closed 6 years ago

gaop commented 6 years ago

someone else has the same problem? [version: 1.0.0-beta.11] 1691517326278_ pic_hd

gaop commented 6 years ago

I have found out. The error will be triggered when setting "option.matcher" to do custom filter like this:

    options: Select2Options = <Select2Options> {
        ...,
        matcher: (term: string, text: string, option: any) => {
            // do customer filter
        }
    };

Finally, I have fix the bug using async & await (ES6):

    private async initPlugin() {
        ...
        if(options.matcher) {
            let oldMatcher: any = await this.requireOldMatcher();
            options.matcher = oldMatcher(options.matcher);
            this.element.select2(options);

            if (typeof this.value !== 'undefined') {
                this.setElementValue(this.value);
            }
        }
        ...
    }
    async requireOldMatcher() : Promise<any> {
        return new Promise<any[]>(resolve => {
            jQuery.fn.select2.amd.require(['select2/compat/matcher'], (oldMatcher: any) => {
                resolve(oldMatcher);
            });
        });
    }
gaop commented 6 years ago

https://github.com/gaop/ng2-select2

htrex commented 6 years ago

I'm using the matcher option and seeing the same error message.

NejcZdovc commented 6 years ago

fixed with #115 in version 1.0.0-beta.13