4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
13 * @class Roo.bootstrap.PagingToolbar
15 * A specialized toolbar that is bound to a {@link Roo.data.Store} and provides automatic paging controls.
17 * Create a new PagingToolbar
18 * @param {Object} config The config object
20 Roo.bootstrap.PagingToolbar = function(config)
22 // old args format still supported... - xtype is prefered..
23 // created from xtype...
24 var ds = el.dataSource;
31 Roo.bootstrap.PagingToolbar.superclass.constructor.call(this, config);
37 // supprot items array.
39 Roo.each(items, function(e) {
40 this.add(Roo.factory(e));
45 Roo.extend(Roo.bootstrap.PagingToolbar, Roo.Row, {
47 * @cfg {Roo.data.Store} dataSource
48 * The underlying data store providing the paged data
51 * @cfg {String/HTMLElement/Element} container
52 * container The id or element that will contain the toolbar
55 * @cfg {Boolean} displayInfo
56 * True to display the displayMsg (defaults to false)
59 * @cfg {Number} pageSize
60 * The number of records to display per page (defaults to 20)
64 * @cfg {String} displayMsg
65 * The paging status message to display (defaults to "Displaying {start} - {end} of {total}")
67 displayMsg : 'Displaying {0} - {1} of {2}',
69 * @cfg {String} emptyMsg
70 * The message to display when no records are found (defaults to "No data to display")
72 emptyMsg : 'No data to display',
74 * Customizable piece of the default paging text (defaults to "Page")
77 beforePageText : "Page",
79 * Customizable piece of the default paging text (defaults to "of %0")
82 afterPageText : "of {0}",
84 * Customizable piece of the default paging text (defaults to "First Page")
87 firstText : "First Page",
89 * Customizable piece of the default paging text (defaults to "Previous Page")
92 prevText : "Previous Page",
94 * Customizable piece of the default paging text (defaults to "Next Page")
97 nextText : "Next Page",
99 * Customizable piece of the default paging text (defaults to "Last Page")
102 lastText : "Last Page",
104 * Customizable piece of the default paging text (defaults to "Refresh")
107 refreshText : "Refresh",
110 renderButtons : function(el)
112 Roo.bootstrap.PagingToolbar.superclass.render.call(this, el);
114 this.first = this.addButton({
115 tooltip: this.firstText,
117 html : '<i class="fa fa-backward"></i>',
119 handler: this.onClick.createDelegate(this, ["first"])
121 this.prev = this.addButton({
122 tooltip: this.prevText,
124 html : '<i class="fa fa-step-backward"></i>' + this.prevText,
126 handler: this.onClick.createDelegate(this, ["prev"])
128 //this.addSeparator();
129 this.add(this.beforePageText);
130 this.field = Roo.get(this.addDom({
135 cls: "x-grid-page-number"
137 this.field.on("keydown", this.onPagingKeydown, this);
138 this.field.on("focus", function(){this.dom.select();});
141 this.afterTextEl = this.addText(String.format(this.afterPageText, 1));
142 //this.field.setHeight(18);
143 //this.addSeparator();
144 this.next = this.addButton({
145 tooltip: this.nextText,
147 html : this.nextText + '<i class="fa fa-step-forward"></i>',
149 handler: this.onClick.createDelegate(this, ["next"])
151 this.last = this.addButton({
152 tooltip: this.lastText,
153 html : '<i class="fa fa-forward"></i>',
156 handler: this.onClick.createDelegate(this, ["last"])
158 //this.addSeparator();
159 this.loading = this.addButton({
160 tooltip: this.refreshText,
161 html : '<i class="fa fa-reload"></i>',
163 handler: this.onClick.createDelegate(this, ["refresh"])
166 if(this.displayInfo){
167 this.displayEl = Roo.fly(this.el.dom.firstChild).createChild({cls:'x-paging-info'});
172 updateInfo : function(){
174 var count = this.ds.getCount();
175 var msg = count == 0 ?
179 this.cursor+1, this.cursor+count, this.ds.getTotalCount()
181 this.displayEl.update(msg);
186 onLoad : function(ds, r, o){
187 this.cursor = o.params ? o.params.start : 0;
188 var d = this.getPageData(), ap = d.activePage, ps = d.pages;
190 this.afterTextEl.el.innerHTML = String.format(this.afterPageText, d.pages);
191 this.field.dom.value = ap;
192 this.first.setDisabled(ap == 1);
193 this.prev.setDisabled(ap == 1);
194 this.next.setDisabled(ap == ps);
195 this.last.setDisabled(ap == ps);
196 this.loading.enable();
201 getPageData : function(){
202 var total = this.ds.getTotalCount();
205 activePage : Math.ceil((this.cursor+this.pageSize)/this.pageSize),
206 pages : total < this.pageSize ? 1 : Math.ceil(total/this.pageSize)
211 onLoadError : function(){
212 this.loading.enable();
216 onPagingKeydown : function(e){
218 var d = this.getPageData();
220 var v = this.field.dom.value, pageNum;
221 if(!v || isNaN(pageNum = parseInt(v, 10))){
222 this.field.dom.value = d.activePage;
225 pageNum = Math.min(Math.max(1, pageNum), d.pages) - 1;
226 this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
229 else if(k == e.HOME || (k == e.UP && e.ctrlKey) || (k == e.PAGEUP && e.ctrlKey) || (k == e.RIGHT && e.ctrlKey) || k == e.END || (k == e.DOWN && e.ctrlKey) || (k == e.LEFT && e.ctrlKey) || (k == e.PAGEDOWN && e.ctrlKey))
231 var pageNum = (k == e.HOME || (k == e.DOWN && e.ctrlKey) || (k == e.LEFT && e.ctrlKey) || (k == e.PAGEDOWN && e.ctrlKey)) ? 1 : d.pages;
232 this.field.dom.value = pageNum;
233 this.ds.load({params:{start: (pageNum - 1) * this.pageSize, limit: this.pageSize}});
236 else if(k == e.UP || k == e.RIGHT || k == e.PAGEUP || k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN)
238 var v = this.field.dom.value, pageNum;
239 var increment = (e.shiftKey) ? 10 : 1;
240 if(k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN)
242 if(!v || isNaN(pageNum = parseInt(v, 10))) {
243 this.field.dom.value = d.activePage;
246 else if(parseInt(v, 10) + increment >= 1 & parseInt(v, 10) + increment <= d.pages)
248 this.field.dom.value = parseInt(v, 10) + increment;
249 pageNum = Math.min(Math.max(1, pageNum + increment), d.pages) - 1;
250 this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
257 beforeLoad : function(){
259 this.loading.disable();
264 onClick : function(which){
268 ds.load({params:{start: 0, limit: this.pageSize}});
271 ds.load({params:{start: Math.max(0, this.cursor-this.pageSize), limit: this.pageSize}});
274 ds.load({params:{start: this.cursor+this.pageSize, limit: this.pageSize}});
277 var total = ds.getTotalCount();
278 var extra = total % this.pageSize;
279 var lastStart = extra ? (total - extra) : total-this.pageSize;
280 ds.load({params:{start: lastStart, limit: this.pageSize}});
283 ds.load({params:{start: this.cursor, limit: this.pageSize}});
289 * Unbinds the paging toolbar from the specified {@link Roo.data.Store}
290 * @param {Roo.data.Store} store The data store to unbind
292 unbind : function(ds){
293 ds.un("beforeload", this.beforeLoad, this);
294 ds.un("load", this.onLoad, this);
295 ds.un("loadexception", this.onLoadError, this);
296 ds.un("remove", this.updateInfo, this);
297 ds.un("add", this.updateInfo, this);
302 * Binds the paging toolbar to the specified {@link Roo.data.Store}
303 * @param {Roo.data.Store} store The data store to bind
306 ds.on("beforeload", this.beforeLoad, this);
307 ds.on("load", this.onLoad, this);
308 ds.on("loadexception", this.onLoadError, this);
309 ds.on("remove", this.updateInfo, this);
310 ds.on("add", this.updateInfo, this);