4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/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> * As the number of records increases, the time required for the browser to render them increases. Paging is used to
20 * reduce the amount of data exchanged with the client. Note: if there are more records/rows than can be viewed in the
21 * available screen area, vertical scrollbars will be added.
23 * Paging is typically handled on the server side (see exception below). The client sends parameters to the server side,
24 * which the server needs to interpret and then respond with the appropriate data.
26 * Ext.toolbar.Paging is a specialized toolbar that is bound to a {@link Ext.data.Store} and provides automatic
27 * paging control. This Component {@link Ext.data.Store#load load}s blocks of data into the {@link #store} by passing
28 * parameters used for paging criteria.
30 * {@img Ext.toolbar.Paging/Ext.toolbar.Paging.png Ext.toolbar.Paging component}
32 * Paging Toolbar is typically used as one of the Grid's toolbars:
35 * var itemsPerPage = 2; // set the number of items you want per page
37 * var store = Ext.create('Ext.data.Store', {
40 * fields:['name', 'email', 'phone'],
41 * pageSize: itemsPerPage, // items per page
44 * url: 'pagingstore.js', // url that will load data with respect to start and limit params
48 * totalProperty: 'total'
53 * // specify segment of data you want to load using params
61 * Ext.create('Ext.grid.Panel', {
65 * { header: 'Name', dataIndex: 'name' },
66 * { header: 'Email', dataIndex: 'email', flex: 1 },
67 * { header: 'Phone', dataIndex: 'phone' }
72 * xtype: 'pagingtoolbar',
73 * store: store, // same store GridPanel is using
77 * renderTo: Ext.getBody()
80 * To use paging, pass the paging requirements to the server when the store is first loaded.
84 * // specify params for the first page load if using paging
92 * If using {@link Ext.data.Store#autoLoad store's autoLoad} configuration:
94 * var myStore = Ext.create('Ext.data.Store', {
95 * {@link Ext.data.Store#autoLoad autoLoad}: {start: 0, limit: 25},
99 * The packet sent back from the server would have this form:
102 * "success": true,
103 * "results": 2000,
104 * "rows": [ // ***Note:** this must be an Array
105 * { "id": 1, "name": "Bill", "occupation": "Gardener" },
106 * { "id": 2, "name": "Ben", "occupation": "Horticulturalist" },
108 * { "id": 25, "name": "Sue", "occupation": "Botanist" }
112 * ## Paging with Local Data
114 * Paging can also be accomplished with local data using extensions:
116 * - [Ext.ux.data.PagingStore][1]
117 * - Paging Memory Proxy (examples/ux/PagingMemoryProxy.js)
119 * [1]: http://sencha.com/forum/showthread.php?t=71532
121 Ext.define('Ext.toolbar.Paging', {
122 extend: 'Ext.toolbar.Toolbar',
123 alias: 'widget.pagingtoolbar',
124 alternateClassName: 'Ext.PagingToolbar',
125 requires: ['Ext.toolbar.TextItem', 'Ext.form.field.Number'],
126 <span id='Ext-toolbar-Paging-cfg-store'> /**
127 </span> * @cfg {Ext.data.Store} store (required)
128 * The {@link Ext.data.Store} the paging toolbar should use as its data source.
131 <span id='Ext-toolbar-Paging-cfg-displayInfo'> /**
132 </span> * @cfg {Boolean} displayInfo
133 * true to display the displayMsg
137 <span id='Ext-toolbar-Paging-cfg-prependButtons'> /**
138 </span> * @cfg {Boolean} prependButtons
139 * true to insert any configured items _before_ the paging buttons.
141 prependButtons: false,
143 <span id='Ext-toolbar-Paging-cfg-displayMsg'> /**
144 </span> * @cfg {String} displayMsg
145 * The paging status message to display. Note that this string is
146 * formatted using the braced numbers {0}-{2} as tokens that are replaced by the values for start, end and total
147 * respectively. These tokens should be preserved when overriding this string if showing those values is desired.
149 displayMsg : 'Displaying {0} - {1} of {2}',
151 <span id='Ext-toolbar-Paging-cfg-emptyMsg'> /**
152 </span> * @cfg {String} emptyMsg
153 * The message to display when no records are found.
155 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.
161 beforePageText : 'Page',
163 <span id='Ext-toolbar-Paging-cfg-afterPageText'> /**
164 </span> * @cfg {String} afterPageText
165 * Customizable piece of the default paging text. Note that this string is formatted using
166 * {0} as a token that is replaced by the number of total pages. This token should be preserved when overriding this
167 * string if showing the total page count is desired.
169 afterPageText : 'of {0}',
171 <span id='Ext-toolbar-Paging-cfg-firstText'> /**
172 </span> * @cfg {String} firstText
173 * The quicktip text displayed for the first page button.
174 * **Note**: quick tips must be initialized for the quicktip to show.
176 firstText : 'First Page',
178 <span id='Ext-toolbar-Paging-cfg-prevText'> /**
179 </span> * @cfg {String} prevText
180 * The quicktip text displayed for the previous page button.
181 * **Note**: quick tips must be initialized for the quicktip to show.
183 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.
188 * **Note**: quick tips must be initialized for the quicktip to show.
190 nextText : 'Next Page',
192 <span id='Ext-toolbar-Paging-cfg-lastText'> /**
193 </span> * @cfg {String} lastText
194 * The quicktip text displayed for the last page button.
195 * **Note**: quick tips must be initialized for the quicktip to show.
197 lastText : 'Last Page',
199 <span id='Ext-toolbar-Paging-cfg-refreshText'> /**
200 </span> * @cfg {String} refreshText
201 * The quicktip text displayed for the Refresh button.
202 * **Note**: quick tips must be initialized for the quicktip to show.
204 refreshText : 'Refresh',
206 <span id='Ext-toolbar-Paging-cfg-inputItemWidth'> /**
207 </span> * @cfg {Number} inputItemWidth
208 * The width in pixels of the input field used to display and change the current page number.
212 <span id='Ext-toolbar-Paging-method-getPagingItems'> /**
213 </span> * Gets the standard paging items in the toolbar
216 getPagingItems: function() {
221 tooltip: me.firstText,
222 overflowText: me.firstText,
223 iconCls: Ext.baseCSSPrefix + 'tbar-page-first',
225 handler: me.moveFirst,
229 tooltip: me.prevText,
230 overflowText: me.prevText,
231 iconCls: Ext.baseCSSPrefix + 'tbar-page-prev',
233 handler: me.movePrevious,
239 xtype: 'numberfield',
242 cls: Ext.baseCSSPrefix + 'tbar-page-number',
243 allowDecimals: false,
246 enableKeyEvents: true,
249 width: me.inputItemWidth,
253 keydown: me.onPagingKeyDown,
254 blur: me.onPagingBlur
258 itemId: 'afterTextItem',
259 text: Ext.String.format(me.afterPageText, 1)
264 tooltip: me.nextText,
265 overflowText: me.nextText,
266 iconCls: Ext.baseCSSPrefix + 'tbar-page-next',
268 handler: me.moveNext,
272 tooltip: me.lastText,
273 overflowText: me.lastText,
274 iconCls: Ext.baseCSSPrefix + 'tbar-page-last',
276 handler: me.moveLast,
282 tooltip: me.refreshText,
283 overflowText: me.refreshText,
284 iconCls: Ext.baseCSSPrefix + 'tbar-loading',
285 handler: me.doRefresh,
290 initComponent : function(){
292 pagingItems = me.getPagingItems(),
293 userItems = me.items || me.buttons || [];
295 if (me.prependButtons) {
296 me.items = userItems.concat(pagingItems);
298 me.items = pagingItems.concat(userItems);
302 if (me.displayInfo) {
303 me.items.push('->');
304 me.items.push({xtype: 'tbtext', itemId: 'displayItem'});
310 <span id='Ext-toolbar-Paging-event-change'> /**
311 </span> * @event change
312 * Fires after the active page has been changed.
313 * @param {Ext.toolbar.Paging} this
314 * @param {Object} pageData An object that has these properties:
318 * The total number of records in the dataset as returned by the server
320 * - `currentPage` : Number
322 * The current page number
324 * - `pageCount` : Number
326 * The total number of pages (calculated from the total number of records in the dataset as returned by the
327 * server and the current {@link Ext.data.Store#pageSize pageSize})
329 * - `toRecord` : Number
331 * The starting record index for the current page
333 * - `fromRecord` : Number
335 * The ending record index for the current page
339 <span id='Ext-toolbar-Paging-event-beforechange'> /**
340 </span> * @event beforechange
341 * Fires just before the active page is changed. Return false to prevent the active page from being changed.
342 * @param {Ext.toolbar.Paging} this
343 * @param {Number} page The page number that will be loaded on change
347 me.on('afterlayout', me.onLoad, me, {single: true});
349 me.bindStore(me.store || 'ext-empty-store', true);
352 updateInfo : function(){
354 displayItem = me.child('#displayItem'),
356 pageData = me.getPageData(),
360 count = store.getCount();
364 msg = Ext.String.format(
371 displayItem.setText(msg);
372 me.doComponentLayout();
388 pageData = me.getPageData();
389 currPage = pageData.currentPage;
390 pageCount = pageData.pageCount;
391 afterText = Ext.String.format(me.afterPageText, isNaN(pageCount) ? 1 : pageCount);
393 me.child('#afterTextItem').setText(afterText);
394 me.child('#inputItem').setValue(currPage);
395 me.child('#first').setDisabled(currPage === 1);
396 me.child('#prev').setDisabled(currPage === 1);
397 me.child('#next').setDisabled(currPage === pageCount);
398 me.child('#last').setDisabled(currPage === pageCount);
399 me.child('#refresh').enable();
401 me.fireEvent('change', me, pageData);
405 getPageData : function(){
406 var store = this.store,
407 totalCount = store.getTotalCount();
411 currentPage : store.currentPage,
412 pageCount: Math.ceil(totalCount / store.pageSize),
413 fromRecord: ((store.currentPage - 1) * store.pageSize) + 1,
414 toRecord: Math.min(store.currentPage * store.pageSize, totalCount)
420 onLoadError : function(){
421 if (!this.rendered) {
424 this.child('#refresh').enable();
428 readPageFromInput : function(pageData){
429 var v = this.child('#inputItem').getValue(),
430 pageNum = parseInt(v, 10);
432 if (!v || isNaN(pageNum)) {
433 this.child('#inputItem').setValue(pageData.currentPage);
439 onPagingFocus : function(){
440 this.child('#inputItem').select();
444 onPagingBlur : function(e){
445 var curPage = this.getPageData().currentPage;
446 this.child('#inputItem').setValue(curPage);
450 onPagingKeyDown : function(field, e){
453 pageData = me.getPageData(),
454 increment = e.shiftKey ? 10 : 1,
459 pageNum = me.readPageFromInput(pageData);
460 if (pageNum !== false) {
461 pageNum = Math.min(Math.max(1, pageNum), pageData.pageCount);
462 if(me.fireEvent('beforechange', me, pageNum) !== false){
463 me.store.loadPage(pageNum);
466 } else if (k == e.HOME || k == e.END) {
468 pageNum = k == e.HOME ? 1 : pageData.pageCount;
469 field.setValue(pageNum);
470 } else if (k == e.UP || k == e.PAGEUP || k == e.DOWN || k == e.PAGEDOWN) {
472 pageNum = me.readPageFromInput(pageData);
474 if (k == e.DOWN || k == e.PAGEDOWN) {
477 pageNum += increment;
478 if (pageNum >= 1 && pageNum <= pageData.pages) {
479 field.setValue(pageNum);
486 beforeLoad : function(){
487 if(this.rendered && this.refresh){
488 this.refresh.disable();
493 doLoad : function(start){
494 if(this.fireEvent('beforechange', this, o) !== false){
499 <span id='Ext-toolbar-Paging-method-moveFirst'> /**
500 </span> * Move to the first page, has the same effect as clicking the 'first' button.
502 moveFirst : function(){
503 if (this.fireEvent('beforechange', this, 1) !== false){
504 this.store.loadPage(1);
508 <span id='Ext-toolbar-Paging-method-movePrevious'> /**
509 </span> * Move to the previous page, has the same effect as clicking the 'previous' button.
511 movePrevious : function(){
513 prev = me.store.currentPage - 1;
516 if (me.fireEvent('beforechange', me, prev) !== false) {
517 me.store.previousPage();
522 <span id='Ext-toolbar-Paging-method-moveNext'> /**
523 </span> * Move to the next page, has the same effect as clicking the 'next' button.
525 moveNext : function(){
527 total = me.getPageData().pageCount,
528 next = me.store.currentPage + 1;
530 if (next <= total) {
531 if (me.fireEvent('beforechange', me, next) !== false) {
537 <span id='Ext-toolbar-Paging-method-moveLast'> /**
538 </span> * Move to the last page, has the same effect as clicking the 'last' button.
540 moveLast : function(){
542 last = me.getPageData().pageCount;
544 if (me.fireEvent('beforechange', me, last) !== false) {
545 me.store.loadPage(last);
549 <span id='Ext-toolbar-Paging-method-doRefresh'> /**
550 </span> * Refresh the current page, has the same effect as clicking the 'refresh' button.
552 doRefresh : function(){
554 current = me.store.currentPage;
556 if (me.fireEvent('beforechange', me, current) !== false) {
557 me.store.loadPage(current);
561 <span id='Ext-toolbar-Paging-method-bindStore'> /**
562 </span> * Binds the paging toolbar to the specified {@link Ext.data.Store}
563 * @param {Ext.data.Store} store The store to bind to this toolbar
564 * @param {Boolean} initial (Optional) true to not remove listeners
566 bindStore : function(store, initial){
569 if (!initial && me.store) {
570 if(store !== me.store && me.store.autoDestroy){
571 me.store.destroyStore();
573 me.store.un('beforeload', me.beforeLoad, me);
574 me.store.un('load', me.onLoad, me);
575 me.store.un('exception', me.onLoadError, me);
582 store = Ext.data.StoreManager.lookup(store);
585 beforeload: me.beforeLoad,
587 exception: me.onLoadError
593 <span id='Ext-toolbar-Paging-method-unbind'> /**
594 </span> * Unbinds the paging toolbar from the specified {@link Ext.data.Store} **(deprecated)**
595 * @param {Ext.data.Store} store The data store to unbind
597 unbind : function(store){
598 this.bindStore(null);
601 <span id='Ext-toolbar-Paging-method-bind'> /**
602 </span> * Binds the paging toolbar to the specified {@link Ext.data.Store} **(deprecated)**
603 * @param {Ext.data.Store} store The data store to bind
605 bind : function(store){
606 this.bindStore(store);
610 onDestroy : function(){
611 this.bindStore(null);