Change event on select with knockout binding, how can I know if it is a real change?

前端 未结 11 1875
独厮守ぢ
独厮守ぢ 2021-01-30 08:05

I am building a permissions UI, I have a list of permissions with a select list next to each permission. The permissions are represented by an observable array of objects which

相关标签:
11条回答
  • 2021-01-30 08:43

    I use this custom binding (based on this fiddle by RP Niemeyer, see his answer to this question), which makes sure the numeric value is properly converted from string to number (as suggested by the solution of Michael Best):

    Javascript:

    ko.bindingHandlers.valueAsNumber = {
        init: function (element, valueAccessor, allBindingsAccessor) {
            var observable = valueAccessor(),
                interceptor = ko.computed({
                    read: function () {
                        var val = ko.utils.unwrapObservable(observable);
                        return (observable() ? observable().toString() : observable());
                    },
                    write: function (newValue) {
                        observable(newValue ? parseInt(newValue, 10) : newValue);
                    },
                    owner: this
                });
            ko.applyBindingsToNode(element, { value: interceptor });
        }
    };
    

    Example HTML:

    <select data-bind="valueAsNumber: level, event:{ change: $parent.permissionChanged }">
        <option value="0"></option>
        <option value="1">R</option>
        <option value="2">RW</option>
    </select>
    
    0 讨论(0)
  • 2021-01-30 08:43

    Try this one:

    self.GetHierarchyNodeList = function (data, index, event)
    {
        debugger;
        if (event.type != "change") {
            return;
        }        
    } 
    
    event.type == "change"
    event.type == "load" 
    
    0 讨论(0)
  • 2021-01-30 08:47

    If you are working using Knockout, use the key functionality of observable functionality knockout.
    Use ko.computed() method and do and trigger ajax call within that function.

    0 讨论(0)
  • 2021-01-30 08:49

    This is just a guess, but I think it's happening because level is a number. In that case, the value binding will trigger a change event to update level with the string value. You can fix this, therefore, by making sure level is a string to start with.

    Additionally, the more "Knockout" way of doing this is to not use event handlers, but to use observables and subscriptions. Make level an observable and then add a subscription to it, which will get run whenever level changes.

    0 讨论(0)
  • 2021-01-30 08:51

    Here is a solution that may help with this strange behaviour. I couldn't find a better solution than place a button to manually trigger the change event.

    EDIT: Maybe a custom binding like this could help:

    ko.bindingHandlers.changeSelectValue = {
    
       init: function(element,valueAccessor){
    
            $(element).change(function(){
    
                var value = $(element).val();
    
                if($(element).is(":focus")){
    
                      //Do whatever you want with the new value
                }
    
            });
    
        }
      };
    

    And in your select data-bind attribute add:

    changeSelectValue: yourSelectValue
    
    0 讨论(0)
提交回复
热议问题