0

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());
2
  • Do you get any error or it doesn't add extra items ? Commented Jul 9, 2013 at 9:31
  • @Damien it doesn't add extra items Commented Jul 9, 2013 at 9:34

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.