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 * @children Roo.Toolbar.Item Roo.Toolbar.Button Roo.Toolbar.SplitButton Roo.form.Field
16 * A specialized toolbar that is bound to a {@link Roo.data.Store} and provides automatic paging controls.
18 * Create a new PagingToolbar
19 * @param {Object} config The config object
21 Roo.PagingToolbar = function(el, ds, config)
23 // old args format still supported... - xtype is prefered..
24 if (typeof(el) == 'object' && el.xtype) {
25 // created from xtype...
28 el = config.container;
36 Roo.PagingToolbar.superclass.constructor.call(this, el, null, config);
39 this.renderButtons(this.el);
42 // supprot items array.
44 Roo.each(items, function(e) {
45 this.add(Roo.factory(e));
50 Roo.extend(Roo.PagingToolbar, Roo.Toolbar, {
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)
63 * @cfg {Number} pageSize
64 * The number of records to display per page (defaults to 20)
68 * @cfg {String} displayMsg
69 * The paging status message to display (defaults to "Displaying {start} - {end} of {total}")
71 displayMsg : 'Displaying {0} - {1} of {2}',
73 * @cfg {String} emptyMsg
74 * The message to display when no records are found (defaults to "No data to display")
76 emptyMsg : 'No data to display',
78 * Customizable piece of the default paging text (defaults to "Page")
81 beforePageText : "Page",
83 * Customizable piece of the default paging text (defaults to "of %0")
86 afterPageText : "of {0}",
88 * Customizable piece of the default paging text (defaults to "First Page")
91 firstText : "First Page",
93 * Customizable piece of the default paging text (defaults to "Previous Page")
96 prevText : "Previous Page",
98 * Customizable piece of the default paging text (defaults to "Next Page")
101 nextText : "Next Page",
103 * Customizable piece of the default paging text (defaults to "Last Page")
106 lastText : "Last Page",
108 * Customizable piece of the default paging text (defaults to "Refresh")
111 refreshText : "Refresh",
114 renderButtons : function(el){
115 Roo.PagingToolbar.superclass.render.call(this, el);
116 this.first = this.addButton({
117 tooltip: this.firstText,
118 cls: "x-btn-icon x-grid-page-first",
120 handler: this.onClick.createDelegate(this, ["first"])
122 this.prev = this.addButton({
123 tooltip: this.prevText,
124 cls: "x-btn-icon x-grid-page-prev",
126 handler: this.onClick.createDelegate(this, ["prev"])
128 //this.addSeparator();
129 this.add(this.beforePageText);
130 this.field = Roo.get(this.addDom({
135 cls: "x-grid-page-number"
137 this.field.on("keydown", this.onPagingKeydown, this);
138 this.field.on("focus", function(){this.dom.select();});
139 this.afterTextEl = this.addText(String.format(this.afterPageText, 1));
140 this.field.setHeight(18);
141 //this.addSeparator();
142 this.next = this.addButton({
143 tooltip: this.nextText,
144 cls: "x-btn-icon x-grid-page-next",
146 handler: this.onClick.createDelegate(this, ["next"])
148 this.last = this.addButton({
149 tooltip: this.lastText,
150 cls: "x-btn-icon x-grid-page-last",
152 handler: this.onClick.createDelegate(this, ["last"])
154 //this.addSeparator();
155 this.loading = this.addButton({
156 tooltip: this.refreshText,
157 cls: "x-btn-icon x-grid-loading",
158 handler: this.onClick.createDelegate(this, ["refresh"])
161 if(this.displayInfo){
162 this.displayEl = Roo.fly(this.el.dom.firstChild).createChild({cls:'x-paging-info'});
167 updateInfo : function(){
169 var count = this.ds.getCount();
170 var msg = count == 0 ?
174 this.cursor+1, this.cursor+count, this.ds.getTotalCount()
176 this.displayEl.update(msg);
181 onLoad : function(ds, r, o){
182 this.cursor = o.params ? o.params.start : 0;
183 var d = this.getPageData(), ap = d.activePage, ps = d.pages;
185 this.afterTextEl.el.innerHTML = String.format(this.afterPageText, d.pages);
186 this.field.dom.value = ap;
187 this.first.setDisabled(ap == 1);
188 this.prev.setDisabled(ap == 1);
189 this.next.setDisabled(ap == ps);
190 this.last.setDisabled(ap == ps);
191 this.loading.enable();
196 getPageData : function(){
197 var total = this.ds.getTotalCount();
200 activePage : Math.ceil((this.cursor+this.pageSize)/this.pageSize),
201 pages : total < this.pageSize ? 1 : Math.ceil(total/this.pageSize)
206 onLoadError : function(){
207 this.loading.enable();
211 onPagingKeydown : function(e){
213 var d = this.getPageData();
215 var v = this.field.dom.value, pageNum;
216 if(!v || isNaN(pageNum = parseInt(v, 10))){
217 this.field.dom.value = d.activePage;
220 pageNum = Math.min(Math.max(1, pageNum), d.pages) - 1;
221 this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
224 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))
226 var pageNum = (k == e.HOME || (k == e.DOWN && e.ctrlKey) || (k == e.LEFT && e.ctrlKey) || (k == e.PAGEDOWN && e.ctrlKey)) ? 1 : d.pages;
227 this.field.dom.value = pageNum;
228 this.ds.load({params:{start: (pageNum - 1) * this.pageSize, limit: this.pageSize}});
231 else if(k == e.UP || k == e.RIGHT || k == e.PAGEUP || k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN)
233 var v = this.field.dom.value, pageNum;
234 var increment = (e.shiftKey) ? 10 : 1;
235 if(k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN) {
238 if(!v || isNaN(pageNum = parseInt(v, 10))) {
239 this.field.dom.value = d.activePage;
242 else if(parseInt(v, 10) + increment >= 1 & parseInt(v, 10) + increment <= d.pages)
244 this.field.dom.value = parseInt(v, 10) + increment;
245 pageNum = Math.min(Math.max(1, pageNum + increment), d.pages) - 1;
246 this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
253 beforeLoad : function(){
255 this.loading.disable();
259 * event that occurs when you click on the navigation buttons - can be used to trigger load of a grid.
260 * @param {String} which (first|prev|next|last|refresh) which button to press.
264 onClick : function(which){
268 ds.load({params:{start: 0, limit: this.pageSize}});
271 ds.load({params:{start: Math.max(0, this.cursor-this.pageSize), limit: this.pageSize}});
274 ds.load({params:{start: this.cursor+this.pageSize, limit: this.pageSize}});
277 var total = ds.getTotalCount();
278 var extra = total % this.pageSize;
279 var lastStart = extra ? (total - extra) : total-this.pageSize;
280 ds.load({params:{start: lastStart, limit: this.pageSize}});
283 ds.load({params:{start: this.cursor, limit: this.pageSize}});
289 * Unbinds the paging toolbar from the specified {@link Roo.data.Store}
290 * @param {Roo.data.Store} store The data store to unbind
292 unbind : function(ds){
293 ds.un("beforeload", this.beforeLoad, this);
294 ds.un("load", this.onLoad, this);
295 ds.un("loadexception", this.onLoadError, this);
296 ds.un("remove", this.updateInfo, this);
297 ds.un("add", this.updateInfo, this);
302 * Binds the paging toolbar to the specified {@link Roo.data.Store}
303 * @param {Roo.data.Store} store The data store to bind
306 ds.on("beforeload", this.beforeLoad, this);
307 ds.on("load", this.onLoad, this);
308 ds.on("loadexception", this.onLoadError, this);
309 ds.on("remove", this.updateInfo, this);
310 ds.on("add", this.updateInfo, this);