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
14 * @extends Roo.bootstrap.NavSimplebar
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
19 * @param {Roo.data.Store} store
21 Roo.bootstrap.PagingToolbar = function(config)
23 // old args format still supported... - xtype is prefered..
24 // created from xtype...
26 var ds = config.dataSource;
31 if (this.store && !ds) {
34 this.store= Roo.factory(this.store, Roo.data);
36 ds.xmodule = this.xmodule || false;
39 this.toolbarItems = [];
41 this.toolbarItems = config.items;
45 Roo.bootstrap.PagingToolbar.superclass.constructor.call(this, config);
53 this.navgroup = new Roo.bootstrap.NavGroup({ cls: 'pagination' });
57 Roo.extend(Roo.bootstrap.PagingToolbar, Roo.bootstrap.NavSimplebar, {
59 * @cfg {Roo.data.Store} dataSource
60 * The underlying data store providing the paged data
63 * @cfg {String/HTMLElement/Element} container
64 * container The id or element that will contain the toolbar
67 * @cfg {Boolean} displayInfo
68 * True to display the displayMsg (defaults to false)
71 * @cfg {Number} pageSize
72 * The number of records to display per page (defaults to 20)
76 * @cfg {String} displayMsg
77 * The paging status message to display (defaults to "Displaying {start} - {end} of {total}")
79 displayMsg : 'Displaying {0} - {1} of {2}',
81 * @cfg {String} emptyMsg
82 * The message to display when no records are found (defaults to "No data to display")
84 emptyMsg : 'No data to display',
86 * Customizable piece of the default paging text (defaults to "Page")
89 beforePageText : "Page",
91 * Customizable piece of the default paging text (defaults to "of %0")
94 afterPageText : "of {0}",
96 * Customizable piece of the default paging text (defaults to "First Page")
99 firstText : "First Page",
101 * Customizable piece of the default paging text (defaults to "Previous Page")
104 prevText : "Previous Page",
106 * Customizable piece of the default paging text (defaults to "Next Page")
109 nextText : "Next Page",
111 * Customizable piece of the default paging text (defaults to "Last Page")
114 lastText : "Last Page",
116 * Customizable piece of the default paging text (defaults to "Refresh")
119 refreshText : "Refresh",
123 onRender : function(ct, position)
125 Roo.bootstrap.PagingToolbar.superclass.onRender.call(this, ct, position);
126 this.navgroup.parentId = this.id;
127 this.navgroup.onRender(this.el, null);
128 // add the buttons to the navgroup
130 if(this.displayInfo){
131 Roo.log(this.el.select('ul.navbar-nav',true).first());
132 this.el.select('ul.navbar-nav',true).first().createChild({cls:'x-paging-info'});
133 this.displayEl = this.el.select('.x-paging-info', true).first();
134 // var navel = this.navgroup.addItem( { tagtype : 'span', html : '', cls : 'x-paging-info', preventDefault : true } );
135 // this.displayEl = navel.el.select('span',true).first();
141 Roo.each(_this.buttons, function(e){
142 Roo.factory(e).onRender(_this.el, null);
146 Roo.each(_this.toolbarItems, function(e) {
147 _this.navgroup.addItem(e);
151 this.first = this.navgroup.addItem({
152 tooltip: this.firstText,
154 icon : 'fa fa-backward',
156 preventDefault: true,
157 listeners : { click : this.onClick.createDelegate(this, ["first"]) }
160 this.prev = this.navgroup.addItem({
161 tooltip: this.prevText,
163 icon : 'fa fa-step-backward',
165 preventDefault: true,
166 listeners : { click : this.onClick.createDelegate(this, ["prev"]) }
168 //this.addSeparator();
171 var field = this.navgroup.addItem( {
173 cls : 'x-paging-position',
175 html : this.beforePageText +
176 '<input type="text" size="3" value="1" class="x-grid-page-number">' +
177 '<span class="x-paging-after">' + String.format(this.afterPageText, 1) + '</span>'
180 this.field = field.el.select('input', true).first();
181 this.field.on("keydown", this.onPagingKeydown, this);
182 this.field.on("focus", function(){this.dom.select();});
185 this.afterTextEl = field.el.select('.x-paging-after',true).first();
186 //this.field.setHeight(18);
187 //this.addSeparator();
188 this.next = this.navgroup.addItem({
189 tooltip: this.nextText,
191 html : ' <i class="fa fa-step-forward">',
193 preventDefault: true,
194 listeners : { click : this.onClick.createDelegate(this, ["next"]) }
196 this.last = this.navgroup.addItem({
197 tooltip: this.lastText,
198 icon : 'fa fa-forward',
201 preventDefault: true,
202 listeners : { click : this.onClick.createDelegate(this, ["last"]) }
204 //this.addSeparator();
205 this.loading = this.navgroup.addItem({
206 tooltip: this.refreshText,
207 icon: 'fa fa-refresh',
208 preventDefault: true,
209 listeners : { click : this.onClick.createDelegate(this, ["refresh"]) }
215 updateInfo : function(){
217 var count = (typeof(this.getCount) == 'undefined') ? this.ds.getCount() : this.getCount();
218 var msg = count == 0 ?
222 this.cursor+1, this.cursor+count, this.ds.getTotalCount()
224 this.displayEl.update(msg);
229 onLoad : function(ds, r, o){
230 this.cursor = o.params ? o.params.start : 0;
231 var d = this.getPageData(),
235 this.afterTextEl.dom.innerHTML = String.format(this.afterPageText, d.pages);
236 this.field.dom.value = ap;
237 this.first.setDisabled(ap == 1);
238 this.prev.setDisabled(ap == 1);
239 this.next.setDisabled(ap == ps);
240 this.last.setDisabled(ap == ps);
241 this.loading.enable();
246 getPageData : function(){
247 var total = this.ds.getTotalCount();
250 activePage : Math.ceil((this.cursor+this.pageSize)/this.pageSize),
251 pages : total < this.pageSize ? 1 : Math.ceil(total/this.pageSize)
256 onLoadError : function(){
257 this.loading.enable();
261 onPagingKeydown : function(e){
263 var d = this.getPageData();
265 var v = this.field.dom.value, pageNum;
266 if(!v || isNaN(pageNum = parseInt(v, 10))){
267 this.field.dom.value = d.activePage;
270 pageNum = Math.min(Math.max(1, pageNum), d.pages) - 1;
271 this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
274 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))
276 var pageNum = (k == e.HOME || (k == e.DOWN && e.ctrlKey) || (k == e.LEFT && e.ctrlKey) || (k == e.PAGEDOWN && e.ctrlKey)) ? 1 : d.pages;
277 this.field.dom.value = pageNum;
278 this.ds.load({params:{start: (pageNum - 1) * this.pageSize, limit: this.pageSize}});
281 else if(k == e.UP || k == e.RIGHT || k == e.PAGEUP || k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN)
283 var v = this.field.dom.value, pageNum;
284 var increment = (e.shiftKey) ? 10 : 1;
285 if(k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN)
287 if(!v || isNaN(pageNum = parseInt(v, 10))) {
288 this.field.dom.value = d.activePage;
291 else if(parseInt(v, 10) + increment >= 1 & parseInt(v, 10) + increment <= d.pages)
293 this.field.dom.value = parseInt(v, 10) + increment;
294 pageNum = Math.min(Math.max(1, pageNum + increment), d.pages) - 1;
295 this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
302 beforeLoad : function(){
304 this.loading.disable();
309 onClick : function(which){
318 ds.load({params:{start: 0, limit: this.pageSize}});
321 ds.load({params:{start: Math.max(0, this.cursor-this.pageSize), limit: this.pageSize}});
324 ds.load({params:{start: this.cursor+this.pageSize, limit: this.pageSize}});
327 var total = ds.getTotalCount();
328 var extra = total % this.pageSize;
329 var lastStart = extra ? (total - extra) : total-this.pageSize;
330 ds.load({params:{start: lastStart, limit: this.pageSize}});
333 ds.load({params:{start: this.cursor, limit: this.pageSize}});
339 * Unbinds the paging toolbar from the specified {@link Roo.data.Store}
340 * @param {Roo.data.Store} store The data store to unbind
342 unbind : function(ds){
343 ds.un("beforeload", this.beforeLoad, this);
344 ds.un("load", this.onLoad, this);
345 ds.un("loadexception", this.onLoadError, this);
346 ds.un("remove", this.updateInfo, this);
347 ds.un("add", this.updateInfo, this);
352 * Binds the paging toolbar to the specified {@link Roo.data.Store}
353 * @param {Roo.data.Store} store The data store to bind
356 ds.on("beforeload", this.beforeLoad, this);
357 ds.on("load", this.onLoad, this);
358 ds.on("loadexception", this.onLoadError, this);
359 ds.on("remove", this.updateInfo, this);
360 ds.on("add", this.updateInfo, this);