我有公司列表,点击每个公司时需要显示该公司的销售人员。
我为此尝试了淘汰赛。下面是脚本,您可以在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);
}
});
});