可观察的嵌套数组集合在淘汰赛中不起作用
我有公司列表,当点击每个公司时需要显示该公司的销售人员。
我为此尝试了淘汰赛。下面是脚本,您可以在 http://jsfiddle.net/habdulha/gkqeD/29/ 中找到整个脚本
初始公司已列出,但单击销售人员未加载的公司。
<!-- language: lang-js -->
var rootViewModel = function(companies) {
this.companiesModel = ko.observableArray(companies);
}
var companyModel = function(salesPersonModel) {
CompanyId = ko.observable();
Name = ko.observable();
SalesPersonList = ko.observableArray(salesPersonModel);
LoadSalesPerson = function() {
var self = this;
var postData = {
companyId: this.CompanyId()
}
/// ajax to get the emp name based on company id
var data = $.ajax({
type: 'GET',
url: '/echo/json/',
data: postData,
success: function(result) {
/// result is based on companyid
var ajaxReply = {
"empName": "John",
"empName": "John 1"
};
self.SalesPersonList = ko.observableArray(ko.mapping.fromJS(ajaxReply)());
},
error: function(xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
}
}
var salesModel = function() {
empName = ko.observable();
}
var myCompanyModelArray = new Array();
var mySalesModelArray = new Array();
mySalesModelArray[0] = new salesModel();
myCompanyModelArray[0] = new companyModel(mySalesModelArray);
var mainViewModel = new rootViewModel(myCompanyModelArray);
ko.applyBindings(mainViewModel);
$(document).ready(function() {
var data = $.ajax({
type: 'GET',
url: '/echo/json/',
//// get all the companies
success: function(data) {
var ajaxReply = {
{
"CompanyId ": "1",
"Name ": "Comp 1"
}, {
"CompanyId ": "2",
"Name ": "Comp 2"
}
};
residencyViewModel.yearGroupModel(ko.mapping.fromJS(ajaxReply)());
},
error: function(xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
});
I have company list, when clicking on each company need to show the salespersons of that company.
I tried knockout for this. below is script and the whole you can find in http://jsfiddle.net/habdulha/gkqeD/29/
Initial company is listed but clicking on the company the salespersons are not loaded in.
<!-- language: lang-js -->
var rootViewModel = function(companies) {
this.companiesModel = ko.observableArray(companies);
}
var companyModel = function(salesPersonModel) {
CompanyId = ko.observable();
Name = ko.observable();
SalesPersonList = ko.observableArray(salesPersonModel);
LoadSalesPerson = function() {
var self = this;
var postData = {
companyId: this.CompanyId()
}
/// ajax to get the emp name based on company id
var data = $.ajax({
type: 'GET',
url: '/echo/json/',
data: postData,
success: function(result) {
/// result is based on companyid
var ajaxReply = {
"empName": "John",
"empName": "John 1"
};
self.SalesPersonList = ko.observableArray(ko.mapping.fromJS(ajaxReply)());
},
error: function(xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
}
}
var salesModel = function() {
empName = ko.observable();
}
var myCompanyModelArray = new Array();
var mySalesModelArray = new Array();
mySalesModelArray[0] = new salesModel();
myCompanyModelArray[0] = new companyModel(mySalesModelArray);
var mainViewModel = new rootViewModel(myCompanyModelArray);
ko.applyBindings(mainViewModel);
$(document).ready(function() {
var data = $.ajax({
type: 'GET',
url: '/echo/json/',
//// get all the companies
success: function(data) {
var ajaxReply = {
{
"CompanyId ": "1",
"Name ": "Comp 1"
}, {
"CompanyId ": "2",
"Name ": "Comp 2"
}
};
residencyViewModel.yearGroupModel(ko.mapping.fromJS(ajaxReply)());
},
error: function(xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题出在这行代码中:
SalesPersonList
是一个可观察数组,因此当它绑定到您的 UI 时,Konckout 将观察该数组的变化。在上面的代码中,您只需用新实例替换数组。为了完成这项工作,您需要替换绑定到 UI 的数组的内容,而不是替换数组本身......The problem is in this line of code:
SalesPersonList
is an observable array, so when it is bound to your UI, Konckout will observe the array for changes. In the code above you are simply replacing your array with a new instance. To make this work you need to replace the contents of the array bound to your UI rather than replacing the array itself ...我遇到了同样的问题,但 @ColinE 的答案对我不起作用,但这个想法是正确的...
在我的例子中,我收到 JSON 形式的项目列表,并在映射后将它们添加到可观察数组中。
I had the same problem but @ColinE's answer wasn't working for me but the idea was right...
In my case i am receiving the list of items as JSON and adding them to the observable array after mapping.