I heave this in html tbody data-bind="foreach: contacts"
and Knockout view model
var viewModel = function () {
$this = this;
$this.contacts = ko.observableArray();
$this.nextPage = function () {
$.ajax({
url: "/api/AddressBook",
data: { pagesize: pageSize, currentpage: CPage },
type: "GET"
}).done(function (data) {
var myKoObservableArray = $this.contacts;
myKoObservableArray.push(null);
myKoObservableArray.push(data);
alert(data[0].Name);
});
}
$(document).ready(function () {
$.ajax({
url: "/api/AddressBook",
data: { pagesize: 10,currentpage: 0 },
type: "GET"
}).done(function (data) {
var vm = new viewModel();
vm.contacts(data);
ko.applyBindings(vm);
});
});
First time page is loaded, table is filled from ajax call from $(document).ready. When I call nextPage from UI I make ajax call and alert(data[0].Name) show first element from returned data array. Is is different collection returned from server each time. The problem is that my table in UI not changed after change "contacts" observable array from second(and next) ajax call.
Why are you doing this?
This adds a single element to the end
contacts, namely a single reference to the entire array you received.It sounds like you want to replace the contents of
contactswith the array you got from your ajax call. If that's so, just replace those two statements withIf instead you want to append the new items to the existing ones, than use