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;
35 if (this.store && !ds) {
39 this.store= Roo.factory(this.store, Roo.data);
41 ds.xmodule = this.xmodule || false;
44 this.toolbarItems = [];
46 this.toolbarItems = config.items;
50 Roo.bootstrap.PagingToolbar.superclass.constructor.call(this, config);
58 this.navgroup = new Roo.bootstrap.NavGroup({ cls: 'pagination' });
62 Roo.extend(Roo.bootstrap.PagingToolbar, Roo.bootstrap.NavSimplebar, {
64 * @cfg {Roo.data.Store} dataSource
65 * The underlying data store providing the paged data
68 * @cfg {String/HTMLElement/Element} container
69 * container The id or element that will contain the toolbar
72 * @cfg {Boolean} displayInfo
73 * True to display the displayMsg (defaults to false)
76 * @cfg {Number} pageSize
77 * The number of records to display per page (defaults to 20)
81 * @cfg {String} displayMsg
82 * The paging status message to display (defaults to "Displaying {start} - {end} of {total}")
84 displayMsg : 'Displaying {0} - {1} of {2}',
86 * @cfg {String} emptyMsg
87 * The message to display when no records are found (defaults to "No data to display")
89 emptyMsg : 'No data to display',
91 * Customizable piece of the default paging text (defaults to "Page")
94 beforePageText : "Page",
96 * Customizable piece of the default paging text (defaults to "of %0")
99 afterPageText : "of {0}",
101 * Customizable piece of the default paging text (defaults to "First Page")
104 firstText : "First Page",
106 * Customizable piece of the default paging text (defaults to "Previous Page")
109 prevText : "Previous Page",
111 * Customizable piece of the default paging text (defaults to "Next Page")
114 nextText : "Next Page",
116 * Customizable piece of the default paging text (defaults to "Last Page")
119 lastText : "Last Page",
121 * Customizable piece of the default paging text (defaults to "Refresh")
124 refreshText : "Refresh",
128 onRender : function(ct, position)
130 Roo.bootstrap.PagingToolbar.superclass.onRender.call(this, ct, position);
131 this.navgroup.parentId = this.id;
132 this.navgroup.onRender(this.el, null);
133 // add the buttons to the navgroup
135 if(this.displayInfo){
136 Roo.log(this.el.select('ul.navbar-nav',true).first());
137 this.el.select('ul.navbar-nav',true).first().createChild({cls:'x-paging-info'});
138 this.displayEl = this.el.select('.x-paging-info', true).first();
139 // var navel = this.navgroup.addItem( { tagtype : 'span', html : '', cls : 'x-paging-info', preventDefault : true } );
140 // this.displayEl = navel.el.select('span',true).first();
146 Roo.each(_this.buttons, function(e){
147 Roo.factory(e).onRender(_this.el, null);
151 Roo.each(_this.toolbarItems, function(e) {
152 _this.navgroup.addItem(e);
156 this.first = this.navgroup.addItem({
157 tooltip: this.firstText,
159 icon : 'fa fa-backward',
161 preventDefault: true,
162 listeners : { click : this.onClick.createDelegate(this, ["first"]) }
165 this.prev = this.navgroup.addItem({
166 tooltip: this.prevText,
168 icon : 'fa fa-step-backward',
170 preventDefault: true,
171 listeners : { click : this.onClick.createDelegate(this, ["prev"]) }
173 //this.addSeparator();
176 var field = this.navgroup.addItem( {
178 cls : 'x-paging-position',
180 html : this.beforePageText +
181 '<input type="text" size="3" value="1" class="x-grid-page-number">' +
182 '<span class="x-paging-after">' + String.format(this.afterPageText, 1) + '</span>'
185 this.field = field.el.select('input', true).first();
186 this.field.on("keydown", this.onPagingKeydown, this);
187 this.field.on("focus", function(){this.dom.select();});
190 this.afterTextEl = field.el.select('.x-paging-after',true).first();
191 //this.field.setHeight(18);
192 //this.addSeparator();
193 this.next = this.navgroup.addItem({
194 tooltip: this.nextText,
196 html : ' <i class="fa fa-step-forward">',
198 preventDefault: true,
199 listeners : { click : this.onClick.createDelegate(this, ["next"]) }
201 this.last = this.navgroup.addItem({
202 tooltip: this.lastText,
203 icon : 'fa fa-forward',
206 preventDefault: true,
207 listeners : { click : this.onClick.createDelegate(this, ["last"]) }
209 //this.addSeparator();
210 this.loading = this.navgroup.addItem({
211 tooltip: this.refreshText,
212 icon: 'fa fa-refresh',
213 preventDefault: true,
214 listeners : { click : this.onClick.createDelegate(this, ["refresh"]) }
220 updateInfo : function(){
222 var count = (typeof(this.getCount) == 'undefined') ? this.ds.getCount() : this.getCount();
223 var msg = count == 0 ?
227 this.cursor+1, this.cursor+count, this.ds.getTotalCount()
229 this.displayEl.update(msg);
234 onLoad : function(ds, r, o){
235 this.cursor = o.params ? o.params.start : 0;
236 var d = this.getPageData(),
240 this.afterTextEl.dom.innerHTML = String.format(this.afterPageText, d.pages);
241 this.field.dom.value = ap;
242 this.first.setDisabled(ap == 1);
243 this.prev.setDisabled(ap == 1);
244 this.next.setDisabled(ap == ps);
245 this.last.setDisabled(ap == ps);
246 this.loading.enable();
251 getPageData : function(){
252 var total = this.ds.getTotalCount();
255 activePage : Math.ceil((this.cursor+this.pageSize)/this.pageSize),
256 pages : total < this.pageSize ? 1 : Math.ceil(total/this.pageSize)
261 onLoadError : function(){
262 this.loading.enable();
266 onPagingKeydown : function(e){
268 var d = this.getPageData();
270 var v = this.field.dom.value, pageNum;
271 if(!v || isNaN(pageNum = parseInt(v, 10))){
272 this.field.dom.value = d.activePage;
275 pageNum = Math.min(Math.max(1, pageNum), d.pages) - 1;
276 this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
279 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))
281 var pageNum = (k == e.HOME || (k == e.DOWN && e.ctrlKey) || (k == e.LEFT && e.ctrlKey) || (k == e.PAGEDOWN && e.ctrlKey)) ? 1 : d.pages;
282 this.field.dom.value = pageNum;
283 this.ds.load({params:{start: (pageNum - 1) * this.pageSize, limit: this.pageSize}});
286 else if(k == e.UP || k == e.RIGHT || k == e.PAGEUP || k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN)
288 var v = this.field.dom.value, pageNum;
289 var increment = (e.shiftKey) ? 10 : 1;
290 if(k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN)
292 if(!v || isNaN(pageNum = parseInt(v, 10))) {
293 this.field.dom.value = d.activePage;
296 else if(parseInt(v, 10) + increment >= 1 & parseInt(v, 10) + increment <= d.pages)
298 this.field.dom.value = parseInt(v, 10) + increment;
299 pageNum = Math.min(Math.max(1, pageNum + increment), d.pages) - 1;
300 this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
307 beforeLoad : function(){
309 this.loading.disable();
314 onClick : function(which){
323 ds.load({params:{start: 0, limit: this.pageSize}});
326 ds.load({params:{start: Math.max(0, this.cursor-this.pageSize), limit: this.pageSize}});
329 ds.load({params:{start: this.cursor+this.pageSize, limit: this.pageSize}});
332 var total = ds.getTotalCount();
333 var extra = total % this.pageSize;
334 var lastStart = extra ? (total - extra) : total-this.pageSize;
335 ds.load({params:{start: lastStart, limit: this.pageSize}});
338 ds.load({params:{start: this.cursor, limit: this.pageSize}});
344 * Unbinds the paging toolbar from the specified {@link Roo.data.Store}
345 * @param {Roo.data.Store} store The data store to unbind
347 unbind : function(ds){
348 ds.un("beforeload", this.beforeLoad, this);
349 ds.un("load", this.onLoad, this);
350 ds.un("loadexception", this.onLoadError, this);
351 ds.un("remove", this.updateInfo, this);
352 ds.un("add", this.updateInfo, this);
357 * Binds the paging toolbar to the specified {@link Roo.data.Store}
358 * @param {Roo.data.Store} store The data store to bind
361 ds.on("beforeload", this.beforeLoad, this);
362 ds.on("load", this.onLoad, this);
363 ds.on("loadexception", this.onLoadError, this);
364 ds.on("remove", this.updateInfo, this);
365 ds.on("add", this.updateInfo, this);