我需要将 projectLine 数组添加到从 json 对象返回的值中。我尝试了以下方法,但没有奏效。
function ViewModel() {
var self = this;
this.CheckIn = ko.observable();
this.CheckOut = ko.observable();
this.Lunch = ko.observable();
this.dateEntered = ko.observable();
this.isDateValidate = ko.observable();
this.Rest = ko.observable();
this.projectLine = ko.observableArray([new projectsWorked()]);
// problem occurs in this function
this.displayData = function () {
var date = self.dateEntered();
$.ajax({
type: "POST",
url: 'TimeRecord.aspx/ReturnProjectDetail',
data: "{'date':" + JSON.stringify(date) + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (arg) {
self.CheckIn(arg.d.CheckIn.Hours + ":" + arg.d.CheckIn.Minutes);
self.CheckOut(arg.d.CheckOut.Hours + ":" + arg.d.CheckOut.Minutes);
self.Lunch(arg.d.Lunch.Hours + ":" + arg.d.Lunch.Minutes);
self.Rest(arg.d.Rest.Hours + ":" + arg.d.Rest.Minutes);
// problem is here.
for (var i = 0; i < arg.d.WorkedProjects.length ; i++) {
self.projectLine.selectedProject(arg.d.WorkedProjects[i].ProjectCode);
self.projectLine.Hours(arg.d.WorkedProjects[i].Hours);
}
},
error: function (arg) {
alert('fail ');
}
});
};
};
function projectsWorked() {
var self = this;
this.projectEnable = ko.observable(false);
this.Projects = ko.observableArray();
this.hours = ko.observable();
this.projectWork=ko.computed(function () {
// return parseFloat($('#txthour').val());
return this.hours();
}, this);
this.selectedProject = ko.observable();
this.GetProjects = function () {
$.ajax({
type: "POST",
url: 'TimeRecord.aspx/ReturnComplexType',
data: {},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (arg) {
for (var i = 0; i < arg.d.length ; i++) {
self.Projects.push(arg.d[i].ProjectCode);
}
},
error: function (arg) {
}
});
};
};
ko.applyBindings(new ViewModel());