Last post Jun 27, 2012 01:56 AM by MisterFantastic
Member
30 Points
64 Posts
Jun 12, 2012 07:39 PM|libin.eferns|LINK
Could someone please advice me how can calculte the total number of hours (totalHours) from the function shown below.
<script type="text/javascript"> function timeSheetEntry(name, hours) { this.name = name; this.hours = ko.observable(hours); this.wages = ko.computed(function () { return this.hours()*25; }, this); } function ViewModel() { this.days = [ { dayName: "Monday" }, { dayName: "Tuesday" }, { dayName: "Wednesday" }, { dayName: "Thursday" }, { dayName: "Friday" }, { dayName: "Saturday" }, { dayName: "Sunday" } ]; this.timeSheetEntries = ko.observableArray([ new timeSheetEntry(this.days[0], "", ""), new timeSheetEntry(this.days[1], "","") ]); this.addEntry = function() { this.timeSheetEntries.push(new timeSheetEntry(this.days[0], "", "")); }; this.totalHours = ko.computed(function () { var total = 0; for (var i = 0; i < this.timeSheetEntries().length; i++) total += this.timeSheetEntries.hours; return total; }, this); this.removeEntry = function (entries) { this.timeSheetEntries.remove(entries); }.bind(this); } ko.applyBindings(new ViewModel()); </script>
All-Star
20376 Points
6505 Posts
ASPInsiders
MVP
Jun 12, 2012 09:04 PM|BrockAllen|LINK
Umm, without running this, it's a bit hard to tell, but it looks as if this line:
total += this.timeSheetEntries.hours;
needs to be this instead:
total += this.timeSheetEntries()[i].hours;
636 Points
371 Posts
Jun 27, 2012 01:56 AM|MisterFantastic|LINK
try this .
this.totalHours = ko.computed(function () { var total = 0; for (var i = 0; i < this.timeSheetEntries().length-1; i++) total += this.timeSheetEntries[i].hours; return total; }, this);
are you getting any error ?
Member
30 Points
64 Posts
Basic Knockout Function
Jun 12, 2012 07:39 PM|libin.eferns|LINK
Could someone please advice me how can calculte the total number of hours (totalHours) from the function shown below.
All-Star
20376 Points
6505 Posts
ASPInsiders
MVP
Re: Basic Knockout Function
Jun 12, 2012 09:04 PM|BrockAllen|LINK
Umm, without running this, it's a bit hard to tell, but it looks as if this line:
needs to be this instead:
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
Member
636 Points
371 Posts
Re: Basic Knockout Function
Jun 27, 2012 01:56 AM|MisterFantastic|LINK
try this .
this.totalHours = ko.computed(function () {
var total = 0;
for (var i = 0; i < this.timeSheetEntries().length-1; i++)
total += this.timeSheetEntries[i].hours;
return total;
}, this);
are you getting any error ?