【发布时间】:2016-01-03 01:28:10
【问题描述】:
我有一个引导导航选项卡,我想在选择选项卡时动态显示内容。 Ajax 返回一个结果数组。每个结果都有 Price、Logo、Companyname 和 Covers 数组。每个封面都有 Price、MaxCover、Optional 和 Description。 其余的 html 代码在这里 link 但现在我想返回一个更复杂的类型。
<script type='text/javascript'>
var cover = new
{
Price: ko.observable(),
MaxCover: ko.observable(),
Optional: ko.observable(),
Description: ko.observable(),
}
var result = new
{
Price: ko.observable(),
InsLogo: ko.observable(),
CompanyName: ko.observable(),
Covers: ko.observableArray()
};
var tab = function (Id, name, selected) {
this.Id = Id;
this.name = name;
this.Results = ko.observableArray();
this.isSelected = ko.computed(function () {
return this === selected();
}, this);
}
var ViewModel = function () {
var self = this;
self.selectedTab = ko.observable();
self.tabs = ko.observableArray([
new tab(0, 'Tab1', self.selectedTab),
new tab(1, 'Tab2', self.selectedTab),
new tab(2, 'Tab3', self.selectedTab)
]);
self.selectedTab(self.tabs()[0]);
self.selectedTab.subscribe(function () {
$.ajax({
url: '@Url.Action("GetSection")',
data: { Id: self.selectedTab().Id },
type: 'GET',
success: function (data) {
self.selectedTab().Results(data.Results); //Here I want to fill the results!!!!!!
}
});
});
}
ko.applyBindings(new ViewModel());
【问题讨论】:
-
您遇到的任何问题?据我所知,数据并没有那么复杂。添加一个小提琴,这样我就可以帮助你。欢呼
-
您不需要将
new与对象文字{}一起使用——事实上,这是一个错误。使用{}就足够了。