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 this.ds = config.dataSource;
28 if (config.store && !this.ds) {
29 this.store= Roo.factory(config.store, Roo.data);
31 this.ds.xmodule = this.xmodule || false;
34 this.toolbarItems = [];
36 this.toolbarItems = config.items;
39 Roo.bootstrap.PagingToolbar.superclass.constructor.call(this, config);
47 this.navgroup = new Roo.bootstrap.NavGroup({ cls: 'pagination' });
51 Roo.extend(Roo.bootstrap.PagingToolbar, Roo.bootstrap.NavSimplebar, {
53 * @cfg {Roo.data.Store} dataSource
54 * The underlying data store providing the paged data
57 * @cfg {String/HTMLElement/Element} container
58 * container The id or element that will contain the toolbar
61 * @cfg {Boolean} displayInfo
62 * True to display the displayMsg (defaults to false)
65 * @cfg {Number} pageSize
66 * The number of records to display per page (defaults to 20)
70 * @cfg {String} displayMsg
71 * The paging status message to display (defaults to "Displaying {start} - {end} of {total}")
73 displayMsg : 'Displaying {0} - {1} of {2}',
75 * @cfg {String} emptyMsg
76 * The message to display when no records are found (defaults to "No data to display")
78 emptyMsg : 'No data to display',
80 * Customizable piece of the default paging text (defaults to "Page")
83 beforePageText : "Page",
85 * Customizable piece of the default paging text (defaults to "of %0")
88 afterPageText : "of {0}",
90 * Customizable piece of the default paging text (defaults to "First Page")
93 firstText : "First Page",
95 * Customizable piece of the default paging text (defaults to "Previous Page")
98 prevText : "Previous Page",
100 * Customizable piece of the default paging text (defaults to "Next Page")
103 nextText : "Next Page",
105 * Customizable piece of the default paging text (defaults to "Last Page")
108 lastText : "Last Page",
110 * Customizable piece of the default paging text (defaults to "Refresh")
113 refreshText : "Refresh",
117 onRender : function(ct, position)
119 Roo.bootstrap.PagingToolbar.superclass.onRender.call(this, ct, position);
120 this.navgroup.parentId = this.id;
121 this.navgroup.onRender(this.el, null);
122 // add the buttons to the navgroup
124 if(this.displayInfo){
125 this.el.select('ul.navbar-nav',true).first().createChild({cls:'x-paging-info'});
126 this.displayEl = this.el.select('.x-paging-info', true).first();
127 // var navel = this.navgroup.addItem( { tagtype : 'span', html : '', cls : 'x-paging-info', preventDefault : true } );
128 // this.displayEl = navel.el.select('span',true).first();
133 Roo.log('PagingToolbar onRender!!!!!!!!!!!!!!!!!!!!');
134 Roo.log(this.buttons);
137 Roo.each(_this.buttons, function(e){ // this might need to use render????
138 Roo.factory(e).onRender(_this.el, null);
142 Roo.each(_this.toolbarItems, function(e) {
143 _this.navgroup.addItem(e);
147 this.first = this.navgroup.addItem({
148 tooltip: this.firstText,
150 icon : 'fa fa-backward',
152 preventDefault: true,
153 listeners : { click : this.onClick.createDelegate(this, ["first"]) }
156 this.prev = this.navgroup.addItem({
157 tooltip: this.prevText,
159 icon : 'fa fa-step-backward',
161 preventDefault: true,
162 listeners : { click : this.onClick.createDelegate(this, ["prev"]) }
164 //this.addSeparator();
167 var field = this.navgroup.addItem( {
169 cls : 'x-paging-position',
171 html : this.beforePageText +
172 '<input type="text" size="3" value="1" class="x-grid-page-number">' +
173 '<span class="x-paging-after">' + String.format(this.afterPageText, 1) + '</span>'
176 this.field = field.el.select('input', true).first();
177 this.field.on("keydown", this.onPagingKeydown, this);
178 this.field.on("focus", function(){this.dom.select();});
181 this.afterTextEl = field.el.select('.x-paging-after',true).first();
182 //this.field.setHeight(18);
183 //this.addSeparator();
184 this.next = this.navgroup.addItem({
185 tooltip: this.nextText,
187 html : ' <i class="fa fa-step-forward">',
189 preventDefault: true,
190 listeners : { click : this.onClick.createDelegate(this, ["next"]) }
192 this.last = this.navgroup.addItem({
193 tooltip: this.lastText,
194 icon : 'fa fa-forward',
197 preventDefault: true,
198 listeners : { click : this.onClick.createDelegate(this, ["last"]) }
200 //this.addSeparator();
201 this.loading = this.navgroup.addItem({
202 tooltip: this.refreshText,
203 icon: 'fa fa-refresh',
204 preventDefault: true,
205 listeners : { click : this.onClick.createDelegate(this, ["refresh"]) }
211 updateInfo : function(){
213 var count = (typeof(this.getCount) == 'undefined') ? this.ds.getCount() : this.getCount();
214 var msg = count == 0 ?
218 this.cursor+1, this.cursor+count, this.ds.getTotalCount()
220 this.displayEl.update(msg);
225 onLoad : function(ds, r, o)
227 this.cursor = o.params.start ? o.params.start : 0;
229 var d = this.getPageData(),
234 this.afterTextEl.dom.innerHTML = String.format(this.afterPageText, d.pages);
235 this.field.dom.value = ap;
236 this.first.setDisabled(ap == 1);
237 this.prev.setDisabled(ap == 1);
238 this.next.setDisabled(ap == ps);
239 this.last.setDisabled(ap == ps);
240 this.loading.enable();
245 getPageData : function(){
246 var total = this.ds.getTotalCount();
249 activePage : Math.ceil((this.cursor+this.pageSize)/this.pageSize),
250 pages : total < this.pageSize ? 1 : Math.ceil(total/this.pageSize)
255 onLoadError : function(){
256 this.loading.enable();
260 onPagingKeydown : function(e){
262 var d = this.getPageData();
264 var v = this.field.dom.value, pageNum;
265 if(!v || isNaN(pageNum = parseInt(v, 10))){
266 this.field.dom.value = d.activePage;
269 pageNum = Math.min(Math.max(1, pageNum), d.pages) - 1;
270 this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
273 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))
275 var pageNum = (k == e.HOME || (k == e.DOWN && e.ctrlKey) || (k == e.LEFT && e.ctrlKey) || (k == e.PAGEDOWN && e.ctrlKey)) ? 1 : d.pages;
276 this.field.dom.value = pageNum;
277 this.ds.load({params:{start: (pageNum - 1) * this.pageSize, limit: this.pageSize}});
280 else if(k == e.UP || k == e.RIGHT || k == e.PAGEUP || k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN)
282 var v = this.field.dom.value, pageNum;
283 var increment = (e.shiftKey) ? 10 : 1;
284 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);