2

Update: I added a jsFiddle of my problem. I also completely rewrote the question as ideas have changed.

I have a page where I am using knockout.js for the layout. I have this working successfully on another page in my application, but I can't get this one to work. My model consists of a parent record and it's child records. My problem is when I try to make copy of the previous row to make it easier for the user to add many similar records. When I type values in the my Department, Project, Job, and Comment fields, the values are blank in the model so when I go to copy those fields, they remain blank.=. Any ideas? I am guessing I am missing something stupid.

 function DayViewModel(day) {
     var self = this;

     self.Date = day.Date;
     self.Time = day.Time;
     self.Hours = ko.observable(day.Hours);
     self.TimesheetCode = ko.observable(day.TimesheetCode);
     self.Department = ko.observable(day.Department);
     self.Project = ko.observable(day.Project);
     self.Job = ko.observable(day.Job);
     self.Comments = ko.observable(day.Comments);
     self.JobIsRequired = ko.observable((day.JobIsRequired == undefined) ? false : day.JobIsRequired);
 }

 function RequestViewModel() {
     var self = this;

     self.DaysRequested = ko.observableArray([new DayViewModel({
         Date: new Date().toString("MM/dd/yyyy"),
         Time: "08:00 AM",
         Hours: 1,
         TimesheetCode: "",
         Department: 0,
         Project: 0,
         Job: 0,
         Comments: ""
     })]);
     self.timesheetCodes // gets data from an external data source

     // Add another day copying the previous day.
     self.addDay = function () {
         var array = self.DaysRequested();
         var previousDay = array[array.length - 1];
         previousDay.Date = Date.parse(previousDay.Date).addDays(1).toString("MM/dd/yyyy");

         var test = ko.utils.unwrapObservable(previousDay.Department);

         var newDay = new DayViewModel({
             Date: previousDay.Date.toString("MM/dd/yyyy"),
             Time: previousDay.Time,
             Hours: ko.utils.unwrapObservable(previousDay.Hours),
             Department: ko.utils.unwrapObservable(previousDay.Department),
             Project: ko.utils.unwrapObservable(previousDay.Project),
             Job: ko.utils.unwrapObservable(previousDay.Job),
             Comments: ko.utils.unwrapObservable(previousDay.Comments),
             TimesheetCode: ko.utils.unwrapObservable(previousDay.TimesheetCode)
         });
         self.DaysRequested.push(newDay);
     }
 }
4

2 回答 2

1

当您需要值绑定时,您正在某些字段上使用文本绑定。例如,部门应该是

<input type="text" class="input-small department-entry" max="5" data-bind="value: Department, uniqueName: true" />
于 2013-05-22T15:24:38.307 回答
0

除了你的 observable 数组,你唯一的 observable 是 TimesheetCode。从 ko文档

observableArray 跟踪数组中的对象,而不是这些对象的状态

我猜您已将日期、时间、小时数等绑定到页面上的输入框,并期望用户在这些输入框中键入的更新会反映在底层 javascript 对象中,但它们不是,因此“未保存淘汰赛值”。

如果是这种情况,那么我建议将日期、时间、小时数等创建为可观察对象。

如果您需要更详细的解决方案,请提供更详细的问题,包括相关标记。

于 2013-05-22T03:49:30.200 回答