Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onSwitchChange not firing if indeterminate option is set #541

Open
ipundit opened this issue Jan 24, 2016 · 2 comments
Open

onSwitchChange not firing if indeterminate option is set #541

ipundit opened this issue Jan 24, 2016 · 2 comments

Comments

@ipundit
Copy link

ipundit commented Jan 24, 2016

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;
+ }
@Alfredo-Anchondo
Copy link

Thanks that help me :D

@takerukoushirou
Copy link

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 88cdb64).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants