bootstrap-switch icon indicating copy to clipboard operation
bootstrap-switch copied to clipboard

onSwitchChange not firing if indeterminate option is set

Open ipundit opened this issue 10 years ago • 2 comments

To reproduce:

  1. $("[name='my-checkbox']").bootstrapSwitch({ 'indeterminate': true });
  2. Click the switch to toggle from indeterminate to Off
  • Bug: expected onSwitchChange to fire with state = false
  • Note: Toggling the switch from indeterminate to On works as expected; ie: onSwitchChange fires with state = true. The root issue is that this.options.state is initialized to false when this.options.indeterminate = true, so switching the switch to the Off state already has options.state = false, and thus no onSwitchChange event is fired.

To fix in bootstrap-switch.js version 3.3.2, add these lines:

      handleWidth: this.$element.data("handle-width"),
      labelWidth: this.$element.data("label-width"),
      baseClass: this.$element.data("base-class"),
      wrapperClass: this.$element.data("wrapper-class")
    }, options);
    + if (this.options.indeterminate) { 
+   this.options.state = undefined;
+ }

ipundit avatar Jan 24 '16 05:01 ipundit

Thanks that help me :D

Alfredo-Anchondo avatar Jul 27 '16 16:07 Alfredo-Anchondo

Alternative temporary fix approach during intialisation, if the bootstrap-switch.js file is treated as an asset that should not be modified:

$(node).find("input.bootstrap-switch").each(function () {
    var input = $(this);

    input.bootstrapSwitch();

    if (input.prop("indeterminate")) {
        // Temporary fix for state change detection not working if switch is changed from indeterminate to
        // off, cf. https://github.com/Bttstrp/bootstrap-switch/issues/541
        var data = input.data("bootstrap-switch");
        
        if (data) {
            data.options.state = undefined;
        }
    }
});

Works fine with the v4 branch (last commit at the time of this comment was 88cdb642b19cab292fc2977f79a92346e852616c).

takerukoushirou avatar Jan 11 '18 17:01 takerukoushirou