I want to bind GuestNumber
field with UI how can I calculate GuestNumber
field?
var GuestLine = function () {
var self = this;
self.FirstName = ko.observable();
self.LastName = ko.observable();
self.Email = ko.observable();
this.GuestNumber = ko.computed(function() {
return this.index;
}, this);
};
var Cart = function () {
// Stores an array of lines, and from these, can work out the grandTotal
var self = this;
self.Guests = ko.observableArray([new GuestLine()]); // Put one line in by default
self.ParticipantFirstName = ko.observable();
self.ParticipantLastName = ko.observable();
self.ParticipantEmail= ko.observable();
// Operations
self.addLine = function () { self.Guests.push(new GuestLine()); };
self.removeLine = function (line) { self.lines.remove(line); };
self.save = function () {
var dataToSave = ko.mapping.toJSON(self);
$.ajax({
type: 'POST',
url: '/API/RSVPHandlerService.ashx',
data: dataToSave,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (r) {
alert(r.d.Name);
alert(r.d.Population);
}
});
};
};
$(function () {
ko.applyBindings(new Cart());
});
You are better using
$index
. Example 2 shows how to do this: http://knockoutjs.com/documentation/foreach-binding.html