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 // supprot items array.
43 Roo.each(items, function(e) {
44 this.add(Roo.factory(e));
49 Roo.extend(Roo.PagingToolbar, Roo.Toolbar, {
51 * @cfg {Roo.data.Store} dataSource
52 * The underlying data store providing the paged data
55 * @cfg {String/HTMLElement/Element} container
56 * container The id or element that will contain the toolbar
59 * @cfg {Boolean} displayInfo
60 * 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)
237 if(!v || isNaN(pageNum = parseInt(v, 10))) {
238 this.field.dom.value = d.activePage;
241 else if(parseInt(v, 10) + increment >= 1 & parseInt(v, 10) + increment <= d.pages)
243 this.field.dom.value = parseInt(v, 10) + increment;
244 pageNum = Math.min(Math.max(1, pageNum + increment), d.pages) - 1;
245 this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
252 beforeLoad : function(){
254 this.loading.disable();
259 onClick : function(which){
263 ds.load({params:{start: 0, limit: this.pageSize}});
266 ds.load({params:{start: Math.max(0, this.cursor-this.pageSize), limit: this.pageSize}});
269 ds.load({params:{start: this.cursor+this.pageSize, limit: this.pageSize}});
272 var total = ds.getTotalCount();
273 var extra = total % this.pageSize;
274 var lastStart = extra ? (total - extra) : total-this.pageSize;
275 ds.load({params:{start: lastStart, limit: this.pageSize}});
278 ds.load({params:{start: this.cursor, limit: this.pageSize}});
284 * Unbinds the paging toolbar from the specified {@link Roo.data.Store}
285 * @param {Roo.data.Store} store The data store to unbind
287 unbind : function(ds){
288 ds.un("beforeload", this.beforeLoad, this);
289 ds.un("load", this.onLoad, this);
290 ds.un("loadexception", this.onLoadError, this);
291 ds.un("remove", this.updateInfo, this);
292 ds.un("add", this.updateInfo, this);
297 * Binds the paging toolbar to the specified {@link Roo.data.Store}
298 * @param {Roo.data.Store} store The data store to bind
301 ds.on("beforeload", this.beforeLoad, this);
302 ds.on("load", this.onLoad, this);
303 ds.on("loadexception", this.onLoadError, this);
304 ds.on("remove", this.updateInfo, this);
305 ds.on("add", this.updateInfo, this);