分页的第二种方式
var List
viewmodel2 = function() {
//
viewmodel本身。用来防止直接使用this的时候作用域混乱
var self = this;
self.loadingState = ko.observable(true);
self.pageSize = ko.observable(3);
//数据
this.pagedList = ko.observableArray();
//要访问的
页码
this.pageIndex = ko.observable(1);
//总页数
this.pageCount = ko.observable(1);
//
页码数
this.allPages = ko.observableArray();
//当前页
this.currengePage = ko.observable(1);
self.totalCount = ko.observable(1);
this.refresh = function() {
//限制请求
页码在该数据
页码范围内
if (self.pageIndex() < 1)
self.pageIndex(1);
if (self.pageIndex() > self.pageCount()) {
self.pageIndex(self.pageCount());
}
//post异步加载数据
sendAjaxRequest("GET",function (data) {
// 加载新的数据前,先移除原先的数据
self.pagedList.removeAll();
self.allPages.removeAll();
self.totalCount(data.totalCount);
self.pageCount(data.pageCount);
self.loadingState(false);
for (var i = 1; i <= data.pageCount; i++) {
//装填
页码
self.allPages.push({ pageNumber: i });
}
//for...in 语句用于对数组或者对象的
属性进行
循环操作。
//for ... in 循环中的
代码每执行一次,就会对数组的元素或者对象的
属性进行一次操作。
for (var i in data.pagedData) {
//装填数据
self.pagedList.push(data.pagedData[i]);
}
},'GetByPaged',{ 'pageIndex': self.pageIndex() });
};
//请求第一页数据
this.first = function() {
self.pageIndex(1);
self.refresh();
};
//请求
下一页数据
this.next = function() {
self.pageIndex(this.pageIndex() + 1);
self.refresh();
};
//请求先前一页数据
this.prev
IoUs = function() {
self.pageIndex(this.pageIndex() - 1);
self.refresh();
};
//请求最后一页数据
this.last = function() {
self.pageIndex(this.pageCount() - 1);
self.refresh();
};
//
跳转到某页
this.gotoPage = function (data,event) {
self.pageIndex(data);
self.refresh();
};
};
function sendAjaxRequest(httpMethod,callback,url,reqData) {
$.ajax("/api/task" + (url ? "/" + url : ""),{
type: httpMethod,success: callback,data: reqData
});
}
$(document).ready(function () {
var
viewmodel = new List
viewmodel2();
viewmodel.refresh();
if ($('#list2').length)
ko.applyBindings(
viewmodel,$('#list2').get(0));
});
0) {
self.pageIndex(self.pageIndex() - 1);
}
};
self.nextPage = function () {
if (self.pageIndex() < self.maxPageIndex()) {
self.pageIndex(self.pageIndex() + 1);
}
};
self.allPages = ko.dependentObservable(function () {
var pages = [];
for (var i = 0; i <= self.maxPageIndex() ; i++) {
pages.push({ pageNumber: (i + 1) });
}
return pages;
});
self.moveToPage = function (index) {
self.pageIndex(index);
};
};
var list
viewmodel = new List
viewmodel();
function bind
viewmodel() {
sendAjaxRequest("GET",function (data) {
list
viewmodel.loadingState(false);
list
viewmodel.list(data);
list
viewmodel.totalCount(data.length);
if ($('#list').length)
ko.applyBindings(list
viewmodel,$('#list').get(0));
},null,null);
}
$(document).ready(function () {
bind
viewmodel();
});