backbone.js - Issue in pagination in backbone -
i want pagination in backbone. have selectbox , when click on want show sepcific number of data.
<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>
model
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; },
this collection:
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(); }); },
and view in separate file: (uncompleted)
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); }
to honset, not know how evaluate max page in view. did id of selector when user click on (the number of data want load e.g. 100, 50 etc) , created function evaluating max page load. how can number of data
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 load"); this.setglobalmessage("no more pages load", "information"); } return this;
i suggest on server side. load items want show , if parameters change create new request.
so steps:
- create service recives per page , page number parameters , returns corresponding items.
- when user first enter page request default parameters , render items.
- when user changes page or items per page new request , render items came server.
Comments
Post a Comment