gpt4 book ai didi

带有 knockout 功能的 jQueryUI Spinner 小部件

转载 作者:行者123 更新时间:2023-12-03 22:24:32 24 4
gpt4 key购买 nike

我如何使用jQuery UI Spinner Knockout 绑定(bind)输入中的小部件?

    <tbody data-bind="foreach: orders">
<tr>
<td data-bind="text: Name"></td>
<td><input type="number" style="width: 100px;" data-bind="value: Price" /></td>
<td><input type="number" style="width: 50px;" data-bind="value: VAT" /></td>
<td><input type="number" style="width: 50px;" data-bind="value: Number" /></td>
<td data-bind="text: Final()"></td>
<td><a href="javascript:void(0);" data-bind="click: $root.removeOrder">Remove</a></td>
</tr>
</tbody>

最佳答案

最好的方法是创建自定义绑定(bind)以将spinner绑定(bind)到输入:

ko.bindingHandlers.spinner = {
init: function(element, valueAccessor, allBindingsAccessor) {
//initialize spinner with some optional options
var options = allBindingsAccessor().spinnerOptions || {};
$(element).spinner(options);

//handle the field changing
ko.utils.registerEventHandler(element, "spinchange", function () {
var observable = valueAccessor();
observable($(element).spinner("value"));
});

//handle disposal (if KO removes by the template binding)
ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
$(element).spinner("destroy");
});

},
update: function(element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor()),
current = $(element).spinner("value");

if (value !== current) {
$(element).spinner("value", value);
}
}
};

然后用它代替 value 绑定(bind):

<input
type="number"
style="width: 100px;"
data-bind="spinner: Price, spinnerOptions: { min: 0 } " />

这是工作 fiddle :http://jsfiddle.net/vyshniakov/SwKGb/

关于带有 knockout 功能的 jQueryUI Spinner 小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12929801/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com