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'>/**
19 </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>
124 Ext.define('Ext.toolbar.Paging', {
125 extend: 'Ext.toolbar.Toolbar',
126 alias: 'widget.pagingtoolbar',
127 alternateClassName: 'Ext.PagingToolbar',
128 requires: ['Ext.toolbar.TextItem', 'Ext.form.field.Number'],
129 <span id='Ext-toolbar-Paging-cfg-store'> /**
130 </span> * @cfg {Ext.data.Store} store
131 * The {@link Ext.data.Store} the paging toolbar should use as its data source (required).
133 <span id='Ext-toolbar-Paging-cfg-displayInfo'> /**
134 </span> * @cfg {Boolean} displayInfo
135 * <tt>true</tt> to display the displayMsg (defaults to <tt>false</tt>)
138 <span id='Ext-toolbar-Paging-cfg-prependButtons'> /**
139 </span> * @cfg {Boolean} prependButtons
140 * <tt>true</tt> to insert any configured <tt>items</tt> <i>before</i> the paging buttons.
141 * Defaults to <tt>false</tt>.
143 prependButtons: false,
144 <span id='Ext-toolbar-Paging-cfg-displayMsg'> /**
145 </span> * @cfg {String} displayMsg
146 * The paging status message to display (defaults to <tt>'Displaying {0} - {1} of {2}'</tt>).
147 * Note that this string is formatted using the braced numbers <tt>{0}-{2}</tt> as tokens
148 * that are replaced by the values for start, end and total respectively. These tokens should
149 * be preserved when overriding this string if showing those values is desired.
151 displayMsg : 'Displaying {0} - {1} of {2}',
152 <span id='Ext-toolbar-Paging-cfg-emptyMsg'> /**
153 </span> * @cfg {String} emptyMsg
154 * The message to display when no records are found (defaults to 'No data to display')
156 emptyMsg : 'No data to display',
157 <span id='Ext-toolbar-Paging-cfg-beforePageText'> /**
158 </span> * @cfg {String} beforePageText
159 * The text displayed before the input item (defaults to <tt>'Page'</tt>).
161 beforePageText : 'Page',
162 <span id='Ext-toolbar-Paging-cfg-afterPageText'> /**
163 </span> * @cfg {String} afterPageText
164 * Customizable piece of the default paging text (defaults to <tt>'of {0}'</tt>). Note that
165 * this string is formatted using <tt>{0}</tt> as a token that is replaced by the number of
166 * total pages. This token should be preserved when overriding this string if showing the
167 * total page count is desired.
169 afterPageText : 'of {0}',
170 <span id='Ext-toolbar-Paging-cfg-firstText'> /**
171 </span> * @cfg {String} firstText
172 * The quicktip text displayed for the first page button (defaults to <tt>'First Page'</tt>).
173 * <b>Note</b>: quick tips must be initialized for the quicktip to show.
175 firstText : 'First Page',
176 <span id='Ext-toolbar-Paging-cfg-prevText'> /**
177 </span> * @cfg {String} prevText
178 * The quicktip text displayed for the previous page button (defaults to <tt>'Previous Page'</tt>).
179 * <b>Note</b>: quick tips must be initialized for the quicktip to show.
181 prevText : 'Previous Page',
182 <span id='Ext-toolbar-Paging-cfg-nextText'> /**
183 </span> * @cfg {String} nextText
184 * The quicktip text displayed for the next page button (defaults to <tt>'Next Page'</tt>).
185 * <b>Note</b>: quick tips must be initialized for the quicktip to show.
187 nextText : 'Next Page',
188 <span id='Ext-toolbar-Paging-cfg-lastText'> /**
189 </span> * @cfg {String} lastText
190 * The quicktip text displayed for the last page button (defaults to <tt>'Last Page'</tt>).
191 * <b>Note</b>: quick tips must be initialized for the quicktip to show.
193 lastText : 'Last Page',
194 <span id='Ext-toolbar-Paging-cfg-refreshText'> /**
195 </span> * @cfg {String} refreshText
196 * The quicktip text displayed for the Refresh button (defaults to <tt>'Refresh'</tt>).
197 * <b>Note</b>: quick tips must be initialized for the quicktip to show.
199 refreshText : 'Refresh',
200 <span id='Ext-toolbar-Paging-cfg-inputItemWidth'> /**
201 </span> * @cfg {Number} inputItemWidth
202 * The width in pixels of the input field used to display and change the current page number (defaults to 30).
206 <span id='Ext-toolbar-Paging-method-getPagingItems'> /**
207 </span> * Gets the standard paging items in the toolbar
210 getPagingItems: function() {
215 tooltip: me.firstText,
216 overflowText: me.firstText,
217 iconCls: Ext.baseCSSPrefix + 'tbar-page-first',
219 handler: me.moveFirst,
223 tooltip: me.prevText,
224 overflowText: me.prevText,
225 iconCls: Ext.baseCSSPrefix + 'tbar-page-prev',
227 handler: me.movePrevious,
233 xtype: 'numberfield',
236 cls: Ext.baseCSSPrefix + 'tbar-page-number',
237 allowDecimals: false,
240 enableKeyEvents: true,
243 width: me.inputItemWidth,
247 keydown: me.onPagingKeyDown,
248 blur: me.onPagingBlur
252 itemId: 'afterTextItem',
253 text: Ext.String.format(me.afterPageText, 1)
258 tooltip: me.nextText,
259 overflowText: me.nextText,
260 iconCls: Ext.baseCSSPrefix + 'tbar-page-next',
262 handler: me.moveNext,
266 tooltip: me.lastText,
267 overflowText: me.lastText,
268 iconCls: Ext.baseCSSPrefix + 'tbar-page-last',
270 handler: me.moveLast,
276 tooltip: me.refreshText,
277 overflowText: me.refreshText,
278 iconCls: Ext.baseCSSPrefix + 'tbar-loading',
279 handler: me.doRefresh,
284 initComponent : function(){
286 pagingItems = me.getPagingItems(),
287 userItems = me.items || me.buttons || [];
289 if (me.prependButtons) {
290 me.items = userItems.concat(pagingItems);
292 me.items = pagingItems.concat(userItems);
296 if (me.displayInfo) {
297 me.items.push('->');
298 me.items.push({xtype: 'tbtext', itemId: 'displayItem'});
304 <span id='Ext-toolbar-Paging-event-change'> /**
305 </span> * @event change
306 * Fires after the active page has been changed.
307 * @param {Ext.toolbar.Paging} this
308 * @param {Object} pageData An object that has these properties:<ul>
309 * <li><code>total</code> : Number <div class="sub-desc">The total number of records in the dataset as
310 * returned by the server</div></li>
311 * <li><code>currentPage</code> : Number <div class="sub-desc">The current page number</div></li>
312 * <li><code>pageCount</code> : Number <div class="sub-desc">The total number of pages (calculated from
313 * the total number of records in the dataset as returned by the server and the current {@link #pageSize})</div></li>
314 * <li><code>toRecord</code> : Number <div class="sub-desc">The starting record index for the current page</div></li>
315 * <li><code>fromRecord</code> : Number <div class="sub-desc">The ending record index for the current page</div></li>
319 <span id='Ext-toolbar-Paging-event-beforechange'> /**
320 </span> * @event beforechange
321 * Fires just before the active page is changed.
322 * Return false to prevent the active page from being changed.
323 * @param {Ext.toolbar.Paging} this
324 * @param {Number} page The page number that will be loaded on change
328 me.on('afterlayout', me.onLoad, me, {single: true});
330 me.bindStore(me.store, true);
333 updateInfo : function(){
335 displayItem = me.child('#displayItem'),
337 pageData = me.getPageData(),
341 count = store.getCount();
345 msg = Ext.String.format(
352 displayItem.setText(msg);
353 me.doComponentLayout();
369 pageData = me.getPageData();
370 currPage = pageData.currentPage;
371 pageCount = pageData.pageCount;
372 afterText = Ext.String.format(me.afterPageText, isNaN(pageCount) ? 1 : pageCount);
374 me.child('#afterTextItem').setText(afterText);
375 me.child('#inputItem').setValue(currPage);
376 me.child('#first').setDisabled(currPage === 1);
377 me.child('#prev').setDisabled(currPage === 1);
378 me.child('#next').setDisabled(currPage === pageCount);
379 me.child('#last').setDisabled(currPage === pageCount);
380 me.child('#refresh').enable();
382 me.fireEvent('change', me, pageData);
386 getPageData : function(){
387 var store = this.store,
388 totalCount = store.getTotalCount();
392 currentPage : store.currentPage,
393 pageCount: Math.ceil(totalCount / store.pageSize),
394 fromRecord: ((store.currentPage - 1) * store.pageSize) + 1,
395 toRecord: Math.min(store.currentPage * store.pageSize, totalCount)
401 onLoadError : function(){
402 if (!this.rendered) {
405 this.child('#refresh').enable();
409 readPageFromInput : function(pageData){
410 var v = this.child('#inputItem').getValue(),
411 pageNum = parseInt(v, 10);
413 if (!v || isNaN(pageNum)) {
414 this.child('#inputItem').setValue(pageData.currentPage);
420 onPagingFocus : function(){
421 this.child('#inputItem').select();
425 onPagingBlur : function(e){
426 var curPage = this.getPageData().currentPage;
427 this.child('#inputItem').setValue(curPage);
431 onPagingKeyDown : function(field, e){
434 pageData = me.getPageData(),
435 increment = e.shiftKey ? 10 : 1,
440 pageNum = me.readPageFromInput(pageData);
441 if (pageNum !== false) {
442 pageNum = Math.min(Math.max(1, pageNum), pageData.pageCount);
443 if(me.fireEvent('beforechange', me, pageNum) !== false){
444 me.store.loadPage(pageNum);
447 } else if (k == e.HOME || k == e.END) {
449 pageNum = k == e.HOME ? 1 : pageData.pageCount;
450 field.setValue(pageNum);
451 } else if (k == e.UP || k == e.PAGEUP || k == e.DOWN || k == e.PAGEDOWN) {
453 pageNum = me.readPageFromInput(pageData);
455 if (k == e.DOWN || k == e.PAGEDOWN) {
458 pageNum += increment;
459 if (pageNum >= 1 && pageNum <= pageData.pages) {
460 field.setValue(pageNum);
467 beforeLoad : function(){
468 if(this.rendered && this.refresh){
469 this.refresh.disable();
474 doLoad : function(start){
475 if(this.fireEvent('beforechange', this, o) !== false){
480 <span id='Ext-toolbar-Paging-method-moveFirst'> /**
481 </span> * Move to the first page, has the same effect as clicking the 'first' button.
483 moveFirst : function(){
484 if (this.fireEvent('beforechange', this, 1) !== false){
485 this.store.loadPage(1);
489 <span id='Ext-toolbar-Paging-method-movePrevious'> /**
490 </span> * Move to the previous page, has the same effect as clicking the 'previous' button.
492 movePrevious : function(){
494 prev = me.store.currentPage - 1;
497 if (me.fireEvent('beforechange', me, prev) !== false) {
498 me.store.previousPage();
503 <span id='Ext-toolbar-Paging-method-moveNext'> /**
504 </span> * Move to the next page, has the same effect as clicking the 'next' button.
506 moveNext : function(){
508 total = me.getPageData().pageCount,
509 next = me.store.currentPage + 1;
511 if (next <= total) {
512 if (me.fireEvent('beforechange', me, next) !== false) {
518 <span id='Ext-toolbar-Paging-method-moveLast'> /**
519 </span> * Move to the last page, has the same effect as clicking the 'last' button.
521 moveLast : function(){
523 last = me.getPageData().pageCount;
525 if (me.fireEvent('beforechange', me, last) !== false) {
526 me.store.loadPage(last);
530 <span id='Ext-toolbar-Paging-method-doRefresh'> /**
531 </span> * Refresh the current page, has the same effect as clicking the 'refresh' button.
533 doRefresh : function(){
535 current = me.store.currentPage;
537 if (me.fireEvent('beforechange', me, current) !== false) {
538 me.store.loadPage(current);
542 <span id='Ext-toolbar-Paging-method-bindStore'> /**
543 </span> * Binds the paging toolbar to the specified {@link Ext.data.Store}
544 * @param {Store} store The store to bind to this toolbar
545 * @param {Boolean} initial (Optional) true to not remove listeners
547 bindStore : function(store, initial){
550 if (!initial && me.store) {
551 if(store !== me.store && me.store.autoDestroy){
554 me.store.un('beforeload', me.beforeLoad, me);
555 me.store.un('load', me.onLoad, me);
556 me.store.un('exception', me.onLoadError, me);
563 store = Ext.data.StoreManager.lookup(store);
566 beforeload: me.beforeLoad,
568 exception: me.onLoadError
574 <span id='Ext-toolbar-Paging-method-unbind'> /**
575 </span> * Unbinds the paging toolbar from the specified {@link Ext.data.Store} <b>(deprecated)</b>
576 * @param {Ext.data.Store} store The data store to unbind
578 unbind : function(store){
579 this.bindStore(null);
582 <span id='Ext-toolbar-Paging-method-bind'> /**
583 </span> * Binds the paging toolbar to the specified {@link Ext.data.Store} <b>(deprecated)</b>
584 * @param {Ext.data.Store} store The data store to bind
586 bind : function(store){
587 this.bindStore(store);
591 onDestroy : function(){
592 this.bindStore(null);