【发布时间】:2016-12-13 19:23:31
【问题描述】:
所以我正在构建这个图表构建 ui。您是否可以从数据库中选择字段来构建图表。并且图表有一个刷新间隔下拉菜单。
在我尝试从保存的“视图”加载值之前,一切正常 当我添加对 RefreshInterval 变量的订阅时,我可以看到该值已设置。
但它会立即重置为我的选项数组的 0'd 索引。
[html]
<div class="wrapper wrapper-content" id="charts">
<select id="interval" data-bind="options: RefreshIntervals,
optionsText: 'Value',
value: RefreshInterval,
chosen: {
disable_search_threshold: 0,
width:'175px',
no_results_text: 'No results!',
placeholder_text_single: 'Select an interval',
search_contains: true,
}">
</select>
</div>
[javascript]
function ViewModel() {
var self = this;
this.RefreshIntervals = ko.observableArray([
new Interval(0, "Never"), new Interval(10, "seconds"), new Interval(1, "minutes"), new Interval(5, "minutes")
]);
this.RefreshInterval = ko.observable(new Interval(5, "minutes"));
};
var Interval = (function () {
function Interval(length, measurement) {
var _this = this;
this.Length = 0;
this.Measurement = "";
this.Value = "";
this.TimeoutValue = 0;
this.GetTimeoutValue = function () {
switch (_this.Measurement) {
case "seconds":
return _this.Length * 1000;
case "minutes":
return _this.Length * 60000;
default:
return 0;
}
};
this.Length = length;
this.Measurement = measurement;
if (length == 0 || measurement == "Never") {
this.Value = "Never";
}
else {
this.Value = this.Length + " " + this.Measurement;
}
this.TimeoutValue = this.GetTimeoutValue();
}
return Interval;
}());
ko.bindingHandlers.chosen =
{
init: function (element, valueAccessor, allBindings) {
$(element).chosen(valueAccessor());
// trigger chosen:updated event when the bound value or options changes
$.each('value|selectedOptions|options'.split("|"), function (i, e) {
var bv = allBindings.get(e);
if (ko.isObservable(bv)) {
bv.subscribe(function () {
$(element).trigger('chosen:updated');
});
}
});
}
};
var vm = new ViewModel();
ko.applyBindings(vm, document.getElementById("charts"));
我这里有个小提琴:Fiddle
我有 3 个需要填充的下拉框。 我希望该值是实际元素,而不是一个值字段,然后我需要与选项列表中的实际对象相关联,因为选项列表可能会发生变化。
有什么办法可以解决这个问题吗?
【问题讨论】:
标签: javascript knockout.js jquery-chosen