Listen to original select changes
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.
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.
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?
+1 for this
+1
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.
+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
+1 this would make this truly awesome
Posted Bounty for this https://www.bountysource.com/issues/898383-listen-to-original-select-changes
+1
+1 Really Need This To Work...
+1
+1
+1
+1
+1
+1
+1
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
+1
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!