4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
13 * @class Roo.PagingToolbar
14 * @extends Roo.Toolbar
15 * A specialized toolbar that is bound to a {@link Roo.data.Store} and provides automatic paging controls.
17 * Create a new PagingToolbar
18 * @param {Object} config The config object
20 Roo.PagingToolbar = function(el, ds, config)
22 // old args format still supported... - xtype is prefered..
23 if (typeof(el) == 'object' && el.xtype) {
24 // created from xtype...
27 el = config.container;
35 Roo.PagingToolbar.superclass.constructor.call(this, el, null, config);
38 this.renderButtons(this.el);
41 Roo.each(items, function(e) {
42 _this.add(Roo.factory(e));
47 Roo.extend(Roo.PagingToolbar, Roo.Toolbar, {
49 * @cfg {Roo.data.Store} dataSource
50 * The underlying data store providing the paged data
53 * @cfg {String/HTMLElement/Element} container
54 * container The id or element that will contain the toolbar
57 * @cfg {Boolean} displayInfo
58 * True to display the displayMsg (defaults to false)
61 * @cfg {Number} pageSize
62 * The number of records to display per page (defaults to 20)
66 * @cfg {String} displayMsg
67 * The paging status message to display (defaults to "Displaying {start} - {end} of {total}")
69 displayMsg : 'Displaying {0} - {1} of {2}',
71 * @cfg {String} emptyMsg
72 * The message to display when no records are found (defaults to "No data to display")
74 emptyMsg : 'No data to display',
76 * Customizable piece of the default paging text (defaults to "Page")
79 beforePageText : "Page",
81 * Customizable piece of the default paging text (defaults to "of %0")
84 afterPageText : "of {0}",
86 * Customizable piece of the default paging text (defaults to "First Page")
89 firstText : "First Page",
91 * Customizable piece of the default paging text (defaults to "Previous Page")
94 prevText : "Previous Page",
96 * Customizable piece of the default paging text (defaults to "Next Page")
99 nextText : "Next Page",
101 * Customizable piece of the default paging text (defaults to "Last Page")
104 lastText : "Last Page",
106 * Customizable piece of the default paging text (defaults to "Refresh")
109 refreshText : "Refresh",
112 renderButtons : function(el){
113 Roo.PagingToolbar.superclass.render.call(this, el);
114 this.first = this.addButton({
115 tooltip: this.firstText,
116 cls: "x-btn-icon x-grid-page-first",
118 handler: this.onClick.createDelegate(this, ["first"])
120 this.prev = this.addButton({
121 tooltip: this.prevText,
122 cls: "x-btn-icon x-grid-page-prev",
124 handler: this.onClick.createDelegate(this, ["prev"])
126 //this.addSeparator();
127 this.add(this.beforePageText);
128 this.field = Roo.get(this.addDom({
133 cls: "x-grid-page-number"
135 this.field.on("keydown", this.onPagingKeydown, this);
136 this.field.on("focus", function(){this.dom.select();});
137 this.afterTextEl = this.addText(String.format(this.afterPageText, 1));
138 this.field.setHeight(18);
139 //this.addSeparator();
140 this.next = this.addButton({
141 tooltip: this.nextText,
142 cls: "x-btn-icon x-grid-page-next",
144 handler: this.onClick.createDelegate(this, ["next"])
146 this.last = this.addButton({
147 tooltip: this.lastText,
148 cls: "x-btn-icon x-grid-page-last",
150 handler: this.onClick.createDelegate(this, ["last"])
152 //this.addSeparator();
153 this.loading = this.addButton({
154 tooltip: this.refreshText,
155 cls: "x-btn-icon x-grid-loading",
156 handler: this.onClick.createDelegate(this, ["refresh"])
159 if(this.displayInfo){
160 this.displayEl = Roo.fly(this.el.dom.firstChild).createChild({cls:'x-paging-info'});
165 updateInfo : function(){
167 var count = this.ds.getCount();
168 var msg = count == 0 ?
172 this.cursor+1, this.cursor+count, this.ds.getTotalCount()
174 this.displayEl.update(msg);
179 onLoad : function(ds, r, o){
180 this.cursor = o.params ? o.params.start : 0;
181 var d = this.getPageData(), ap = d.activePage, ps = d.pages;
183 this.afterTextEl.el.innerHTML = String.format(this.afterPageText, d.pages);
184 this.field.dom.value = ap;
185 this.first.setDisabled(ap == 1);
186 this.prev.setDisabled(ap == 1);
187 this.next.setDisabled(ap == ps);
188 this.last.setDisabled(ap == ps);
189 this.loading.enable();
194 getPageData : function(){
195 var total = this.ds.getTotalCount();
198 activePage : Math.ceil((this.cursor+this.pageSize)/this.pageSize),
199 pages : total < this.pageSize ? 1 : Math.ceil(total/this.pageSize)
204 onLoadError : function(){
205 this.loading.enable();
209 onPagingKeydown : function(e){
211 var d = this.getPageData();
213 var v = this.field.dom.value, pageNum;
214 if(!v || isNaN(pageNum = parseInt(v, 10))){
215 this.field.dom.value = d.activePage;
218 pageNum = Math.min(Math.max(1, pageNum), d.pages) - 1;
219 this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
222 else if(k == e.HOME || (k == e.UP && e.ctrlKey) || (k == e.PAGEUP && e.ctrlKey) || (k == e.RIGHT && e.ctrlKey) || k == e.END || (k == e.DOWN && e.ctrlKey) || (k == e.LEFT && e.ctrlKey) || (k == e.PAGEDOWN && e.ctrlKey))
224 var pageNum = (k == e.HOME || (k == e.DOWN && e.ctrlKey) || (k == e.LEFT && e.ctrlKey) || (k == e.PAGEDOWN && e.ctrlKey)) ? 1 : d.pages;
225 this.field.dom.value = pageNum;
226 this.ds.load({params:{start: (pageNum - 1) * this.pageSize, limit: this.pageSize}});
229 else if(k == e.UP || k == e.RIGHT || k == e.PAGEUP || k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN)
231 var v = this.field.dom.value, pageNum;
232 var increment = (e.shiftKey) ? 10 : 1;
233 if(k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN)
235 if(!v || isNaN(pageNum = parseInt(v, 10))) {
236 this.field.dom.value = d.activePage;
239 else if(parseInt(v, 10) + increment >= 1 & parseInt(v, 10) + increment <= d.pages)
241 this.field.dom.value = parseInt(v, 10) + increment;
242 pageNum = Math.min(Math.max(1, pageNum + increment), d.pages) - 1;
243 this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
250 beforeLoad : function(){
252 this.loading.disable();
257 onClick : function(which){
261 ds.load({params:{start: 0, limit: this.pageSize}});
264 ds.load({params:{start: Math.max(0, this.cursor-this.pageSize), limit: this.pageSize}});
267 ds.load({params:{start: this.cursor+this.pageSize, limit: this.pageSize}});
270 var total = ds.getTotalCount();
271 var extra = total % this.pageSize;
272 var lastStart = extra ? (total - extra) : total-this.pageSize;
273 ds.load({params:{start: lastStart, limit: this.pageSize}});
276 ds.load({params:{start: this.cursor, limit: this.pageSize}});
282 * Unbinds the paging toolbar from the specified {@link Roo.data.Store}
283 * @param {Roo.data.Store} store The data store to unbind
285 unbind : function(ds){
286 ds.un("beforeload", this.beforeLoad, this);
287 ds.un("load", this.onLoad, this);
288 ds.un("loadexception", this.onLoadError, this);
289 ds.un("remove", this.updateInfo, this);
290 ds.un("add", this.updateInfo, this);
295 * Binds the paging toolbar to the specified {@link Roo.data.Store}
296 * @param {Roo.data.Store} store The data store to bind
299 ds.on("beforeload", this.beforeLoad, this);
300 ds.on("load", this.onLoad, this);
301 ds.on("loadexception", this.onLoadError, this);
302 ds.on("remove", this.updateInfo, this);
303 ds.on("add", this.updateInfo, this);