selectize.js icon indicating copy to clipboard operation
selectize.js copied to clipboard

Listen to original select changes

Open heruan opened this issue 13 years ago • 27 comments

Selectize should be listening on original <select> change events and update accordingly, so if the original value is changed by other plugins Selectize would reflect the change.

P.S. Selectize is awesome! I replaced chosen with selectize using the Bootstrap 3 theme and it looks amazing.

heruan avatar Sep 05 '13 13:09 heruan

Also, it should listen on form reset event and update to the value of the original <select> since on form reset the change event isn't triggered.

heruan avatar Sep 12 '13 21:09 heruan

Maybe it could be related: when the original select changes its options (e.g. plugin, AJAX, etc.) does Selectize have a method to update its contents relflecting the original select new state?

heruan avatar Sep 16 '13 12:09 heruan

+1 for this

MattKetmo avatar Sep 24 '13 16:09 MattKetmo

+1

legomind avatar Oct 09 '13 16:10 legomind

It looks like this issue is effecting two way data binding as seen in AngularJS and Knockout.

Does this test case look okay?

    it('should trigger change when original select changes value.', function(done) {
        var test = setup_test('<select>' +
            '<option value="">Select an option...</option>' +
            '<option value="a">A</option>' +
            '<option value="b">B</option>' +
        '</select>', {});

        var counter = 0;
        test.$select.on('change', function() {
            counter++;
        });

        test.selectize.$input.val('b');

        window.setTimeout(function() {
            expect(test.selectize.$input.val()).to.be.equal('b');
            expect(counter).to.be.equal(1);
            done();
        }, 0);
    });

I tried adding the following code in the setup

    // update selectize if dom is updated directly
    self.$input.on('change', function(e, eventNS) {
        /* don't handle events sent from selectize */
        if (self.eventNS == eventNS) {
            return;
        }

        self.trigger('change', self.$input.val());
    });

But the result was an infinite loop :-\

It would be nice if selectize added its onChange handlers to the original select, then when altering value on selectize it would then update value on original select, triggering change handlers.

troynt avatar Oct 25 '13 22:10 troynt

+1 to make this work painlessly with knockout, this is a great UI component, but too difficult currently to get the data to update properly with knockout

pilavdzic avatar Dec 18 '13 23:12 pilavdzic

+1 this would make this truly awesome

chadedrupt avatar Jan 24 '14 09:01 chadedrupt

Posted Bounty for this https://www.bountysource.com/issues/898383-listen-to-original-select-changes

troynt avatar Jan 24 '14 23:01 troynt

+1

anishpateluk avatar Mar 30 '14 12:03 anishpateluk

+1 Really Need This To Work...

Van-Dame avatar Apr 01 '14 11:04 Van-Dame

+1

elikrief avatar May 13 '14 15:05 elikrief

+1

heldchen avatar May 13 '14 15:05 heldchen

+1

brendo avatar May 28 '14 10:05 brendo

+1

isDipesh avatar Jun 06 '14 16:06 isDipesh

+1

konclave avatar Jul 03 '14 14:07 konclave

+1

bperrine avatar Jul 14 '14 17:07 bperrine

+1

gregeatworld avatar Jul 14 '14 19:07 gregeatworld

I take back my +1 for this request. Initially I thought hooking into the original select events was essential to creating a knockout binding handler. However, after getting to know the selectize api more I have taken a different approach to creating a ko binding handler.

I've knocked up a quick gist of my ko binding handler, it works really well selectize. I'll post up some documentation at some point in the future.

https://gist.github.com/anishpateluk/64ff5ae385b770532322

anishpateluk avatar Jul 17 '14 18:07 anishpateluk

+1

scratscratte avatar Oct 18 '14 20:10 scratscratte

It honestly amazes me that this feature hasn't been added yet; it seems pretty core. In the mean time, though, I've created a simple jQuery method that will update the selectize form based on the original select element.

$.fn.updateSelectize = function() {

   // Convert the HTML options into a usable array/object for selectize
    var options = [];
    this.find('option').each( function( index ) {
      var option = {
        text:$(  this ).text(),
        value: $( this ).val()
      }
      options.push( option );
    } );

    // Get the Selectize instance
    selectize = this[0].selectize;
    selectize.clearOptions();
    selectize.load(function(callback){
      callback(options);
    });

    // Select the first option
    $.each( selectize.options, function( index, prop ) {
      first_option = index;
      return false;
    });
    selectize.addItem(first_option);

    // Return the jQuery object for chaining
    return this;
};

With a little bit more work that could be built into a change listener. Hopefully that'll help someone!

petemolinero avatar Sep 04 '15 17:09 petemolinero