我想在backbone中进行分页。我有一个选择框,当我点击它时,我想让它显示特定数量的数据。
<div class="results right" >
<form>
<label for="results">Results pr. page</label>
<select name="results" id="ShowSelect">
<option value="25" >25</option>
<option value="50" >50</option>
<option value="100" >100</option>
<option value="500" >500</option>
</select>
</form>
</div>模型
defaults : {
id : null,
created : null,
timestamp : null
},
parse : function(response){
response.id = response._id.$oid;
response.created = response.created.$date;
response.timestamp = response.timestamp.$date;
return response;
},这是我的收藏:
pagination : function(perPage, page) {
page = page - 1;
var collection = this;
collection = _(collection.rest(perPage * page));
collection = _(collection.first(perPage));
return collection.map( function(model) {
return model.toJSON();
});
},这是我在一个单独文件中的视图:(未完成)
events : {
"change #messageSelect" : "changedGroup",
},
changedGroup : function(e){
e.preventDefault();
console.log("selector changed");
this.currentPage = 1;
this.selectedGroupId = $("#messageSelect :selected").attr("value");
console.log("id of select is", this.selectedGroupId);
//Resets current groups when changed.
this.currentGroup = this.collection.get(this.selectedGroupId);
},
renderRecentList : function(groupID, pageToLoad) {
var banPage = pageToLoad || this.currentBansPage ;
console.log("Load page", banPage);
this.selectedGroup = this.collection.get(groupID);
}老实说,我不知道如何评估视图中的最大页面。我所做的是,当用户点击选择器时,我得到选择器的id (我想要加载的数据数量,例如100,50等),并创建了另一个函数来评估要加载的最大页面。但是我怎样才能得到数据的数量呢?
var maxPages = Math.ceil(? / this.pageSize);
console.log("max", maxPages);
var pageToLoad = this.currentPage + 1;
console.log("load me page", pageToLoad);
if(pageToLoad <= maxPages) this.renderRecentList(this.selectedGroupId, pageToLoad);
else{
console.log("no more pages to load");
this.setGlobalMessage("no more pages to load", "information");
}
return this;发布于 2013-09-04 12:35:11
我建议你在服务器端这样做。只加载您想要显示的项,如果参数发生更改,则创建一个新请求。
因此,步骤如下:
https://stackoverflow.com/questions/18605238
复制相似问题