我有以下可观察数组:
self.Profiles =ko.observableArray( ko.utils.arrayMap(initialData, function (profile) {
return {
StartDate : formatDateOnly(profile.StartDate),
EndDate : formatDateOnly(profile.EndDate),
ProfileID :profile.ID,
ProfileName : profile.Name,
ProjectName : profile.ProjectName,
ReadingListID : profile.ReadingListID,
ReadingListName : profile.ReadingListName
};
}));
我想将下拉列表绑定到配置文件以显示配置文件名称,如果下拉列表的值发生更改,那么我想使用新的相应值更新span元素到所选的profileID。
<table id="readingListApplyToProfile" class="fullWidthTable">
<tr>
<td>
Profile:
</td>
<td>
<select id="cboProfile" name="cboProfile" data-bind="options: Profiles, optionsText: 'ProfileName', 'optionsCaption': 'Select profile...', optionsValue:'ProfileID'"></select>
</td>
</tr>
<tr>
<td>
End Date:
</td>
<td>
<span data-bind="'text':EndDate"></span>
</td>
</tr>
</table>
我无法更新跨度,因为span元素不知道下拉值,任何人都可以帮助我。我完全迷失了。
答案 0 :(得分:3)
您缺少的是下拉列表中的值绑定。这是我创造的小提琴。
http://jsfiddle.net/sujesharukil/sBZvb/1/
<select id="cboProfile" name="cboProfile" data-bind="options: Profiles, optionsText: 'ProfileName', 'optionsCaption': 'Select profile...', optionsValue:'ProfileID', value: selectedProfileId ">
这是viewmodel
var myViewModel = function()
{
var self = this;
this.Profiles =ko.observableArray([{
StartDate : '02/01/2012',
EndDate : '01/01/2013',
ProfileID :10,
ProfileName : 'Some Name',
ProjectName : 'Some Project',
ReadingListID : 100,
ReadingListName : 'Some List',
},
{
StartDate : '12/01/2012',
EndDate : '02/27/2013',
ProfileID :12,
ProfileName : 'New Name',
ProjectName : 'New Project',
ReadingListID : 200,
}]);
this.selectedProfileId = ko.observable({}); //this stores the selected id
}
ko.applyBindings(new myViewModel());
希望有所帮助。
SUJ
答案 1 :(得分:2)
我有一个computedObservable的想法,它接受一个ProfileID并输出对应于该ID的Profile,然后将spans绑定到输出对象的各种属性。令人惊讶的是,它奏效了。我正在弄乱Sujesh Arukil的小提琴尝试我的想法,所以这个模型非常相似。
显示多个跨度的工作示例:http://jsfiddle.net/jonhopkins/fgZNQ/2/
模特:
var myViewModel = function()
{
var self = this;
self.Profiles =ko.observableArray([{
StartDate : '02/01/2012',
EndDate : '01/01/2013',
ProfileID :10,
ProfileName : 'Some Name',
ProjectName : 'Some Project',
ReadingListID : 100,
ReadingListName : 'Some List',
},
{
StartDate : '12/01/2012',
EndDate : '02/27/2013',
ProfileID :12,
ProfileName : 'New Name',
ProjectName : 'New Project',
ReadingListID : 200,
}]);
self.selectedProfileId = ko.observable();
self.getProfile = ko.computed({
read: function() {
for (var i = 0; i < self.Profiles().length; i++) {
if (self.Profiles()[i].ProfileID == self.selectedProfileId()) {
return self.Profiles()[i];
}
}
// in case there was no match, output a blank Profile
return [{
StartDate : '',
EndDate : '',
ProfileID : '',
ProfileName : '',
ProjectName : '',
ReadingListID : '',
ReadingListName : ''
}];
},
write: function(value) {
self.selectedProfileId(value);
},
owner: self
});
}
ko.applyBindings(new myViewModel());
编辑: Sujesh提出了一个更好的版本的computedObservable。
self.getProfile = ko.computed(function(){
var profile = ko.utils.arrayFirst(self.Profiles(), function(prof){
return prof.ProfileID == self.selectedProfileId();
});
return profile || {};
});
HTML:
<table id="readingListApplyToProfile" class="fullWidthTable">
<tr>
<td>
Profile:
</td>
<td>
<select id="cboProfile" name="cboProfile" data-bind="options: Profiles, optionsText: 'ProfileName', 'optionsCaption': 'Select profile...', optionsValue:'ProfileID', value: getProfile "></select>
</td>
</tr>
<tr>
<td>
End Date:
</td>
<td>
<span data-bind="text: getProfile().ProfileName"></span>
</td>
</tr>
</table>