3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
\r
4 <title>The source code</title>
\r
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
8 <body onload="prettyPrint();">
\r
9 <pre class="prettyprint lang-js"><div id="cls-Ext.PagingToolbar"></div>/**
10 * @class Ext.PagingToolbar
11 * @extends Ext.Toolbar
12 * <p>As the amount of records increases, the time required for the browser to render
13 * them increases. Paging is used to reduce the amount of data exchanged with the client.
14 * Note: if there are more records/rows than can be viewed in the available screen area, vertical
15 * scrollbars will be added.</p>
16 * <p>Paging is typically handled on the server side (see exception below). The client sends
17 * parameters to the server side, which the server needs to interpret and then respond with the
18 * approprate data.</p>
19 * <p><b>Ext.PagingToolbar</b> is a specialized toolbar that is bound to a {@link Ext.data.Store}
20 * and provides automatic paging control. This Component {@link Ext.data.Store#load load}s blocks
21 * of data into the <tt>{@link #store}</tt> by passing {@link Ext.data.Store#paramNames paramNames} used for
22 * paging criteria.</p>
23 * <p>PagingToolbar is typically used as one of the Grid's toolbars:</p>
25 Ext.QuickTips.init(); // to display button quicktips
27 var myStore = new Ext.data.Store({
28 reader: new Ext.data.JsonReader({
29 {@link Ext.data.JsonReader#totalProperty totalProperty}: 'results',
35 var myPageSize = 25; // server script should only send back 25 items at a time
37 var grid = new Ext.grid.GridPanel({
40 bbar: new Ext.PagingToolbar({
41 {@link #store}: myStore, // grid and PagingToolbar using same store
42 {@link #displayInfo}: true,
43 {@link #pageSize}: myPageSize,
44 {@link #prependButtons}: true,
52 * <p>To use paging, pass the paging requirements to the server when the store is first loaded.</p>
56 // specify params for the first page load if using paging
65 * <p>If using {@link Ext.data.Store#autoLoad store's autoLoad} configuration:</p>
67 var myStore = new Ext.data.Store({
68 {@link Ext.data.Store#autoLoad autoLoad}: {params:{start: 0, limit: 25}},
73 * <p>The packet sent back from the server would have this form:</p>
78 "rows": [ // <b>*Note:</b> this must be an Array
79 { "id": 1, "name": "Bill", "occupation": "Gardener" },
80 { "id": 2, "name": "Ben", "occupation": "Horticulturalist" },
82 { "id": 25, "name": "Sue", "occupation": "Botanist" }
86 * <p><u>Paging with Local Data</u></p>
87 * <p>Paging can also be accomplished with local data using extensions:</p>
88 * <div class="mdetail-params"><ul>
89 * <li><a href="http://extjs.com/forum/showthread.php?t=71532">Ext.ux.data.PagingStore</a></li>
90 * <li>Paging Memory Proxy (examples/ux/PagingMemoryProxy.js)</li>
92 * @constructor Create a new PagingToolbar
93 * @param {Object} config The config object
100 Ext.PagingToolbar = Ext.extend(Ext.Toolbar, {
101 <div id="cfg-Ext.PagingToolbar-store"></div>/**
102 * @cfg {Ext.data.Store} store
103 * The {@link Ext.data.Store} the paging toolbar should use as its data source (required).
105 <div id="cfg-Ext.PagingToolbar-displayInfo"></div>/**
106 * @cfg {Boolean} displayInfo
107 * <tt>true</tt> to display the displayMsg (defaults to <tt>false</tt>)
109 <div id="cfg-Ext.PagingToolbar-pageSize"></div>/**
110 * @cfg {Number} pageSize
111 * The number of records to display per page (defaults to <tt>20</tt>)
114 <div id="cfg-Ext.PagingToolbar-prependButtons"></div>/**
115 * @cfg {Boolean} prependButtons
116 * <tt>true</tt> to insert any configured <tt>items</tt> <i>before</i> the paging buttons.
117 * Defaults to <tt>false</tt>.
119 <div id="cfg-Ext.PagingToolbar-displayMsg"></div>/**
120 * @cfg {String} displayMsg
121 * The paging status message to display (defaults to <tt>'Displaying {0} - {1} of {2}'</tt>).
122 * Note that this string is formatted using the braced numbers <tt>{0}-{2}</tt> as tokens
123 * that are replaced by the values for start, end and total respectively. These tokens should
124 * be preserved when overriding this string if showing those values is desired.
126 displayMsg : 'Displaying {0} - {1} of {2}',
127 <div id="cfg-Ext.PagingToolbar-emptyMsg"></div>/**
128 * @cfg {String} emptyMsg
129 * The message to display when no records are found (defaults to 'No data to display')
131 emptyMsg : 'No data to display',
132 <div id="cfg-Ext.PagingToolbar-beforePageText"></div>/**
133 * @cfg {String} beforePageText
134 * The text displayed before the input item (defaults to <tt>'Page'</tt>).
136 beforePageText : 'Page',
137 <div id="cfg-Ext.PagingToolbar-afterPageText"></div>/**
138 * @cfg {String} afterPageText
139 * Customizable piece of the default paging text (defaults to <tt>'of {0}'</tt>). Note that
140 * this string is formatted using <tt>{0}</tt> as a token that is replaced by the number of
141 * total pages. This token should be preserved when overriding this string if showing the
142 * total page count is desired.
144 afterPageText : 'of {0}',
145 <div id="cfg-Ext.PagingToolbar-firstText"></div>/**
146 * @cfg {String} firstText
147 * The quicktip text displayed for the first page button (defaults to <tt>'First Page'</tt>).
148 * <b>Note</b>: quick tips must be initialized for the quicktip to show.
150 firstText : 'First Page',
151 <div id="cfg-Ext.PagingToolbar-prevText"></div>/**
152 * @cfg {String} prevText
153 * The quicktip text displayed for the previous page button (defaults to <tt>'Previous Page'</tt>).
154 * <b>Note</b>: quick tips must be initialized for the quicktip to show.
156 prevText : 'Previous Page',
157 <div id="cfg-Ext.PagingToolbar-nextText"></div>/**
158 * @cfg {String} nextText
159 * The quicktip text displayed for the next page button (defaults to <tt>'Next Page'</tt>).
160 * <b>Note</b>: quick tips must be initialized for the quicktip to show.
162 nextText : 'Next Page',
163 <div id="cfg-Ext.PagingToolbar-lastText"></div>/**
164 * @cfg {String} lastText
165 * The quicktip text displayed for the last page button (defaults to <tt>'Last Page'</tt>).
166 * <b>Note</b>: quick tips must be initialized for the quicktip to show.
168 lastText : 'Last Page',
169 <div id="cfg-Ext.PagingToolbar-refreshText"></div>/**
170 * @cfg {String} refreshText
171 * The quicktip text displayed for the Refresh button (defaults to <tt>'Refresh'</tt>).
172 * <b>Note</b>: quick tips must be initialized for the quicktip to show.
174 refreshText : 'Refresh',
176 <div id="prop-Ext.PagingToolbar-paramNames"></div>/**
177 * <p><b>Deprecated</b>. <code>paramNames</code> should be set in the <b>data store</b>
178 * (see {@link Ext.data.Store#paramNames}).</p>
179 * <br><p>Object mapping of parameter names used for load calls, initially set to:</p>
180 * <pre>{start: 'start', limit: 'limit'}</pre>
182 * @property paramNames
186 <div id="prop-Ext.PagingToolbar-pageSize"></div>/**
187 * The number of records to display per page. See also <tt>{@link #cursor}</tt>.
192 <div id="prop-Ext.PagingToolbar-cursor"></div>/**
193 * Indicator for the record position. This property might be used to get the active page
194 * number for example:<pre><code>
195 * // t is reference to the paging toolbar instance
196 * var activePage = Math.ceil((t.cursor + t.pageSize) / t.pageSize);
202 initComponent : function(){
203 var pagingItems = [this.first = new T.Button({
204 tooltip: this.firstText,
205 overflowText: this.firstText,
206 iconCls: 'x-tbar-page-first',
208 handler: this.moveFirst,
210 }), this.prev = new T.Button({
211 tooltip: this.prevText,
212 overflowText: this.prevText,
213 iconCls: 'x-tbar-page-prev',
215 handler: this.movePrevious,
217 }), '-', this.beforePageText,
218 this.inputItem = new Ext.form.NumberField({
219 cls: 'x-tbar-page-number',
220 allowDecimals: false,
221 allowNegative: false,
222 enableKeyEvents: true,
227 keydown: this.onPagingKeyDown,
228 blur: this.onPagingBlur
230 }), this.afterTextItem = new T.TextItem({
231 text: String.format(this.afterPageText, 1)
232 }), '-', this.next = new T.Button({
233 tooltip: this.nextText,
234 overflowText: this.nextText,
235 iconCls: 'x-tbar-page-next',
237 handler: this.moveNext,
239 }), this.last = new T.Button({
240 tooltip: this.lastText,
241 overflowText: this.lastText,
242 iconCls: 'x-tbar-page-last',
244 handler: this.moveLast,
246 }), '-', this.refresh = new T.Button({
247 tooltip: this.refreshText,
248 overflowText: this.refreshText,
249 iconCls: 'x-tbar-loading',
250 handler: this.doRefresh,
255 var userItems = this.items || this.buttons || [];
256 if (this.prependButtons) {
257 this.items = userItems.concat(pagingItems);
259 this.items = pagingItems.concat(userItems);
262 if(this.displayInfo){
263 this.items.push('->');
264 this.items.push(this.displayItem = new T.TextItem({}));
266 Ext.PagingToolbar.superclass.initComponent.call(this);
268 <div id="event-Ext.PagingToolbar-change"></div>/**
270 * Fires after the active page has been changed.
271 * @param {Ext.PagingToolbar} this
272 * @param {Object} pageData An object that has these properties:<ul>
273 * <li><code>total</code> : Number <div class="sub-desc">The total number of records in the dataset as
274 * returned by the server</div></li>
275 * <li><code>activePage</code> : Number <div class="sub-desc">The current page number</div></li>
276 * <li><code>pages</code> : Number <div class="sub-desc">The total number of pages (calculated from
277 * the total number of records in the dataset as returned by the server and the current {@link #pageSize})</div></li>
281 <div id="event-Ext.PagingToolbar-beforechange"></div>/**
282 * @event beforechange
283 * Fires just before the active page is changed.
284 * Return false to prevent the active page from being changed.
285 * @param {Ext.PagingToolbar} this
286 * @param {Object} params An object hash of the parameters which the PagingToolbar will send when
287 * loading the required page. This will contain:<ul>
288 * <li><code>start</code> : Number <div class="sub-desc">The starting row number for the next page of records to
289 * be retrieved from the server</div></li>
290 * <li><code>limit</code> : Number <div class="sub-desc">The number of records to be retrieved from the server</div></li>
292 * <p>(note: the names of the <b>start</b> and <b>limit</b> properties are determined
293 * by the store's {@link Ext.data.Store#paramNames paramNames} property.)</p>
294 * <p>Parameters may be added as required in the event handler.</p>
298 this.on('afterlayout', this.onFirstLayout, this, {single: true});
300 this.bindStore(this.store, true);
304 onFirstLayout : function(){
306 this.onLoad.apply(this, this.dsLoaded);
311 updateInfo : function(){
312 if(this.displayItem){
313 var count = this.store.getCount();
314 var msg = count == 0 ?
318 this.cursor+1, this.cursor+count, this.store.getTotalCount()
320 this.displayItem.setText(msg);
325 onLoad : function(store, r, o){
327 this.dsLoaded = [store, r, o];
330 var p = this.getParams();
331 this.cursor = (o.params && o.params[p.start]) ? o.params[p.start] : 0;
332 var d = this.getPageData(), ap = d.activePage, ps = d.pages;
334 this.afterTextItem.setText(String.format(this.afterPageText, d.pages));
335 this.inputItem.setValue(ap);
336 this.first.setDisabled(ap == 1);
337 this.prev.setDisabled(ap == 1);
338 this.next.setDisabled(ap == ps);
339 this.last.setDisabled(ap == ps);
340 this.refresh.enable();
342 this.fireEvent('change', this, d);
346 getPageData : function(){
347 var total = this.store.getTotalCount();
350 activePage : Math.ceil((this.cursor+this.pageSize)/this.pageSize),
351 pages : total < this.pageSize ? 1 : Math.ceil(total/this.pageSize)
355 <div id="method-Ext.PagingToolbar-changePage"></div>/**
356 * Change the active page
357 * @param {Integer} page The page to display
359 changePage : function(page){
360 this.doLoad(((page-1) * this.pageSize).constrain(0, this.store.getTotalCount()));
364 onLoadError : function(){
368 this.refresh.enable();
372 readPage : function(d){
373 var v = this.inputItem.getValue(), pageNum;
374 if (!v || isNaN(pageNum = parseInt(v, 10))) {
375 this.inputItem.setValue(d.activePage);
381 onPagingFocus : function(){
382 this.inputItem.select();
386 onPagingBlur : function(e){
387 this.inputItem.setValue(this.getPageData().activePage);
391 onPagingKeyDown : function(field, e){
392 var k = e.getKey(), d = this.getPageData(), pageNum;
395 pageNum = this.readPage(d);
396 if(pageNum !== false){
397 pageNum = Math.min(Math.max(1, pageNum), d.pages) - 1;
398 this.doLoad(pageNum * this.pageSize);
400 }else if (k == e.HOME || k == e.END){
402 pageNum = k == e.HOME ? 1 : d.pages;
403 field.setValue(pageNum);
404 }else if (k == e.UP || k == e.PAGEUP || k == e.DOWN || k == e.PAGEDOWN){
406 if((pageNum = this.readPage(d))){
407 var increment = e.shiftKey ? 10 : 1;
408 if(k == e.DOWN || k == e.PAGEDOWN){
411 pageNum += increment;
412 if(pageNum >= 1 & pageNum <= d.pages){
413 field.setValue(pageNum);
420 getParams : function(){
421 //retain backwards compat, allow params on the toolbar itself, if they exist.
422 return this.paramNames || this.store.paramNames;
426 beforeLoad : function(){
427 if(this.rendered && this.refresh){
428 this.refresh.disable();
433 doLoad : function(start){
434 var o = {}, pn = this.getParams();
436 o[pn.limit] = this.pageSize;
437 if(this.fireEvent('beforechange', this, o) !== false){
438 this.store.load({params:o});
442 <div id="method-Ext.PagingToolbar-moveFirst"></div>/**
443 * Move to the first page, has the same effect as clicking the 'first' button.
445 moveFirst : function(){
449 <div id="method-Ext.PagingToolbar-movePrevious"></div>/**
450 * Move to the previous page, has the same effect as clicking the 'previous' button.
452 movePrevious : function(){
453 this.doLoad(Math.max(0, this.cursor-this.pageSize));
456 <div id="method-Ext.PagingToolbar-moveNext"></div>/**
457 * Move to the next page, has the same effect as clicking the 'next' button.
459 moveNext : function(){
460 this.doLoad(this.cursor+this.pageSize);
463 <div id="method-Ext.PagingToolbar-moveLast"></div>/**
464 * Move to the last page, has the same effect as clicking the 'last' button.
466 moveLast : function(){
467 var total = this.store.getTotalCount(),
468 extra = total % this.pageSize;
470 this.doLoad(extra ? (total - extra) : total - this.pageSize);
473 <div id="method-Ext.PagingToolbar-doRefresh"></div>/**
474 * Refresh the current page, has the same effect as clicking the 'refresh' button.
476 doRefresh : function(){
477 this.doLoad(this.cursor);
480 <div id="method-Ext.PagingToolbar-bindStore"></div>/**
481 * Binds the paging toolbar to the specified {@link Ext.data.Store}
482 * @param {Store} store The store to bind to this toolbar
483 * @param {Boolean} initial (Optional) true to not remove listeners
485 bindStore : function(store, initial){
487 if(!initial && this.store){
488 if(store !== this.store && this.store.autoDestroy){
489 this.store.destroy();
491 this.store.un('beforeload', this.beforeLoad, this);
492 this.store.un('load', this.onLoad, this);
493 this.store.un('exception', this.onLoadError, this);
500 store = Ext.StoreMgr.lookup(store);
503 beforeload: this.beforeLoad,
505 exception: this.onLoadError
511 this.onLoad(store, null, {});
515 <div id="method-Ext.PagingToolbar-unbind"></div>/**
516 * Unbinds the paging toolbar from the specified {@link Ext.data.Store} <b>(deprecated)</b>
517 * @param {Ext.data.Store} store The data store to unbind
519 unbind : function(store){
520 this.bindStore(null);
523 <div id="method-Ext.PagingToolbar-bind"></div>/**
524 * Binds the paging toolbar to the specified {@link Ext.data.Store} <b>(deprecated)</b>
525 * @param {Ext.data.Store} store The data store to bind
527 bind : function(store){
528 this.bindStore(store);
532 onDestroy : function(){
533 this.bindStore(null);
534 Ext.PagingToolbar.superclass.onDestroy.call(this);
539 Ext.reg('paging', Ext.PagingToolbar);</pre>
\r