Using knockout js with jquery ui sliders

后端 未结 4 1696
时光取名叫无心
时光取名叫无心 2020-12-01 14:50

I\'m trying to figure out if knockout js would work nicely for the following problem:

I have multiple sliders that I want to link to textboxes.

When the tex

4条回答
  •  北海茫月
    2020-12-01 15:39

    Here is an example: http://jsfiddle.net/jearles/Dt7Ka/

    I use a custom binding to integrate the jquery-ui slider and use Knockout to capture the inputs and calculate the net amount.

    --

    UI

    Slider Demo

    Savings:
    Spent:
    Net:

    View Model

    ko.bindingHandlers.slider = {
      init: function (element, valueAccessor, allBindingsAccessor) {
        var options = allBindingsAccessor().sliderOptions || {};
        $(element).slider(options);
        $(element).slider({
            "slide": function (event, ui) {
                var observable = valueAccessor();
                observable(ui.value);
            },
            "change": function (event, ui) {
                var observable = valueAccessor();
                observable(ui.value);
            }
        });
        ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
            $(element).slider("destroy");
        });
      },
      update: function (element, valueAccessor) {
        var value = ko.unwrap(valueAccessor());
        if (isNaN(value)) {
            value = 0;
        }
        $(element).slider("value", value);
      }
    };
    
    var ViewModel = function() {
        var self = this;
    
        self.savings = ko.observable(10);
        self.spent = ko.observable(5);
        self.net = ko.computed(function() {
            return self.savings() - self.spent();
        });
    }
    
    ko.applyBindings(new ViewModel());
    

提交回复
热议问题