Knockout-jQueryUI

Fork me on GitHub

Spinner.html

Enhance a text input for entering numeric values, with up/down buttons and arrow key handling.

Parameters

For each of the widget's options the binding has a corresponding option with the same name. These can be set to static values, or bound either to normal or to observable properties of the viewmodel.

For each of the widget's events the binding has a corresponding option with the same name. These can be set to functions. When invoked the value of the this keyword evaluates to the viewmodel.

widget

Should be bound to one of the viewmodel's writeable observables. The binding will write a jQuery object containing the binding's DOM element into it. The primary use for this option is to invoke the widget's methods on the element.

value [two-way binding]

The spinner's value. If the valueUpdate binding is also specified on the element, then the view model's property is updated right after the value of the spinner's input is changed. If the valueUpdate is absent, then the view model's property is updated when the spinner's input is blurred.

Example

HTML

<input data-bind="spinner: { value: value }" />
<p>
  <button data-bind="click: pageUp">+50</button>
  <button data-bind="click: pageDown">-50</button>
</p>

JavaScript

var ViewModel = function () {
  this.value = ko.observable(0);
};
ViewModel.prototype.pageUp = function() {
  this.value(this.value() + 50);
};
ViewModel.prototype.pageDown = function() {
  this.value(this.value() - 50);
};

Result