3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js"><div id="cls-Ext.PagingToolbar"></div>/**
9 * @class Ext.PagingToolbar
10 * @extends Ext.Toolbar
11 * <p>As the amount of records increases, the time required for the browser to render
12 * them increases. Paging is used to reduce the amount of data exchanged with the client.
13 * Note: if there are more records/rows than can be viewed in the available screen area, vertical
14 * scrollbars will be added.</p>
15 * <p>Paging is typically handled on the server side (see exception below). The client sends
16 * parameters to the server side, which the server needs to interpret and then respond with the
17 * approprate data.</p>
18 * <p><b>Ext.PagingToolbar</b> is a specialized toolbar that is bound to a {@link Ext.data.Store}
19 * and provides automatic paging control. This Component {@link Ext.data.Store#load load}s blocks
20 * of data into the <tt>{@link #store}</tt> by passing {@link Ext.data.Store#paramNames paramNames} used for
21 * paging criteria.</p>
22 * <p>PagingToolbar is typically used as one of the Grid's toolbars:</p>
24 Ext.QuickTips.init(); // to display button quicktips
26 var myStore = new Ext.data.Store({
30 var myPageSize = 25; // server script should only send back 25 items
32 var grid = new Ext.grid.GridPanel({
35 bbar: new Ext.PagingToolbar({
36 {@link #store}: myStore, // grid and PagingToolbar using same store
37 {@link #displayInfo}: true,
38 {@link #pageSize}: myPageSize,
39 {@link #prependButtons}: true,
47 * <p>To use paging, pass the paging requirements to the server when the store is first loaded.</p>
51 start: 0, // specify params for the first page load if using paging
57 * <p><u>Paging with Local Data</u></p>
58 * <p>Paging can also be accomplished with local data using extensions:</p>
59 * <div class="mdetail-params"><ul>
60 * <li><a href="http://extjs.com/forum/showthread.php?t=57386">Ext.ux.data.PagingStore</a></li>
61 * <li>Paging Memory Proxy (examples/ux/PagingMemoryProxy.js)</li>
63 * @constructor Create a new PagingToolbar
64 * @param {Object} config The config object
71 Ext.PagingToolbar = Ext.extend(Ext.Toolbar, {
72 <div id="cfg-Ext.PagingToolbar-store"></div>/**
73 * @cfg {Ext.data.Store} store
74 * The {@link Ext.data.Store} the paging toolbar should use as its data source (required).
76 <div id="cfg-Ext.PagingToolbar-displayInfo"></div>/**
77 * @cfg {Boolean} displayInfo
78 * <tt>true</tt> to display the displayMsg (defaults to <tt>false</tt>)
80 <div id="cfg-Ext.PagingToolbar-pageSize"></div>/**
81 * @cfg {Number} pageSize
82 * The number of records to display per page (defaults to <tt>20</tt>)
85 <div id="cfg-Ext.PagingToolbar-prependButtons"></div>/**
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>.
90 <div id="cfg-Ext.PagingToolbar-displayMsg"></div>/**
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}',
98 <div id="cfg-Ext.PagingToolbar-emptyMsg"></div>/**
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',
103 <div id="cfg-Ext.PagingToolbar-beforePageText"></div>/**
104 * @cfg {String} beforePageText
105 * The text displayed before the input item (defaults to <tt>'Page'</tt>).
107 beforePageText : 'Page',
108 <div id="cfg-Ext.PagingToolbar-afterPageText"></div>/**
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}',
116 <div id="cfg-Ext.PagingToolbar-firstText"></div>/**
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',
122 <div id="cfg-Ext.PagingToolbar-prevText"></div>/**
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',
128 <div id="cfg-Ext.PagingToolbar-nextText"></div>/**
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',
134 <div id="cfg-Ext.PagingToolbar-lastText"></div>/**
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',
140 <div id="cfg-Ext.PagingToolbar-refreshText"></div>/**
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',
147 <div id="prop-Ext.PagingToolbar-paramNames"></div>/**
148 * <p><b>Deprecated</b>. <code>paramNames</code> should be set in the <b>data store</b>
149 * (see {@link Ext.data.Store#paramNames}).</p>
150 * <br><p>Object mapping of parameter names used for load calls, initially set to:</p>
151 * <pre>{start: 'start', limit: 'limit'}</pre>
153 * @property paramNames
157 <div id="prop-Ext.PagingToolbar-pageSize"></div>/**
158 * The number of records to display per page. See also <tt>{@link #cursor}</tt>.
163 <div id="prop-Ext.PagingToolbar-cursor"></div>/**
164 * Indicator for the record position. This property might be used to get the active page
165 * number for example:<pre><code>
166 * // t is reference to the paging toolbar instance
167 * var activePage = Math.ceil((t.cursor + t.pageSize) / t.pageSize);
173 initComponent : function(){
174 var pagingItems = [this.first = new T.Button({
175 tooltip: this.firstText,
176 overflowText: this.firstText,
177 iconCls: 'x-tbar-page-first',
179 handler: this.moveFirst,
181 }), this.prev = new T.Button({
182 tooltip: this.prevText,
183 overflowText: this.prevText,
184 iconCls: 'x-tbar-page-prev',
186 handler: this.movePrevious,
188 }), '-', this.beforePageText,
189 this.inputItem = new Ext.form.NumberField({
190 cls: 'x-tbar-page-number',
191 allowDecimals: false,
192 allowNegative: false,
193 enableKeyEvents: true,
197 keydown: this.onPagingKeyDown,
198 blur: this.onPagingBlur
200 }), this.afterTextItem = new T.TextItem({
201 text: String.format(this.afterPageText, 1)
202 }), '-', this.next = new T.Button({
203 tooltip: this.nextText,
204 overflowText: this.nextText,
205 iconCls: 'x-tbar-page-next',
207 handler: this.moveNext,
209 }), this.last = new T.Button({
210 tooltip: this.lastText,
211 overflowText: this.lastText,
212 iconCls: 'x-tbar-page-last',
214 handler: this.moveLast,
216 }), '-', this.refresh = new T.Button({
217 tooltip: this.refreshText,
218 overflowText: this.refreshText,
219 iconCls: 'x-tbar-loading',
220 handler: this.refresh,
225 var userItems = this.items || this.buttons || [];
226 if (this.prependButtons) {
227 this.items = userItems.concat(pagingItems);
229 this.items = pagingItems.concat(userItems);
232 if(this.displayInfo){
233 this.items.push('->');
234 this.items.push(this.displayItem = new T.TextItem({}));
236 Ext.PagingToolbar.superclass.initComponent.call(this);
238 <div id="event-Ext.PagingToolbar-change"></div>/**
240 * Fires after the active page has been changed.
241 * @param {Ext.PagingToolbar} this
242 * @param {Object} pageData An object that has these properties:<ul>
243 * <li><code>total</code> : Number <div class="sub-desc">The total number of records in the dataset as
244 * returned by the server</div></li>
245 * <li><code>activePage</code> : Number <div class="sub-desc">The current page number</div></li>
246 * <li><code>pages</code> : Number <div class="sub-desc">The total number of pages (calculated from
247 * the total number of records in the dataset as returned by the server and the current {@link #pageSize})</div></li>
251 <div id="event-Ext.PagingToolbar-beforechange"></div>/**
252 * @event beforechange
253 * Fires just before the active page is changed.
254 * Return false to prevent the active page from being changed.
255 * @param {Ext.PagingToolbar} this
256 * @param {Object} params An object hash of the parameters which the PagingToolbar will send when
257 * loading the required page. This will contain:<ul>
258 * <li><code>start</code> : Number <div class="sub-desc">The starting row number for the next page of records to
259 * be retrieved from the server</div></li>
260 * <li><code>limit</code> : Number <div class="sub-desc">The number of records to be retrieved from the server</div></li>
262 * <p>(note: the names of the <b>start</b> and <b>limit</b> properties are determined
263 * by the store's {@link Ext.data.Store#paramNames paramNames} property.)</p>
264 * <p>Parameters may be added as required in the event handler.</p>
268 this.on('afterlayout', this.onFirstLayout, this, {single: true});
270 this.bindStore(this.store);
274 onFirstLayout : function(){
276 this.onLoad.apply(this, this.dsLoaded);
281 updateInfo : function(){
282 if(this.displayItem){
283 var count = this.store.getCount();
284 var msg = count == 0 ?
288 this.cursor+1, this.cursor+count, this.store.getTotalCount()
290 this.displayItem.setText(msg);
295 onLoad : function(store, r, o){
297 this.dsLoaded = [store, r, o];
300 var p = this.getParams();
301 this.cursor = (o.params && o.params[p.start]) ? o.params[p.start] : 0;
302 var d = this.getPageData(), ap = d.activePage, ps = d.pages;
304 this.afterTextItem.setText(String.format(this.afterPageText, d.pages));
305 this.inputItem.setValue(ap);
306 this.first.setDisabled(ap == 1);
307 this.prev.setDisabled(ap == 1);
308 this.next.setDisabled(ap == ps);
309 this.last.setDisabled(ap == ps);
310 this.refresh.enable();
312 this.fireEvent('change', this, d);
316 getPageData : function(){
317 var total = this.store.getTotalCount();
320 activePage : Math.ceil((this.cursor+this.pageSize)/this.pageSize),
321 pages : total < this.pageSize ? 1 : Math.ceil(total/this.pageSize)
325 <div id="method-Ext.PagingToolbar-changePage"></div>/**
326 * Change the active page
327 * @param {Integer} page The page to display
329 changePage : function(page){
330 this.doLoad(((page-1) * this.pageSize).constrain(0, this.store.getTotalCount()));
334 onLoadError : function(){
338 this.refresh.enable();
342 readPage : function(d){
343 var v = this.inputItem.getValue(), pageNum;
344 if (!v || isNaN(pageNum = parseInt(v, 10))) {
345 this.inputItem.setValue(d.activePage);
351 onPagingFocus : function(){
352 this.inputItem.select();
356 onPagingBlur : function(e){
357 this.inputItem.setValue(this.getPageData().activePage);
361 onPagingKeyDown : function(field, e){
362 var k = e.getKey(), d = this.getPageData(), pageNum;
365 pageNum = this.readPage(d);
366 if(pageNum !== false){
367 pageNum = Math.min(Math.max(1, pageNum), d.pages) - 1;
368 this.doLoad(pageNum * this.pageSize);
370 }else if (k == e.HOME || k == e.END){
372 pageNum = k == e.HOME ? 1 : d.pages;
373 field.setValue(pageNum);
374 }else if (k == e.UP || k == e.PAGEUP || k == e.DOWN || k == e.PAGEDOWN){
376 if((pageNum = this.readPage(d))){
377 var increment = e.shiftKey ? 10 : 1;
378 if(k == e.DOWN || k == e.PAGEDOWN){
381 pageNum += increment;
382 if(pageNum >= 1 & pageNum <= d.pages){
383 field.setValue(pageNum);
390 getParams : function(){
391 //retain backwards compat, allow params on the toolbar itself, if they exist.
392 return this.paramNames || this.store.paramNames;
396 beforeLoad : function(){
397 if(this.rendered && this.refresh){
398 this.refresh.disable();
403 doLoad : function(start){
404 var o = {}, pn = this.getParams();
406 o[pn.limit] = this.pageSize;
407 if(this.fireEvent('beforechange', this, o) !== false){
408 this.store.load({params:o});
412 <div id="method-Ext.PagingToolbar-moveFirst"></div>/**
413 * Move to the first page, has the same effect as clicking the 'first' button.
415 moveFirst : function(){
419 <div id="method-Ext.PagingToolbar-movePrevious"></div>/**
420 * Move to the previous page, has the same effect as clicking the 'previous' button.
422 movePrevious : function(){
423 this.doLoad(Math.max(0, this.cursor-this.pageSize));
426 <div id="method-Ext.PagingToolbar-moveNext"></div>/**
427 * Move to the next page, has the same effect as clicking the 'next' button.
429 moveNext : function(){
430 this.doLoad(this.cursor+this.pageSize);
433 <div id="method-Ext.PagingToolbar-moveLast"></div>/**
434 * Move to the last page, has the same effect as clicking the 'last' button.
436 moveLast : function(){
437 var total = this.store.getTotalCount(),
438 extra = total % this.pageSize;
440 this.doLoad(extra ? (total - extra) : total - this.pageSize);
443 <div id="method-Ext.PagingToolbar-refresh"></div>/**
444 * Refresh the current page, has the same effect as clicking the 'refresh' button.
446 refresh : function(){
447 this.doLoad(this.cursor);
450 <div id="method-Ext.PagingToolbar-bindStore"></div>/**
451 * Binds the paging toolbar to the specified {@link Ext.data.Store}
452 * @param {Store} store The store to bind to this toolbar
453 * @param {Boolean} initial (Optional) true to not remove listeners
455 bindStore : function(store, initial){
457 if(!initial && this.store){
458 this.store.un('beforeload', this.beforeLoad, this);
459 this.store.un('load', this.onLoad, this);
460 this.store.un('exception', this.onLoadError, this);
461 if(store !== this.store && this.store.autoDestroy){
462 this.store.destroy();
466 store = Ext.StoreMgr.lookup(store);
469 beforeload: this.beforeLoad,
471 exception: this.onLoadError
473 doLoad = store.getCount() > 0;
477 this.onLoad(store, null, {});
481 <div id="method-Ext.PagingToolbar-unbind"></div>/**
482 * Unbinds the paging toolbar from the specified {@link Ext.data.Store} <b>(deprecated)</b>
483 * @param {Ext.data.Store} store The data store to unbind
485 unbind : function(store){
486 this.bindStore(null);
489 <div id="method-Ext.PagingToolbar-bind"></div>/**
490 * Binds the paging toolbar to the specified {@link Ext.data.Store} <b>(deprecated)</b>
491 * @param {Ext.data.Store} store The data store to bind
493 bind : function(store){
494 this.bindStore(store);
498 onDestroy : function(){
499 this.bindStore(null);
500 Ext.PagingToolbar.superclass.onDestroy.call(this);
505 Ext.reg('paging', Ext.PagingToolbar);</pre>
\r