Knockoutjs,jquery 移动滑块
我想知道是否有人有一个带有 knockoutjs 的 JQM slider 的工作示例。我有两个问题:
1)将值绑定到可观察值 - 我可以最初绑定它,即设置滑块的“值”,但这在拖动时不会更新底层可观察值 - 我认为这与 JQM 放置有关元素过来代表输入吗?
2)更改min、max、value属性时刷新样式。
这是我所拥有的内容的子集,currentItems 根据选择进行更改:
workloadViewModel.filterValues = ko.dependentObservable(function () {
var tmp = {};
var itms = this.currentItems();
if (itms.length == 0) return;
tmp.max = itms[0].val;
tmp.min = itms[itms.length - 1].val;
tmp.minRange = this.minRange();
return tmp;
}, workloadViewModel);
ko.bindingHandlers.jqmRefreshSlider = {
update: function (element, valueAccessor) {
ko.utils.unwrapObservable(valueAccessor()); //just to create a dependency
try {
$("input[type=range]").slider("refresh");
} catch (error) {
trace("error refreshing slider");
}
}
};
<div id="filters" data-bind="template: {name: 'filterTemplate', data: filterValues}, jqmRefreshSlider: filterValues"></div>
<script id='filterTemplate' type='text/html'>
<p>min: ${minRange}</p>
<p>min: ${min}</p>
<p>max: ${max}</p>
<div>
<input type="range" name="slider_min" id="slider_min" value="${minRange}" min="${min}" max="${max}" />
</div>
</script>
非常感谢您可以给我的任何帮助。
J
I was wondering if someone had a working example of JQM slider with knockoutjs. I have 2 issues:
1) binding the value to an observable - I can bind it initially i.e. set the "value" of the slider, but this does not update the underlying observable when dragged - I think that this is to do with JQM putting elements over the input to represent it?
2) refreshing the style when changing the min, max, value properties.
Here is a subset of what I have, currentItems changes based on a selection:
workloadViewModel.filterValues = ko.dependentObservable(function () {
var tmp = {};
var itms = this.currentItems();
if (itms.length == 0) return;
tmp.max = itms[0].val;
tmp.min = itms[itms.length - 1].val;
tmp.minRange = this.minRange();
return tmp;
}, workloadViewModel);
ko.bindingHandlers.jqmRefreshSlider = {
update: function (element, valueAccessor) {
ko.utils.unwrapObservable(valueAccessor()); //just to create a dependency
try {
$("input[type=range]").slider("refresh");
} catch (error) {
trace("error refreshing slider");
}
}
};
<div id="filters" data-bind="template: {name: 'filterTemplate', data: filterValues}, jqmRefreshSlider: filterValues"></div>
<script id='filterTemplate' type='text/html'>
<p>min: ${minRange}</p>
<p>min: ${min}</p>
<p>max: ${max}</p>
<div>
<input type="range" name="slider_min" id="slider_min" value="${minRange}" min="${min}" max="${max}" />
</div>
</script>
Thanks you very much for any help you can give me.
J
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是绑定到 silver 的工作:
This is working binding to silder:
以上都不适合我,因为我的滑块是动态添加的
none of the above worked for me because my slider was being added dynamically
我对 JQM 滑块咬牙切齿了几天。最后,我能够获得真正的双向绑定到滑块值的可观察值,并让它更新拇指滑块和数字范围输入。以下是我的想法:
以下是它在 HTML 中的使用方式:
我也用此信息更新了我的博客。有关集成 Knockout 和 JQuery Mobile 的更多信息可以在那里找到。
http://www.programico.com/1/post /2012/12/knockoutjs-jquerymobile-slider.html
I was gnashing my teeth for a couple days with the JQM slider. Finally I was able to get true 2-way binding to an observable for the slider's value, and have it update both the thumb slider and the numeric range input. Here is what I came up with:
And here is how it is used in the HTML:
I have updated my Blog with this information as well. More info about integrating Knockout and JQuery Mobile can be found there.
http://www.programico.com/1/post/2012/12/knockoutjs-jquerymobile-slider.html
以下内容对我有用。它还处理动态创建的元素。不过这有点老套。我重用了 Knockoutjs (版本2.1.0): 将布尔值绑定到选择框,用于自动处理将字符串解析为布尔值,反之亦然:
The following worked for me. It also handles dynamically created elements. It's a bit hacky though. I've reused some code from Knockoutjs (version 2.1.0): bind boolean value to select box for autotically handling parsing strings to bools and vice versa: