0

我需要将 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());
4

2 回答 2

1

您需要使用 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);    
}

我希望它有所帮助。

于 2013-07-09T09:43:42.470 回答
0

self.projectLine的是一个ko.observableArray(). 您正在尝试通过self.projectLine.SomeProperty. 那是行不通的。

你可以得到这样的内容:self.projectLine()[index].Hours(...)等。

于 2013-07-09T09:32:51.583 回答