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;
31 Roo.PagingToolbar.superclass.constructor.call(this, el, null, config);
34 this.renderButtons(this.el);
38 Roo.extend(Roo.PagingToolbar, Roo.Toolbar, {
40 * @cfg {Roo.data.Store} dataSource
41 * The underlying data store providing the paged data
44 * @cfg {String/HTMLElement/Element} container
45 * container The id or element that will contain the toolbar
48 * @cfg {Boolean} displayInfo
49 * True to display the displayMsg (defaults to false)
52 * @cfg {Number} pageSize
53 * The number of records to display per page (defaults to 20)
57 * @cfg {String} displayMsg
58 * The paging status message to display (defaults to "Displaying {start} - {end} of {total}")
60 displayMsg : 'Displaying {0} - {1} of {2}',
62 * @cfg {String} emptyMsg
63 * The message to display when no records are found (defaults to "No data to display")
65 emptyMsg : 'No data to display',
67 * Customizable piece of the default paging text (defaults to "Page")
70 beforePageText : "Page",
72 * Customizable piece of the default paging text (defaults to "of %0")
75 afterPageText : "of {0}",
77 * Customizable piece of the default paging text (defaults to "First Page")
80 firstText : "First Page",
82 * Customizable piece of the default paging text (defaults to "Previous Page")
85 prevText : "Previous Page",
87 * Customizable piece of the default paging text (defaults to "Next Page")
90 nextText : "Next Page",
92 * Customizable piece of the default paging text (defaults to "Last Page")
95 lastText : "Last Page",
97 * Customizable piece of the default paging text (defaults to "Refresh")
100 refreshText : "Refresh",
103 renderButtons : function(el){
104 Roo.PagingToolbar.superclass.render.call(this, el);
105 this.first = this.addButton({
106 tooltip: this.firstText,
107 cls: "x-btn-icon x-grid-page-first",
109 handler: this.onClick.createDelegate(this, ["first"])
111 this.prev = this.addButton({
112 tooltip: this.prevText,
113 cls: "x-btn-icon x-grid-page-prev",
115 handler: this.onClick.createDelegate(this, ["prev"])
118 this.add(this.beforePageText);
119 this.field = Roo.get(this.addDom({
124 cls: "x-grid-page-number"
126 this.field.on("keydown", this.onPagingKeydown, this);
127 this.field.on("focus", function(){this.dom.select();});
128 this.afterTextEl = this.addText(String.format(this.afterPageText, 1));
129 this.field.setHeight(18);
131 this.next = this.addButton({
132 tooltip: this.nextText,
133 cls: "x-btn-icon x-grid-page-next",
135 handler: this.onClick.createDelegate(this, ["next"])
137 this.last = this.addButton({
138 tooltip: this.lastText,
139 cls: "x-btn-icon x-grid-page-last",
141 handler: this.onClick.createDelegate(this, ["last"])
144 this.loading = this.addButton({
145 tooltip: this.refreshText,
146 cls: "x-btn-icon x-grid-loading",
147 handler: this.onClick.createDelegate(this, ["refresh"])
150 if(this.displayInfo){
151 this.displayEl = Roo.fly(this.el.dom.firstChild).createChild({cls:'x-paging-info'});
156 updateInfo : function(){
158 var count = this.ds.getCount();
159 var msg = count == 0 ?
163 this.cursor+1, this.cursor+count, this.ds.getTotalCount()
165 this.displayEl.update(msg);
170 onLoad : function(ds, r, o){
171 this.cursor = o.params ? o.params.start : 0;
172 var d = this.getPageData(), ap = d.activePage, ps = d.pages;
174 this.afterTextEl.el.innerHTML = String.format(this.afterPageText, d.pages);
175 this.field.dom.value = ap;
176 this.first.setDisabled(ap == 1);
177 this.prev.setDisabled(ap == 1);
178 this.next.setDisabled(ap == ps);
179 this.last.setDisabled(ap == ps);
180 this.loading.enable();
185 getPageData : function(){
186 var total = this.ds.getTotalCount();
189 activePage : Math.ceil((this.cursor+this.pageSize)/this.pageSize),
190 pages : total < this.pageSize ? 1 : Math.ceil(total/this.pageSize)
195 onLoadError : function(){
196 this.loading.enable();
200 onPagingKeydown : function(e){
202 var d = this.getPageData();
204 var v = this.field.dom.value, pageNum;
205 if(!v || isNaN(pageNum = parseInt(v, 10))){
206 this.field.dom.value = d.activePage;
209 pageNum = Math.min(Math.max(1, pageNum), d.pages) - 1;
210 this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
213 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))
215 var pageNum = (k == e.HOME || (k == e.DOWN && e.ctrlKey) || (k == e.LEFT && e.ctrlKey) || (k == e.PAGEDOWN && e.ctrlKey)) ? 1 : d.pages;
216 this.field.dom.value = pageNum;
217 this.ds.load({params:{start: (pageNum - 1) * this.pageSize, limit: this.pageSize}});
220 else if(k == e.UP || k == e.RIGHT || k == e.PAGEUP || k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN)
222 var v = this.field.dom.value, pageNum;
223 var increment = (e.shiftKey) ? 10 : 1;
224 if(k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN)
226 if(!v || isNaN(pageNum = parseInt(v, 10))) {
227 this.field.dom.value = d.activePage;
230 else if(parseInt(v, 10) + increment >= 1 & parseInt(v, 10) + increment <= d.pages)
232 this.field.dom.value = parseInt(v, 10) + increment;
233 pageNum = Math.min(Math.max(1, pageNum + increment), d.pages) - 1;
234 this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
241 beforeLoad : function(){
243 this.loading.disable();
248 onClick : function(which){
252 ds.load({params:{start: 0, limit: this.pageSize}});
255 ds.load({params:{start: Math.max(0, this.cursor-this.pageSize), limit: this.pageSize}});
258 ds.load({params:{start: this.cursor+this.pageSize, limit: this.pageSize}});
261 var total = ds.getTotalCount();
262 var extra = total % this.pageSize;
263 var lastStart = extra ? (total - extra) : total-this.pageSize;
264 ds.load({params:{start: lastStart, limit: this.pageSize}});
267 ds.load({params:{start: this.cursor, limit: this.pageSize}});
273 * Unbinds the paging toolbar from the specified {@link Roo.data.Store}
274 * @param {Roo.data.Store} store The data store to unbind
276 unbind : function(ds){
277 ds.un("beforeload", this.beforeLoad, this);
278 ds.un("load", this.onLoad, this);
279 ds.un("loadexception", this.onLoadError, this);
280 ds.un("remove", this.updateInfo, this);
281 ds.un("add", this.updateInfo, this);
286 * Binds the paging toolbar to the specified {@link Roo.data.Store}
287 * @param {Roo.data.Store} store The data store to bind
290 ds.on("beforeload", this.beforeLoad, this);
291 ds.on("load", this.onLoad, this);
292 ds.on("loadexception", this.onLoadError, this);
293 ds.on("remove", this.updateInfo, this);
294 ds.on("add", this.updateInfo, this);