Discussion icon indicating copy to clipboard operation
Discussion copied to clipboard

using vue.js with select2

Open ghost opened this issue 10 years ago • 18 comments

I am trying to use select2 plugin in vue.js using custom-directive approach.

I am facing the issue of infinite looping. How do I fix this?

https://jsfiddle.net/9Lwo9r8y/

[Vue warn]: You may have an infinite update loop for the watcher with expression: "selected_college_class_id"

ghost avatar Jul 16 '15 11:07 ghost

It looks like the update watcher triggers the change event which would in turn trigger the update watcher? So this would go on forever.

jakeryansmith avatar Jul 16 '15 12:07 jakeryansmith

Yeah, any idea how to fix this?

Or is there any better searchable select plugin to use with vue.js?

ghost avatar Jul 16 '15 12:07 ghost

@yyx990803 ? Please help!

ghost avatar Jul 16 '15 12:07 ghost

I've been messing with this for the last few min, this is what I have so far: https://jsfiddle.net/9Lwo9r8y/

jakeryansmith avatar Jul 16 '15 12:07 jakeryansmith

@jakeryansmith I think you pasted the same fiddle. Please update.

ghost avatar Jul 16 '15 12:07 ghost

Oh sorry. What I had wasn't working that great anyway. Does this have to be a directive? I think the problem is that you set up an onChange listener on the select menu, but also have an updated listener with the directive. I'm not sure both are needed. You might try writing it in a way that you don't use both, idk just an idea. Or maybe don't even use a directive and just fill the select menu using <select option="college_classes" v-model="selected_college_class_id"> Then change the value of selected_college_class_id when the select value changes.

jakeryansmith avatar Jul 16 '15 13:07 jakeryansmith

This seems to work:

Vue.directive('select2', {
    bind: function () {
        var vm = this.vm;
        var key = this.expression;

        var select = $(this.el);

        select.select2();
        select.on('change', function () {
            vm.$set(key, select.val());
        });
    }
});

new Vue({
    el: '#main',
    data: {
        selected_college_class_id: '',
        college_classes: [{
            id: 1,
            name: 'FE'
        }, {
            id: 2,
            name: 'SE'
        }]
    }
});

I just removed the update listener.

jakeryansmith avatar Jul 16 '15 13:07 jakeryansmith

Nope. If you remove update listener, select 2 will not select the appropriate option if the value of model changes. Try selecting the second option and click the 'set class_id as 1' button. It will change the model value but won't update the option.

ghost avatar Jul 16 '15 13:07 ghost

I did this. It works but it is lot of code and not that clean way of doing it.

https://jsfiddle.net/9Lwo9r8y/3/

I hope @yyx990803 suggests the correct way to achieve this!

ghost avatar Jul 16 '15 13:07 ghost

I don't see anything wrong with the changes you made. It actually looks a lot cleaner to me. Using the 'options' directive is the recommended way of filling a select menu. What exactly don't you like about it?

jakeryansmith avatar Jul 16 '15 14:07 jakeryansmith

The fact that I have to manage 2 way data binding for select2 myself. Imagine for a big app, I will need to register so many watchers and so many change event listeners.

ghost avatar Jul 16 '15 17:07 ghost

Gotcha

jakeryansmith avatar Jul 16 '15 17:07 jakeryansmith

Hello.

I checked the first code, and I found that newVal and oldVal were in reverse order.

kaorun343 avatar Jul 20 '15 05:07 kaorun343

I know this is a bit old but I recently had to do this and found a bit easier way of connecting everything. https://jsfiddle.net/reyekdnj/

MacArthurJustin avatar Sep 10 '15 17:09 MacArthurJustin

@MacArthurJustin Your fiddle seems to be broken.

desprit avatar Dec 22 '15 00:12 desprit

@desprit Thanks I don't knwo why the ready option is there I must have not noticed when I saved it. try this one https://jsfiddle.net/reyekdnj/2/

MacArthurJustin avatar Jan 06 '16 15:01 MacArthurJustin

@MacArthurJustin Thank you!

desprit avatar Jan 06 '16 16:01 desprit

I've created a little fiddle for Vuejs 2, Any ideas why the button isn't working? https://jsfiddle.net/ilgala/82ztLqvL/

ilgala avatar Mar 02 '17 10:03 ilgala