4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-toolbar-Paging-method-constructor'><span id='Ext-toolbar-Paging'>/**
19 </span></span> * @class Ext.toolbar.Paging
20 * @extends Ext.toolbar.Toolbar
21 * <p>As the amount of records increases, the time required for the browser to render
22 * them increases. Paging is used to reduce the amount of data exchanged with the client.
23 * Note: if there are more records/rows than can be viewed in the available screen area, vertical
24 * scrollbars will be added.</p>
25 * <p>Paging is typically handled on the server side (see exception below). The client sends
26 * parameters to the server side, which the server needs to interpret and then respond with the
27 * appropriate data.</p>
28 * <p><b>Ext.toolbar.Paging</b> is a specialized toolbar that is bound to a {@link Ext.data.Store}
29 * and provides automatic paging control. This Component {@link Ext.data.Store#load load}s blocks
30 * of data into the <tt>{@link #store}</tt> by passing {@link Ext.data.Store#paramNames paramNames} used for
31 * paging criteria.</p>
33 * {@img Ext.toolbar.Paging/Ext.toolbar.Paging.png Ext.toolbar.Paging component}
35 * <p>PagingToolbar is typically used as one of the Grid's toolbars:</p>
36 * <pre><code>
37 * var itemsPerPage = 2; // set the number of items you want per page
39 * var store = Ext.create('Ext.data.Store', {
42 * fields:['name', 'email', 'phone'],
43 * pageSize: itemsPerPage, // items per page
46 * url: 'pagingstore.js', // url that will load data with respect to start and limit params
50 * totalProperty: 'total'
55 * // specify segment of data you want to load using params
63 * Ext.create('Ext.grid.Panel', {
67 * {header: 'Name', dataIndex: 'name'},
68 * {header: 'Email', dataIndex: 'email', flex:1},
69 * {header: 'Phone', dataIndex: 'phone'}
74 * xtype: 'pagingtoolbar',
75 * store: store, // same store GridPanel is using
79 * renderTo: Ext.getBody()
81 * </code></pre>
83 * <p>To use paging, pass the paging requirements to the server when the store is first loaded.</p>
84 * <pre><code>
87 // specify params for the first page load if using paging
94 * </code></pre>
96 * <p>If using {@link Ext.data.Store#autoLoad store's autoLoad} configuration:</p>
97 * <pre><code>
98 var myStore = new Ext.data.Store({
99 {@link Ext.data.Store#autoLoad autoLoad}: {start: 0, limit: 25},
102 * </code></pre>
104 * <p>The packet sent back from the server would have this form:</p>
105 * <pre><code>
107 "success": true,
108 "results": 2000,
109 "rows": [ // <b>*Note:</b> this must be an Array
110 { "id": 1, "name": "Bill", "occupation": "Gardener" },
111 { "id": 2, "name": "Ben", "occupation": "Horticulturalist" },
113 { "id": 25, "name": "Sue", "occupation": "Botanist" }
116 * </code></pre>
117 * <p><u>Paging with Local Data</u></p>
118 * <p>Paging can also be accomplished with local data using extensions:</p>
119 * <div class="mdetail-params"><ul>
120 * <li><a href="http://sencha.com/forum/showthread.php?t=71532">Ext.ux.data.PagingStore</a></li>
121 * <li>Paging Memory Proxy (examples/ux/PagingMemoryProxy.js)</li>
122 * </ul></div>
123 * @constructor Create a new PagingToolbar
124 * @param {Object} config The config object
125 * @xtype pagingtoolbar
127 Ext.define('Ext.toolbar.Paging', {
128 extend: 'Ext.toolbar.Toolbar',
129 alias: 'widget.pagingtoolbar',
130 alternateClassName: 'Ext.PagingToolbar',
131 requires: ['Ext.toolbar.TextItem', 'Ext.form.field.Number'],
132 <span id='Ext-toolbar-Paging-cfg-store'> /**
133 </span> * @cfg {Ext.data.Store} store
134 * The {@link Ext.data.Store} the paging toolbar should use as its data source (required).
136 <span id='Ext-toolbar-Paging-cfg-displayInfo'> /**
137 </span> * @cfg {Boolean} displayInfo
138 * <tt>true</tt> to display the displayMsg (defaults to <tt>false</tt>)
141 <span id='Ext-toolbar-Paging-cfg-prependButtons'> /**
142 </span> * @cfg {Boolean} prependButtons
143 * <tt>true</tt> to insert any configured <tt>items</tt> <i>before</i> the paging buttons.
144 * Defaults to <tt>false</tt>.
146 prependButtons: false,
147 <span id='Ext-toolbar-Paging-cfg-displayMsg'> /**
148 </span> * @cfg {String} displayMsg
149 * The paging status message to display (defaults to <tt>'Displaying {0} - {1} of {2}'</tt>).
150 * Note that this string is formatted using the braced numbers <tt>{0}-{2}</tt> as tokens
151 * that are replaced by the values for start, end and total respectively. These tokens should
152 * be preserved when overriding this string if showing those values is desired.
154 displayMsg : 'Displaying {0} - {1} of {2}',
155 <span id='Ext-toolbar-Paging-cfg-emptyMsg'> /**
156 </span> * @cfg {String} emptyMsg
157 * The message to display when no records are found (defaults to 'No data to display')
159 emptyMsg : 'No data to display',
160 <span id='Ext-toolbar-Paging-cfg-beforePageText'> /**
161 </span> * @cfg {String} beforePageText
162 * The text displayed before the input item (defaults to <tt>'Page'</tt>).
164 beforePageText : 'Page',
165 <span id='Ext-toolbar-Paging-cfg-afterPageText'> /**
166 </span> * @cfg {String} afterPageText
167 * Customizable piece of the default paging text (defaults to <tt>'of {0}'</tt>). Note that
168 * this string is formatted using <tt>{0}</tt> as a token that is replaced by the number of
169 * total pages. This token should be preserved when overriding this string if showing the
170 * total page count is desired.
172 afterPageText : 'of {0}',
173 <span id='Ext-toolbar-Paging-cfg-firstText'> /**
174 </span> * @cfg {String} firstText
175 * The quicktip text displayed for the first page button (defaults to <tt>'First Page'</tt>).
176 * <b>Note</b>: quick tips must be initialized for the quicktip to show.
178 firstText : 'First Page',
179 <span id='Ext-toolbar-Paging-cfg-prevText'> /**
180 </span> * @cfg {String} prevText
181 * The quicktip text displayed for the previous page button (defaults to <tt>'Previous Page'</tt>).
182 * <b>Note</b>: quick tips must be initialized for the quicktip to show.
184 prevText : 'Previous Page',
185 <span id='Ext-toolbar-Paging-cfg-nextText'> /**
186 </span> * @cfg {String} nextText
187 * The quicktip text displayed for the next page button (defaults to <tt>'Next Page'</tt>).
188 * <b>Note</b>: quick tips must be initialized for the quicktip to show.
190 nextText : 'Next Page',
191 <span id='Ext-toolbar-Paging-cfg-lastText'> /**
192 </span> * @cfg {String} lastText
193 * The quicktip text displayed for the last page button (defaults to <tt>'Last Page'</tt>).
194 * <b>Note</b>: quick tips must be initialized for the quicktip to show.
196 lastText : 'Last Page',
197 <span id='Ext-toolbar-Paging-cfg-refreshText'> /**
198 </span> * @cfg {String} refreshText
199 * The quicktip text displayed for the Refresh button (defaults to <tt>'Refresh'</tt>).
200 * <b>Note</b>: quick tips must be initialized for the quicktip to show.
202 refreshText : 'Refresh',
203 <span id='Ext-toolbar-Paging-cfg-inputItemWidth'> /**
204 </span> * @cfg {Number} inputItemWidth
205 * The width in pixels of the input field used to display and change the current page number (defaults to 30).
209 <span id='Ext-toolbar-Paging-method-getPagingItems'> /**
210 </span> * Gets the standard paging items in the toolbar
213 getPagingItems: function() {
218 tooltip: me.firstText,
219 overflowText: me.firstText,
220 iconCls: Ext.baseCSSPrefix + 'tbar-page-first',
222 handler: me.moveFirst,
226 tooltip: me.prevText,
227 overflowText: me.prevText,
228 iconCls: Ext.baseCSSPrefix + 'tbar-page-prev',
230 handler: me.movePrevious,
236 xtype: 'numberfield',
239 cls: Ext.baseCSSPrefix + 'tbar-page-number',
240 allowDecimals: false,
243 enableKeyEvents: true,
246 width: me.inputItemWidth,
250 keydown: me.onPagingKeyDown,
251 blur: me.onPagingBlur
255 itemId: 'afterTextItem',
256 text: Ext.String.format(me.afterPageText, 1)
261 tooltip: me.nextText,
262 overflowText: me.nextText,
263 iconCls: Ext.baseCSSPrefix + 'tbar-page-next',
265 handler: me.moveNext,
269 tooltip: me.lastText,
270 overflowText: me.lastText,
271 iconCls: Ext.baseCSSPrefix + 'tbar-page-last',
273 handler: me.moveLast,
279 tooltip: me.refreshText,
280 overflowText: me.refreshText,
281 iconCls: Ext.baseCSSPrefix + 'tbar-loading',
282 handler: me.doRefresh,
287 initComponent : function(){
289 pagingItems = me.getPagingItems(),
290 userItems = me.items || me.buttons || [];
292 if (me.prependButtons) {
293 me.items = userItems.concat(pagingItems);
295 me.items = pagingItems.concat(userItems);
299 if (me.displayInfo) {
300 me.items.push('->');
301 me.items.push({xtype: 'tbtext', itemId: 'displayItem'});
307 <span id='Ext-toolbar-Paging-event-change'> /**
308 </span> * @event change
309 * Fires after the active page has been changed.
310 * @param {Ext.toolbar.Paging} this
311 * @param {Object} pageData An object that has these properties:<ul>
312 * <li><code>total</code> : Number <div class="sub-desc">The total number of records in the dataset as
313 * returned by the server</div></li>
314 * <li><code>currentPage</code> : Number <div class="sub-desc">The current page number</div></li>
315 * <li><code>pageCount</code> : Number <div class="sub-desc">The total number of pages (calculated from
316 * the total number of records in the dataset as returned by the server and the current {@link #pageSize})</div></li>
317 * <li><code>toRecord</code> : Number <div class="sub-desc">The starting record index for the current page</div></li>
318 * <li><code>fromRecord</code> : Number <div class="sub-desc">The ending record index for the current page</div></li>
322 <span id='Ext-toolbar-Paging-event-beforechange'> /**
323 </span> * @event beforechange
324 * Fires just before the active page is changed.
325 * Return false to prevent the active page from being changed.
326 * @param {Ext.toolbar.Paging} this
327 * @param {Number} page The page number that will be loaded on change
331 me.on('afterlayout', me.onLoad, me, {single: true});
333 me.bindStore(me.store, true);
336 updateInfo : function(){
338 displayItem = me.child('#displayItem'),
340 pageData = me.getPageData(),
344 count = store.getCount();
348 msg = Ext.String.format(
355 displayItem.setText(msg);
356 me.doComponentLayout();
372 pageData = me.getPageData();
373 currPage = pageData.currentPage;
374 pageCount = pageData.pageCount;
375 afterText = Ext.String.format(me.afterPageText, isNaN(pageCount) ? 1 : pageCount);
377 me.child('#afterTextItem').setText(afterText);
378 me.child('#inputItem').setValue(currPage);
379 me.child('#first').setDisabled(currPage === 1);
380 me.child('#prev').setDisabled(currPage === 1);
381 me.child('#next').setDisabled(currPage === pageCount);
382 me.child('#last').setDisabled(currPage === pageCount);
383 me.child('#refresh').enable();
385 me.fireEvent('change', me, pageData);
389 getPageData : function(){
390 var store = this.store,
391 totalCount = store.getTotalCount();
395 currentPage : store.currentPage,
396 pageCount: Math.ceil(totalCount / store.pageSize),
397 //pageCount : store.getPageCount(),
398 fromRecord: ((store.currentPage - 1) * store.pageSize) + 1,
399 toRecord: Math.min(store.currentPage * store.pageSize, totalCount)
405 onLoadError : function(){
406 if (!this.rendered) {
409 this.child('#refresh').enable();
413 readPageFromInput : function(pageData){
414 var v = this.child('#inputItem').getValue(),
415 pageNum = parseInt(v, 10);
417 if (!v || isNaN(pageNum)) {
418 this.child('#inputItem').setValue(pageData.currentPage);
424 onPagingFocus : function(){
425 this.child('#inputItem').select();
429 onPagingBlur : function(e){
430 var curPage = this.getPageData().currentPage;
431 this.child('#inputItem').setValue(curPage);
435 onPagingKeyDown : function(field, e){
437 pageData = this.getPageData(),
438 increment = e.shiftKey ? 10 : 1,
444 pageNum = me.readPageFromInput(pageData);
445 if (pageNum !== false) {
446 pageNum = Math.min(Math.max(1, pageNum), pageData.total);
447 if(me.fireEvent('beforechange', me, pageNum) !== false){
448 me.store.loadPage(pageNum);
451 } else if (k == e.HOME || k == e.END) {
453 pageNum = k == e.HOME ? 1 : pageData.pageCount;
454 field.setValue(pageNum);
455 } else if (k == e.UP || k == e.PAGEUP || k == e.DOWN || k == e.PAGEDOWN) {
457 pageNum = me.readPageFromInput(pageData);
459 if (k == e.DOWN || k == e.PAGEDOWN) {
462 pageNum += increment;
463 if (pageNum >= 1 && pageNum <= pageData.pages) {
464 field.setValue(pageNum);
471 beforeLoad : function(){
472 if(this.rendered && this.refresh){
473 this.refresh.disable();
478 doLoad : function(start){
479 if(this.fireEvent('beforechange', this, o) !== false){
484 <span id='Ext-toolbar-Paging-method-moveFirst'> /**
485 </span> * Move to the first page, has the same effect as clicking the 'first' button.
487 moveFirst : function(){
489 if(me.fireEvent('beforechange', me, 1) !== false){
490 me.store.loadPage(1);
494 <span id='Ext-toolbar-Paging-method-movePrevious'> /**
495 </span> * Move to the previous page, has the same effect as clicking the 'previous' button.
497 movePrevious : function(){
499 prev = me.store.currentPage - 1;
501 if(me.fireEvent('beforechange', me, prev) !== false){
502 me.store.previousPage();
506 <span id='Ext-toolbar-Paging-method-moveNext'> /**
507 </span> * Move to the next page, has the same effect as clicking the 'next' button.
509 moveNext : function(){
511 if(me.fireEvent('beforechange', me, me.store.currentPage + 1) !== false){
516 <span id='Ext-toolbar-Paging-method-moveLast'> /**
517 </span> * Move to the last page, has the same effect as clicking the 'last' button.
519 moveLast : function(){
521 last = this.getPageData().pageCount;
523 if(me.fireEvent('beforechange', me, last) !== false){
524 me.store.loadPage(last);
528 <span id='Ext-toolbar-Paging-method-doRefresh'> /**
529 </span> * Refresh the current page, has the same effect as clicking the 'refresh' button.
531 doRefresh : function(){
533 current = me.store.currentPage;
535 if(me.fireEvent('beforechange', me, current) !== false){
536 me.store.loadPage(current);
540 <span id='Ext-toolbar-Paging-method-bindStore'> /**
541 </span> * Binds the paging toolbar to the specified {@link Ext.data.Store}
542 * @param {Store} store The store to bind to this toolbar
543 * @param {Boolean} initial (Optional) true to not remove listeners
545 bindStore : function(store, initial){
548 if (!initial && me.store) {
549 if(store !== me.store && me.store.autoDestroy){
552 me.store.un('beforeload', me.beforeLoad, me);
553 me.store.un('load', me.onLoad, me);
554 me.store.un('exception', me.onLoadError, me);
561 store = Ext.data.StoreManager.lookup(store);
564 beforeload: me.beforeLoad,
566 exception: me.onLoadError
572 <span id='Ext-toolbar-Paging-method-unbind'> /**
573 </span> * Unbinds the paging toolbar from the specified {@link Ext.data.Store} <b>(deprecated)</b>
574 * @param {Ext.data.Store} store The data store to unbind
576 unbind : function(store){
577 this.bindStore(null);
580 <span id='Ext-toolbar-Paging-method-bind'> /**
581 </span> * Binds the paging toolbar to the specified {@link Ext.data.Store} <b>(deprecated)</b>
582 * @param {Ext.data.Store} store The data store to bind
584 bind : function(store){
585 this.bindStore(store);
589 onDestroy : function(){
590 this.bindStore(null);