kartik-v / yii2-widgets

Collection of useful widgets for Yii Framework 2.0
http://demos.krajee.com/widgets
Other
558 stars 177 forks source link

[SwitchInput] Support for configurable "onSwitchChange" handler #356

Closed Wirone closed 2 years ago

Wirone commented 6 years ago

When I want to hook into SwithInput and use onSwitchChange plugin option:

DetailView::widget([
    'id' => 'my-switch-input',
    'model' => $model,
    'enableEditMode' => true,
    'mode' => DetailView::MODE_EDIT,
    'attributes' => [
        [
            'attribute' => 'myFlag',
            'type' => DetailView::INPUT_SWITCH,
            'pluginOptions' => [
                'onSwitchChange' => 'function () { console.log("I AM SWITCHING!"); }'
            ],
            'format' => 'raw',
            'value' => $model->myFlag,
        ],
    ],
])

I got TypeError: _this.options.onSwitchChange.apply is not a function.

Function configured in widget's pluginOptions is not passed to JS as function, but as string:

window.bootstrapSwitch_bad7d579 = {"onSwitchChange":"function () { console.log(\u0027I AM SWITCHING!\u0027); }"};

I need it to display confirmation box just before changing input's state.

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/55657840-switchinput-support-for-configurable-onswitchchange-handler?utm_campaign=plugin&utm_content=tracker%2F530581&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F530581&utm_medium=issues&utm_source=github).
Wirone commented 6 years ago

As far as I see this is only used in switchChange.bootstrapSwitch event handler to return to previous state if custom handler returns false. I can use it for my needs too, but as a side note it would be good to introduce beforeSwitchChange.bootstrapSwitch event and if handler returns false do not change the state (so switchChange.bootstrapSwitch wouldn't be triggered).