OptimalBPM / angular-schema-form-dynamic-select

AngularStrap and AngularUI dynamic select implementation(static/sync/async/http, filters) for Angular Schema Form
MIT License
55 stars 45 forks source link

uiselectmultiple onChange events #98

Open vincetastic opened 8 years ago

vincetastic commented 8 years ago

onChange events for uiselectmultiple were not firing for me. I think this is a known issue and I've seen recommendations to use $scope.$watch() to instead watch the underlying model. That worked unreliably for me, as it seemed to fire when the list was assigned, but not when there were changes within the list (i.e. actual selections).

So, I implemented a simple workaround that appears to behave like the other onChange event handlers. Thought I'd share here, in case it helps someone else (or there's a better way).

Both changes are in angular-schema-form-dynamic-select.js

1.) For the line that creates the template, starting with:

$templateCache.put("directives/decorators/bootstrap/uiselect/uiselectmultiple.html",...

Change the portion that reads:

on-select=\"$$value$$.push($item.value)\" on-remove=\"$$value$$.splice($$value$$.indexOf($item.value), 1)\"

to this:

on-select=\"valueChanged($item.value,this);$$value$$.push($item.value)\" on-remove=\"valueChanged($item.value,this);$$value$$.splice($$value$$.indexOf($item.value), 1)\"

2.) Then, to the "dynamicSelectController" controller section, add the following function:

$scope.valueChanged = function(modelValue, scope)
    {
        if (scope.form.onChange && typeof scope.form.onChange == 'function') {
            scope.form.onChange(modelValue, scope.form);
        }
    }

Doing this causes any onChange handler added to the form element to fire as expected. Not sure if there's a downside, but haven't seen any thus far.

nicklasb commented 7 years ago

Hi @vincetastic, Could you please make a PR for that? And make you change to the source, too, not just the dist?

nicklasb commented 7 years ago

Any progress?

h123eranga commented 6 years ago

Hello, I tried the solution but it is not working. It fires the $scope.valueChanged method. But its not going to be executed the code inside if condition. Any answer?