gpt4 book ai didi

c# - knockout -获取分页数据,初始加载

转载 作者:行者123 更新时间:2023-12-03 10:38:06 25 4
gpt4 key购买 nike

我正在使用KnockoutJS和Twitter Bootstrap构建一个简单的分页表格 View 。现在,除了页面加载时无法加载初始数据集之外,其他所有工作都可以正常进行。查看我的 View 模型,您可以看到通过RESTful API在客户存储库中“浏览”时加载了正确的数据。

如何获取要加载的初始数据?

<script>

var ListViewModel = function() {
var self = this;

window.viewModel = self;

//Properties
self.list = ko.observableArray();
self.pageSize = ko.observable(20);
self.pageIndex = ko.observable(0);
self.itemCount = ko.observable();

self.maxPageIndex = ko.dependentObservable(function () {
return Math.ceil(self.itemCount() / self.pageSize()) - 1;
});

self.allPages = ko.dependentObservable(function () {
var pages = [];
for (i = 0; i <= self.maxPageIndex() ; i++) {
pages.push({
pageNumber: (i + 1)
});
}
return pages;
});


//navigation Functions
self.previousPage = function () {
if (self.pageIndex() > 0) {
self.pageIndex(self.pageIndex() - 1);
}
self.getItems();
};

self.nextPage = function () {
if (self.pageIndex() < self.maxPageIndex()) {
self.pageIndex(self.pageIndex() + 1);
}
self.getItems();
};

self.moveToPage = function (index) {
self.pageIndex(index);
self.getItems();
};

//API interactions
self.getItems = function () {

$.ajax({
url: '/api/customer/?pageNumber='+self.pageIndex()+"&pageSize="+self.pageSize(),
async: false,
dataType: 'json',
success: function (result) {
self.list(result);
}
});
};

self.getTotalItemCount = function() {
//TODO: Add call to fetch total number of customers in the database and set it to self.itemCount property.
};


};

$(document).ready(function() {
ko.applyBindings(new ListViewModel());
});

</script>

最佳答案

更改您的起始代码

$(document).ready(function() {
var vm = new ListViewModel()
vm.getItems()
ko.applyBindings(vm)
});

关于c# - knockout -获取分页数据,初始加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16888782/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com