Reputation: 4933
i need to add projectLine array to values which is return from json object. i tried following method but it didn't work.
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());
Upvotes: 0
Views: 603
Reputation: 8987
You need to use the push function of the observableArray.
for (var i = 0; i < arg.d.WorkedProjects.length ; i++) {
// seems strange : did you add an selectedProject function to the self.projectLine observable array ?
self.projectLine.selectedProject(arg.d.WorkedProjects[i].ProjectCode);
self.projectLine.Hours(arg.d.WorkedProjects[i].Hours);
var newProjectsWorked =new projectsWorked();
// setup your new projects worked
newProjectsWorked.hours(arg.d.WorkedProjects[i].Hours);
// then add it to the list
this.projectLine.push(newProjectsWorked);
}
I hope it helps.
Upvotes: 1
Reputation: 7159
Your self.projectLine
is a ko.observableArray()
. You're trying to access it via self.projectLine.SomeProperty
. That's not going to work.
You can get to the contents like this: self.projectLine()[index].Hours(...)
etc.
Upvotes: 0