我做到了
在View initialize函数中
我打电话
this.reloadCustomPages();
这里是它的实现
reloadCustomPages: function(options) {
var self = this;
self.block();
self.customPages.fetch({data: $.param(options)}).always(function () {
self.unblock();
});
}
在服务器端(java spring)我修改了api以接受新的查询字符串
@RequestParam(value = "pageNumber", defaultValue = "1"),
@RequestParam(value = "perPage", defaultValue = "10")
而不是直接返回列表,而是返回有用的分页信息,例如
- 数据库中的项目总数
- 当前页数
- 页面大小
- 以及物品本身
检查这个服务器端 sn-p(我知道它不相关但值得一看)
@RequestMapping(value = "/editor/pages", method = RequestMethod.GET)
public void listPages(@RequestParam(value = "pageNumber", defaultValue = "1") Integer pageNumber,
@RequestParam(value = "perPage", defaultValue = "10") Integer perPage,
HttpServletResponse httpResp) throws IOException {
Long recordsTotal = pageSvc.findTotalNumberOfPages();// select count(*) from table_name
List<PbCustomPage> pages = pageSvc.findPages(pageNumber, perPage);// server side query that gets a pagenated data
BackbonePaginatorResponse response = new BackbonePaginatorResponse();// I created this simple class
response.setTotalCount(recordsTotal);
response.setPageNumber(pageNumber);
response.setPerPage(perPage);
response.setItems(pages);
httpResp.setContentType("application/json");
json.createCustomPageSerializer().addProperties().serialize(response, httpResp.getWriter());// just make your server send that obkect
}
现在返回函数调用,直到服务器获取页面大小为 10 的页面 1
在我的模板中,我有这个
<div class="pagination clear" style="text-align: center;">
<%= customPages.paginationHtml %>
</div>
让我告诉你我是如何填充它的
customPages.paginationHtml = this.generatePagination(customPages);
这是最重要的部分
generatePagination: function (paginationResponse) {
var currentPage = paginationResponse.pageNumber,
lastPage = paginationResponse.totalCount==0?1:Math.ceil(paginationResponse.totalCount/paginationResponse.perPage);
var html = '<ul class="pagination">';
html += '<li class="'+(currentPage == 1?"disabled":"")+'" data-pb-page-number="1"><a href="#" class="first">««</a></li>';
html += '<li class="'+(currentPage == 1?"disabled":"")+'" data-pb-page-number="'+(currentPage==1?1:currentPage-1)+'"><a href="#" class="prev">«</a></li>';
for (var i = 1; i <= lastPage; i++) {
html += '<li class="'+(currentPage == i?"active":"")+'" data-pb-page-number="'+i+'"><a href="#" class="page">'+ i +'</a></li>';
}
html += '<li class="'+(lastPage == currentPage?"disabled":"")+'" data-pb-page-number="'+(currentPage==lastPage?lastPage:currentPage+1)+'"><a href="#" class="next">»</a></li>';
html += '<li class="'+(lastPage == currentPage?"disabled":"")+'" data-pb-page-number="'+(lastPage)+'"><a href="#" class="last">»»</a></li>';
html += '</ul>';
return html;
},
我创建的每个 li 都有data-pb-page-number 供以后使用
还有一件事,如何向其他页面发出新请求
在View initialize函数中
this.el.on('click', 'ul.pagination li:not(.disabled,.active)', this.getCustomPagesPagination);
这里是它的实现
getCustomPagesPagination: function (e) {
e.preventDefault();
var $el = $(e.target).closest("li");
this.reloadCustomPages({pageNumber:$el.data("pb-page-number")})
},
结果是这样的
这就是我解决问题的方法,但问题仍未得到解答