我正在尝试将jqxDropDownList选定值与ko.observable绑定,但我无法弄清楚出了什么问题。它正在使用常规的<select>
代码,但它不能与<div>
元素一起使用,因为我将在下面的HTML中显示。我需要用jqwidgets下拉列表替换<select>
并按照工作代码中的说明进行相应的绑定。
ViewModel:
var viewModel = function(){
var self = this;
self.patternSelectedIndex = ko.observable(0);
self.windowSelectedIndex = ko.observable(0);
self.colorSelectedIndex = ko.observable(0);
self.hardwareSelectedIndex = ko.observable(0);
self.selectedMake = ko.observable();
self.selectedType = ko.observable();
self.makes = [
{id:1, name: 'Northwoods Prestige', dimensions:true},
{id:2, name: 'Forest Bay', dimensions:true},
{id:3, name: 'Timberland', dimensions:true}
];
self.types = [
{id: 1, make:1, name:'Special Reserve 138', patterns:[{file:'FB_Classic', name:'FB Clasic'},{file:'FB_Long', name:'FB Long'},{file:'FB_Flush', name:'FB Flush'}], colors:[{file:'Brown', name:'Brown'},{file:'Oak', name:'Oak'},{file:'Cherry', name:'Cherry'},{file:'Green', name:'Green'}], windows:[{file:'Cascade', name:'Cascade'},{file:'LongPanel', name:'LongPanel'},{file:'Plain', name:'Plain'},{file:'Savanna', name:'Savanna'},{file:'Sunburst', name:'Sunburst'},{file:'Sherwood', name:'Sherwood'}], hardware:[{file:'hardware1', name:'Strap Hinge'},{file:'hardware2', name:'Door Stud'},{file:'hardware3', name:'Lift Handle'}]},
{id: 2, make:1, name:'Special Reserve II', patterns:[{file:'SR_81', name:'SR 81'}], colors:[{file:'Almond', name:'Almond'},{file:'White', name:'White'}], windows:[{file:'Heritage', name:'Colonial'},{file:'Cascade', name:'Cascade'}], hardware:[{file:'hardware1', name:'Strap Hinge'},{file:'hardware3', name:'Lift Handle'}]},
{id: 3, make:2, name:'TF 138', patterns:[{file:'Rec_Carraige', name:'Rec Carraige'}], colors:[{file:'Green', name:'Green'}, {file:'Sepia', name:'Sepia'}], windows:[{file:'Cathedral', name:'Cathedral'},{file:'Cascade', name:'Cascade'}], hardware:[{file:'hardware1', name:'Strap Hinge'},{file:'hardware3', name:'Lift Handle'}]},
{id: 4, make:2, name:'TF II', patterns:[{file:'Raised_Carriage', name:'Raised Carriage'}], colors:[{file:'Almond', name:'Almond'}], windows:[{file:'Cathedral', name:'Cathedral'},{file:'Cascade', name:'Cascade'}], hardware:[{file:'hardware1', name:'Strap Hinge'},{file:'hardware3', name:'Lift Handle'}]},
{id: 5, make:3, name:'RP 25', patterns:[{file:'FB_Classic', name:'FB Classic'}], colors:[{file:'Cherry', name:'Cherry'}], windows:[{file:'Cathedral', name:'Cathedral'},{file:'Cascade', name:'Cascade'}], hardware:[{file:'hardware1', name:'Strap Hinge'},{file:'hardware3', name:'Lift Handle'}]},
{id: 6, make:3, name:'LP 25', patterns:[{file:'FB_Long', name:'FB Long'}], colors:[{file:'Green', name:'Green'}], windows:[{file:'Cathedral', name:'Cathedral'},{file:'Cascade', name:'Cascade'}], hardware:[{file:'hardware1', name:'Strap Hinge'},{file:'hardware3', name:'Lift Handle'}]}
];
self.doorTypes = ko.computed(function(){
return ko.utils.arrayFilter(self.types, function(item){
return item.make === self.selectedMake();
});
});
self.matchingTypes = ko.computed(function () {
return ko.utils.arrayFilter(self.doorTypes(), function (item, index) {
return item.id === self.selectedType();
});
});
};
var model = new viewModel();
ko.applyBindings(model);
(不工作):在波纹管标记中,没有办法将值与selectedMake绑定,如工作示例* 2所示。
<div id="make" data-bind="jqxDropDownList: {source: makes, autoDropDownHeight: true, height: 25, width: 200, displayMember : 'name'}"></div>
2 * HTML(工作):
<select id="make" class="form-control select pull-left" data-bind="options: makes, value: selectedMake, optionsText : 'name', optionsValue : 'id'"></select>
请看一下:jsfiddle.net/euto6vmj
答案 0 :(得分:0)
看起来jqxknockout只支持按数组索引选择项目,因此您需要添加一个抓取当前所选索引的函数。以下两个函数与selectedMake和selectedType observable同步以获取所选索引:
self.selectedMakeIndex = ko.computed(function() {
return self.makes.map(function(e) { return e.id; }).indexOf(self.selectedMake());
});
self.selectedTypeIndex = ko.computed(function() {
return self.types.map(function(e) { return e.id; }).indexOf(self.selectedType());
});
每次设置selectedMake()可观察时,这些都会自动更新。只需在数据绑定中使用它:
<div id="make" data-bind="jqxDropDownList: {source: makes, autoDropDownHeight: true, height: 25, width: 200, displayMember : 'name', selectedIndex: selectedMakeIndex }"></div>
...和类似的类似。
更新了jsfiddle:http://jsfiddle.net/euto6vmj/1/