3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
8 * @class Ext.PagingToolbar
10 * <p>As the amount of records increases, the time required for the browser to render
11 * them increases. Paging is used to reduce the amount of data exchanged with the client.
12 * Note: if there are more records/rows than can be viewed in the available screen area, vertical
13 * scrollbars will be added.</p>
14 * <p>Paging is typically handled on the server side (see exception below). The client sends
15 * parameters to the server side, which the server needs to interpret and then respond with the
16 * approprate data.</p>
17 * <p><b>Ext.PagingToolbar</b> is a specialized toolbar that is bound to a {@link Ext.data.Store}
18 * and provides automatic paging control. This Component {@link Ext.data.Store#load load}s blocks
19 * of data into the <tt>{@link #store}</tt> by passing {@link Ext.data.Store#paramNames paramNames} used for
20 * paging criteria.</p>
21 * <p>PagingToolbar is typically used as one of the Grid's toolbars:</p>
23 Ext.QuickTips.init(); // to display button quicktips
25 var myStore = new Ext.data.Store({
29 var myPageSize = 25; // server script should only send back 25 items
31 var grid = new Ext.grid.GridPanel({
34 bbar: new Ext.PagingToolbar({
35 {@link #store}: myStore, // grid and PagingToolbar using same store
36 {@link #displayInfo}: true,
37 {@link #pageSize}: myPageSize,
38 {@link #prependButtons}: true,
46 * <p>To use paging, pass the paging requirements to the server when the store is first loaded.</p>
50 start: 0, // specify params for the first page load if using paging
56 * <p><u>Paging with Local Data</u></p>
57 * <p>Paging can also be accomplished with local data using extensions:</p>
58 * <div class="mdetail-params"><ul>
59 * <li><a href="http://extjs.com/forum/showthread.php?t=57386">Ext.ux.data.PagingStore</a></li>
60 * <li>Paging Memory Proxy (examples/ux/PagingMemoryProxy.js)</li>
63 * Create a new PagingToolbar
64 * @param {Object} config The config object
71 Ext.PagingToolbar = Ext.extend(Ext.Toolbar, {
73 * @cfg {Ext.data.Store} store
74 * The {@link Ext.data.Store} the paging toolbar should use as its data source (required).
77 * @cfg {Boolean} displayInfo
78 * <tt>true</tt> to display the displayMsg (defaults to <tt>false</tt>)
81 * @cfg {Number} pageSize
82 * The number of records to display per page (defaults to <tt>20</tt>)
86 * @cfg {Boolean} prependButtons
87 * <tt>true</tt> to insert any configured <tt>items</tt> <i>before</i> the paging buttons.
88 * Defaults to <tt>false</tt>.
91 * @cfg {String} displayMsg
92 * The paging status message to display (defaults to <tt>'Displaying {0} - {1} of {2}'</tt>).
93 * Note that this string is formatted using the braced numbers <tt>{0}-{2}</tt> as tokens
94 * that are replaced by the values for start, end and total respectively. These tokens should
95 * be preserved when overriding this string if showing those values is desired.
97 displayMsg : 'Displaying {0} - {1} of {2}',
99 * @cfg {String} emptyMsg
100 * The message to display when no records are found (defaults to 'No data to display')
102 emptyMsg : 'No data to display',
104 * @cfg {String} beforePageText
105 * The text displayed before the input item (defaults to <tt>'Page'</tt>).
107 beforePageText : 'Page',
109 * @cfg {String} afterPageText
110 * Customizable piece of the default paging text (defaults to <tt>'of {0}'</tt>). Note that
111 * this string is formatted using <tt>{0}</tt> as a token that is replaced by the number of
112 * total pages. This token should be preserved when overriding this string if showing the
113 * total page count is desired.
115 afterPageText : 'of {0}',
117 * @cfg {String} firstText
118 * The quicktip text displayed for the first page button (defaults to <tt>'First Page'</tt>).
119 * <b>Note</b>: quick tips must be initialized for the quicktip to show.
121 firstText : 'First Page',
123 * @cfg {String} prevText
124 * The quicktip text displayed for the previous page button (defaults to <tt>'Previous Page'</tt>).
125 * <b>Note</b>: quick tips must be initialized for the quicktip to show.
127 prevText : 'Previous Page',
129 * @cfg {String} nextText
130 * The quicktip text displayed for the next page button (defaults to <tt>'Next Page'</tt>).
131 * <b>Note</b>: quick tips must be initialized for the quicktip to show.
133 nextText : 'Next Page',
135 * @cfg {String} lastText
136 * The quicktip text displayed for the last page button (defaults to <tt>'Last Page'</tt>).
137 * <b>Note</b>: quick tips must be initialized for the quicktip to show.
139 lastText : 'Last Page',
141 * @cfg {String} refreshText
142 * The quicktip text displayed for the Refresh button (defaults to <tt>'Refresh'</tt>).
143 * <b>Note</b>: quick tips must be initialized for the quicktip to show.
145 refreshText : 'Refresh',
149 * <b>The defaults for these should be set in the data store.</b>
150 * Object mapping of parameter names used for load calls, initially set to:
151 * <pre>{start: 'start', limit: 'limit'}</pre>
155 * The number of records to display per page. See also <tt>{@link #cursor}</tt>.
161 * Indicator for the record position. This property might be used to get the active page
162 * number for example:<pre><code>
163 * // t is reference to the paging toolbar instance
164 * var activePage = Math.ceil((t.cursor + t.pageSize) / t.pageSize);
170 initComponent : function(){
171 var pagingItems = [this.first = new T.Button({
172 tooltip: this.firstText,
173 overflowText: this.firstText,
174 iconCls: 'x-tbar-page-first',
176 handler: this.moveFirst,
178 }), this.prev = new T.Button({
179 tooltip: this.prevText,
180 overflowText: this.prevText,
181 iconCls: 'x-tbar-page-prev',
183 handler: this.movePrevious,
185 }), '-', this.beforePageText,
186 this.inputItem = new Ext.form.NumberField({
187 cls: 'x-tbar-page-number',
188 allowDecimals: false,
189 allowNegative: false,
190 enableKeyEvents: true,
194 keydown: this.onPagingKeyDown,
195 blur: this.onPagingBlur
197 }), this.afterTextItem = new T.TextItem({
198 text: String.format(this.afterPageText, 1)
199 }), '-', this.next = new T.Button({
200 tooltip: this.nextText,
201 overflowText: this.nextText,
202 iconCls: 'x-tbar-page-next',
204 handler: this.moveNext,
206 }), this.last = new T.Button({
207 tooltip: this.lastText,
208 overflowText: this.lastText,
209 iconCls: 'x-tbar-page-last',
211 handler: this.moveLast,
213 }), '-', this.refresh = new T.Button({
214 tooltip: this.refreshText,
215 overflowText: this.refreshText,
216 iconCls: 'x-tbar-loading',
217 handler: this.refresh,
222 var userItems = this.items || this.buttons || [];
223 if (this.prependButtons) {
224 this.items = userItems.concat(pagingItems);
226 this.items = pagingItems.concat(userItems);
229 if(this.displayInfo){
230 this.items.push('->');
231 this.items.push(this.displayItem = new T.TextItem({}));
233 Ext.PagingToolbar.superclass.initComponent.call(this);
237 * Fires after the active page has been changed.
238 * @param {Ext.PagingToolbar} this
239 * @param {Object} pageData An object that has these properties:<ul>
240 * <li><code>total</code> : Number <div class="sub-desc">The total number of records in the dataset as
241 * returned by the server</div></li>
242 * <li><code>activePage</code> : Number <div class="sub-desc">The current page number</div></li>
243 * <li><code>pages</code> : Number <div class="sub-desc">The total number of pages (calculated from
244 * the total number of records in the dataset as returned by the server and the current {@link #pageSize})</div></li>
249 * @event beforechange
250 * Fires just before the active page is changed.
251 * Return false to prevent the active page from being changed.
252 * @param {Ext.PagingToolbar} this
253 * @param {Object} params An object hash of the parameters which the PagingToolbar will send when
254 * loading the required page. This will contain:<ul>
255 * <li><code>start</code> : Number <div class="sub-desc">The starting row number for the next page of records to
256 * be retrieved from the server</div></li>
257 * <li><code>limit</code> : Number <div class="sub-desc">The number of records to be retrieved from the server</div></li>
259 * <p>(note: the names of the <b>start</b> and <b>limit</b> properties are determined
260 * by the store's {@link Ext.data.Store#paramNames paramNames} property.)</p>
261 * <p>Parameters may be added as required in the event handler.</p>
265 this.on('afterlayout', this.onFirstLayout, this, {single: true});
267 this.bindStore(this.store);
271 onFirstLayout : function(){
273 this.onLoad.apply(this, this.dsLoaded);
278 updateInfo : function(){
279 if(this.displayItem){
280 var count = this.store.getCount();
281 var msg = count == 0 ?
285 this.cursor+1, this.cursor+count, this.store.getTotalCount()
287 this.displayItem.setText(msg);
292 onLoad : function(store, r, o){
294 this.dsLoaded = [store, r, o];
297 var p = this.getParams();
298 this.cursor = (o.params && o.params[p.start]) ? o.params[p.start] : 0;
299 var d = this.getPageData(), ap = d.activePage, ps = d.pages;
301 this.afterTextItem.setText(String.format(this.afterPageText, d.pages));
302 this.inputItem.setValue(ap);
303 this.first.setDisabled(ap == 1);
304 this.prev.setDisabled(ap == 1);
305 this.next.setDisabled(ap == ps);
306 this.last.setDisabled(ap == ps);
307 this.refresh.enable();
309 this.fireEvent('change', this, d);
313 getPageData : function(){
314 var total = this.store.getTotalCount();
317 activePage : Math.ceil((this.cursor+this.pageSize)/this.pageSize),
318 pages : total < this.pageSize ? 1 : Math.ceil(total/this.pageSize)
323 * Change the active page
324 * @param {Integer} page The page to display
326 changePage : function(page){
327 this.doLoad(((page-1) * this.pageSize).constrain(0, this.store.getTotalCount()));
331 onLoadError : function(){
335 this.refresh.enable();
339 readPage : function(d){
340 var v = this.inputItem.getValue(), pageNum;
341 if (!v || isNaN(pageNum = parseInt(v, 10))) {
342 this.inputItem.setValue(d.activePage);
348 onPagingFocus : function(){
349 this.inputItem.select();
353 onPagingBlur : function(e){
354 this.inputItem.setValue(this.getPageData().activePage);
358 onPagingKeyDown : function(field, e){
359 var k = e.getKey(), d = this.getPageData(), pageNum;
362 pageNum = this.readPage(d);
363 if(pageNum !== false){
364 pageNum = Math.min(Math.max(1, pageNum), d.pages) - 1;
365 this.doLoad(pageNum * this.pageSize);
367 }else if (k == e.HOME || k == e.END){
369 pageNum = k == e.HOME ? 1 : d.pages;
370 field.setValue(pageNum);
371 }else if (k == e.UP || k == e.PAGEUP || k == e.DOWN || k == e.PAGEDOWN){
373 if((pageNum = this.readPage(d))){
374 var increment = e.shiftKey ? 10 : 1;
375 if(k == e.DOWN || k == e.PAGEDOWN){
378 pageNum += increment;
379 if(pageNum >= 1 & pageNum <= d.pages){
380 field.setValue(pageNum);
387 getParams : function(){
388 //retain backwards compat, allow params on the toolbar itself, if they exist.
389 return this.paramNames || this.store.paramNames;
393 beforeLoad : function(){
394 if(this.rendered && this.refresh){
395 this.refresh.disable();
400 doLoad : function(start){
401 var o = {}, pn = this.getParams();
403 o[pn.limit] = this.pageSize;
404 if(this.fireEvent('beforechange', this, o) !== false){
405 this.store.load({params:o});
410 * Move to the first page, has the same effect as clicking the 'first' button.
412 moveFirst : function(){
417 * Move to the previous page, has the same effect as clicking the 'previous' button.
419 movePrevious : function(){
420 this.doLoad(Math.max(0, this.cursor-this.pageSize));
424 * Move to the next page, has the same effect as clicking the 'next' button.
426 moveNext : function(){
427 this.doLoad(this.cursor+this.pageSize);
431 * Move to the last page, has the same effect as clicking the 'last' button.
433 moveLast : function(){
434 var total = this.store.getTotalCount(),
435 extra = total % this.pageSize;
437 this.doLoad(extra ? (total - extra) : total - this.pageSize);
441 * Refresh the current page, has the same effect as clicking the 'refresh' button.
443 refresh : function(){
444 this.doLoad(this.cursor);
448 * Binds the paging toolbar to the specified {@link Ext.data.Store}
449 * @param {Store} store The store to bind to this toolbar
450 * @param {Boolean} initial (Optional) true to not remove listeners
452 bindStore : function(store, initial){
454 if(!initial && this.store){
455 this.store.un('beforeload', this.beforeLoad, this);
456 this.store.un('load', this.onLoad, this);
457 this.store.un('exception', this.onLoadError, this);
458 if(store !== this.store && this.store.autoDestroy){
459 this.store.destroy();
463 store = Ext.StoreMgr.lookup(store);
466 beforeload: this.beforeLoad,
468 exception: this.onLoadError
470 doLoad = store.getCount() > 0;
474 this.onLoad(store, null, {});
479 * Unbinds the paging toolbar from the specified {@link Ext.data.Store} <b>(deprecated)</b>
480 * @param {Ext.data.Store} store The data store to unbind
482 unbind : function(store){
483 this.bindStore(null);
487 * Binds the paging toolbar to the specified {@link Ext.data.Store} <b>(deprecated)</b>
488 * @param {Ext.data.Store} store The data store to bind
490 bind : function(store){
491 this.bindStore(store);
495 onDestroy : function(){
496 this.bindStore(null);
497 Ext.PagingToolbar.superclass.onDestroy.call(this);
502 Ext.reg('paging', Ext.PagingToolbar);